9、在JSP中如何删除一个COOKIE? <% Cookie killMyCookie = new Cookie("mycookie", null); killMyCookie.setMaxAge(0); killMyCookie.setPath("/"); response.addCookie(killMyCookie); %>
10、在一个JSP的请求处理中如何停止JSP的执行 如下例: <% if (request.getParameter("wen") != null) { // do something } else { return; } %>
11、在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) %>
12、如果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()); %>
13、在JSP中能发送EMAIL吗 可以使用SUN的专用包:sun.net.smtp包。如下脚本使用SmtpClient类发送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("Im in genius.com"); message.println(); client.closeServer(); } catch (IOException e){ System.out.println("ERROR SENDING EMAIL:"+e); } %>
 
2/2 首页 上一页 1 2 |