| 
| 
| 一个关于Jsp的 答集 |  
| 时间:2014-05-04 10:03:11  来源:不详  作者:佚名 |  
| 如何混合使用Jsp和SSI #include? 
 在JSP中可以使用如下方式包含 HTML:
 处理 SSI 文件时出错
 
 但是如果data.inc中包含JSP CODE ,我 可以使用:
 <%@include file="data.inc"%>
 如何 绦幸 程安全的JSP?
 
 只需增加如下指令
 
 <%@ page isThreadSafe="false" %>
 
 JSP如何 理HTML FORM中的 Y料?
 
 通 ^ 戎玫 request物件即可,如下:
 <%
 
 String item = request.getParameter("item");
 int howMany = new Integer(request.getParameter("units")).intValue();
 %>
 
 在JSP如何包含一 o B文件?
 
 o B包含如下:<%@ include file="copyright.html" %>
 B包含如下:
 在JSP中如何使用注 ?
 主要有四中方法:
 1。<%--  c --%>
 2。//
 3。/** c**/
 4。
 
 在JSP中如何 绦 g [重定向?
 
 使用如下方式即可: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"); file://HTTP 1.1
 response.setHeader("Pragma","no-cache"); file://HTTP 1.0
 response.setDateHeader ("Expires", 0); file://prevents caching at the proxy server
 %>
 
 在JSP中如何 O置COOKIE?
 
 COOKIE是作?HTTP HEADER的一部分被 l送的,如下方法即可 O置:
 <%
 Cookie mycookie = new Cookie("aName","aValue");
 response.addCookie(mycookie);
 %>
 
 在JSP中如何 h除一 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中如何定 x方法?
 
 你可以定 x方法,但是你不能直接 L JSP的 戎梦锛 ,而是通 ^ 档姆椒 鬟f。如下:
 <%!
 public String howBadFrom(HttpServletRequest req) {
 HttpSession ses = req.getSession();
 ...
 return req.getRemoteHost();
 }
 %>
 <%
 out.print("in general,lao lee is not baddie ");
 %>
 <%= howBadFrom(request) %>
 
 
 如果BROWSER已 P ]了COOKIES,在JSP中我如何打 _SESSION 砀 ?
 
 使用URL重 即可,如下:
 hello1.jsp
 <%@ page session="true" %>
 <%
 Integer num = new Integer(100);
 session.putValue("num",num);
 String url =response.encodeURL("hello2.jsp");
 %>
 hello2.jsp
 
 hello2.jsp
 <%@ page session="true" %>
 <%
 Integer i= (Integer )session.getValue("num");
 out.println("Num value in session is "+i.intValue());
 %>
 
 
 在JSP中能 l送EMAIL ?
 
 可以使用SUN的 S冒 :sun.net.smtp包。如下 _本使用SmtpClient l送EMAIL。
 <%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>
 <%
 String from="ybwen@sina.com";
 String to="hewenjun@yeah.net, lei@who.com.cn";
 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 e ` ?
 
 然 ] },如下展示了如何在一 SERVLET控制 卧 日{用一 JSP e ` 面。
 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如何 cEJB SessionBean通 ?
 
 下面的代 a段作了很好的示
 <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject,
 foo.AccountHome, foo.Account" %>
 <%!
 file://定 x一    SessionBeanHome介面  例的全局引用
 AccountHome accHome=null;
 public void jspInit() {
 file:// @得Home介面  例
 InitialContext cntxt = new InitialContext( );
 Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB");
 accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class);
 }
 %>
 <%
 file://  例化SessionBean
 Account acct = accHome.create();
 file:// {用 h端方法
 acct.doWhatever(...);
 // 如此等等
 %>
 
 |  |  |  |