当前位置: 首页 > 图文教程 > 网络编程 > JSP > Jsp问答集一

JSP
无需调优的内存优化
JAVA EXCEL API
新手指南之如何搜索你的问题的答案
Java 101:如果我不懂Java该怎么办?
jsp由浅入深
把本页内容导出成word文件或excel文件(原创)
在BEA WebLogic中使用Java消息服务
Form Your Own Design Pattern Study Group
从Coding Fan到真正的技术专家
我的Mysql5.0中文乱码解决方案
结合JAVASCRIPT将HTML导入Excel形成简单Web报表
Struts 的汉字显示问题终结解决方案
j2me网络实战指南
Java Page Flow开发:从JDBC数据源中检索并显示数据
人生、梦想、Java,又一个梦想者上路!
用Struts开发基于MVC的Web应用
Apache plug-in配置代理请求实战
一个简单的Timer Service
Tomcat 服务器下 JSP 页面中文问题及解决方法总结
Java Servlet 编程及应用(一)

JSP 中的 Jsp问答集一


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-02   浏览: 28 ::
收藏到网摘: n/a

作者:ybwen

如何混合使用Jsp和SSI #include?
在JSP中可以使用如下方式包含纯HTML:

在JSP中如何执行浏览重定向?
使用如下方式即可:response.sendRedirect("http://ybwen.home.chinaren.com/index.html");
也能物理地改变HTTP HEADER属性,如下:
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn="/newpath/index.html";
response.setHeader("Location",newLocn);
%>


如何防止在JSP或SERVLET中的输出不被BROWSER保存在CACHE中?
把如下脚本加入到JSP文件的开始即可:
<%
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>


在JSP中如何设置COOKIE?
COOKIE是作为HTTP HEADER的一部分被发送的,如下方法即可设置:
<%
Cookie mycookie = new Cookie("aName","aValue");
response.addCookie(mycookie);
%>


在JSP中如何删除一个COOKIE?
<%
Cookie killMyCookie = new Cookie("mycookie", null);
killMyCookie.setMaxAge(0);
killMyCookie.setPath("/");
response.addCookie(killMyCookie);
%>


在一个JSP的请求处理中如何停止JSP的执行
如下例:
<%
if (request.getParameter("wen") != null) {
// do something
} else {
return;
}
%>


在JSP中如何定义方法
你可以定义方法,但是你不能直接访问JSP的内置对象,而是通过参数的方法传递。如下:
<%!
public String howBadFrom(HttpServletRequest req) {
HttpSession ses = req.getSession();
...
return req.getRemoteHost();
}
%>
<%
out.print("in general,lao lee is not baddie ");
%>
<%= howBadFrom(request) %>


如果BROWSER已关闭了COOKIES,在JSP中我如何打开SESSION来跟踪
使用URL重写即可,如下:
hello1.jsp
<%@ page session="true" %>
<%
Integer num = new Integer(100);
session.putValue("num",num);
String url =response.encodeURL("hello2.jsp");
%>
<a href='<%=url%>'>hello2.jsp</a>

hello2.jsp
<%@ page session="true" %>
<%
Integer i= (Integer )session.getValue("num");
out.println("Num value in session is "+i.intValue());
%>


在JSP中能发送EMAIL吗
可以使用SUN的专用包:sun.net.smtp包。如下脚本使用SmtpClient类发送EMAIL。
<%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>
<%
String from="[email protected]";
String to="[email protected], [email protected]";
try{
SmtpClient client = new SmtpClient("mail.xxxxx.xxx");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("Cool! :-)");
message.println();
message.println("Good Boy");
message.println("I'm in genius.com");
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
%>


在SERVLET中我能调用一个JSP错误页吗
当然没问题,如下展示了如何在一个SERVLET控制逻辑单元内调用一个JSP错误页面。
protected void sendErrorRedirect(HttpServletRequest request,
HttpServletResponse response, String errorPageURL,
Throwable e)
throws ServletException, IOException {
request.setAttribute ("javax.servlet.jsp.jspException", e);
getServletConfig().getServletContext().
getRequestDispatcher(errorPageURL).forward(request,
response);
}

public void doPost(HttpServletRequest request,HttpServletResponse response) {
try {
// do something
} catch (Exception ex) {
try {
sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}


JSP和APPLET如何通讯
JSP如何与EJB SessionBean通讯
下面的代码段作了很好的示范
<%@ page import="javax.naming.*, javax.rmi.PortableRemot"