hello2.jsp < %@ page session="true" % > < % Integer i= (Integer )session.getvalue("num"); out.println("Num value in session is "+i.intvalue()); % > </P>
13、在JSP中能发送EMAIL吗 可以使用SUN的专用包:sun.net.smtp包。如下脚本使用SmtpClient类发送EMAIL。 < %@ page import="sun.net.smtp.SmtpClient, java.io.*" % > < % String from="<A href="mailto:ybwen@sina.com">ybwen@sina.com</A>"; String to="<A href="mailto:hewenjun@yeah.net">hewenjun@yeah.net</A>, <A href="mailto:lei@who.com.cn">lei@who.com.cn</A>"; 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); } % > </P> 14、在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); } </P> 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(); } } } </P>
15、JSP和APPLET如何通讯 JSP如何与EJB SessionBean通讯 下面的代码段作了很好的示范 < %@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, foo.AccountHome, foo.Account" % > < %! //定义一个对SessionBeanHome接口实例的全局引用 AccountHome accHome=null; </P> public void jspInit() { //获得Home接口实例 InitialContext cntxt = new InitialContext( ); Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB"); accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class); } % > < % //实例化SessionBean Account acct = accHome.create(); //调用远程方法 acct.doWhatever(...); // 如此等等 % > </P> |