1 22 package org.jboss.ejb3.test.dd.web.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import javax.ejb.Handle ; 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.HttpServlet ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpSession ; 34 import javax.servlet.http.HttpSessionActivationListener ; 35 import javax.servlet.http.HttpSessionEvent ; 36 37 import org.jboss.logging.Logger; 38 import org.jboss.ejb3.test.dd.web.interfaces.StatelessSession; 39 import org.jboss.security.SecurityAssociation; 40 import org.jboss.security.SimplePrincipal; 41 42 48 public class StatefulSessionServlet extends HttpServlet 49 { 50 static private Logger log = Logger.getLogger(StatefulSessionServlet.class); 51 52 static class SessionHandle implements HttpSessionActivationListener 53 { 54 Handle h; 55 SessionHandle(Handle h) 56 { 57 this.h = h; 58 } 59 public void sessionWillPassivate(HttpSessionEvent event) 60 { 61 log.info("sessionWillPassivate, event="+event); 62 } 63 64 public void sessionDidActivate(HttpSessionEvent event) 65 { 66 log.info("sessionDidActivate, event="+event); 67 } 68 } 69 70 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 71 throws ServletException , IOException 72 { 73 SecurityAssociation.setPrincipal(new SimplePrincipal("jduke")); 74 SecurityAssociation.setCredential("theduke".toCharArray()); 75 76 HttpSession session = request.getSession(); 77 try 78 { 79 StatelessSession localBean = null; 80 if( session.isNew() ) 82 { 83 log.info("Creating a new stateful session"); 84 InitialContext ctx = new InitialContext (); 85 Context enc = (Context ) ctx.lookup("java:comp/env"); 86 localBean = (StatelessSession) enc.lookup("ejb/StatefulEJB"); 87 } 88 else 89 { 90 log.info("Getting existing stateful session"); 91 SessionHandle wrapper = (SessionHandle) session.getAttribute("StatefulEJB"); 92 localBean = (StatelessSession) wrapper.h.getEJBObject(); 93 } 94 localBean.echo("Hello"); 95 } 96 catch (Exception e) 97 { 98 throw new ServletException ("Failed to call StatefulEJB", e); 99 } 100 response.setContentType("text/html"); 101 PrintWriter out = response.getWriter(); 102 out.println("<html>"); 103 out.println("<head><title>StatefulSessionServlet</title></head>"); 104 out.println("<body>"); 105 out.println("<h1>Session Information</h1>"); 106 out.println("SessionID: "+session.getId()); 107 out.println("IsNew: "+session.isNew()); 108 out.println("CreationTime: "+session.getCreationTime()); 109 out.println("LastAccessedTime: "+session.getLastAccessedTime()); 110 out.println("Now: "+System.currentTimeMillis()); 111 out.println("MaxInactiveInterval: "+session.getMaxInactiveInterval()); 112 out.println("</body>"); 113 out.println("</html>"); 114 out.close(); 115 } 116 117 protected void doGet(HttpServletRequest request, HttpServletResponse response) 118 throws ServletException , IOException 119 { 120 processRequest(request, response); 121 } 122 123 protected void doPost(HttpServletRequest request, HttpServletResponse response) 124 throws ServletException , IOException 125 { 126 processRequest(request, response); 127 } 128 } 129 | Popular Tags |