KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > servlets > StatefulSessionServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.web.servlets;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import javax.ejb.Handle JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServlet JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34 import javax.servlet.http.HttpSessionActivationListener JavaDoc;
35 import javax.servlet.http.HttpSessionEvent JavaDoc;
36
37 import org.jboss.logging.Logger;
38 import org.jboss.test.web.interfaces.StatelessSession;
39 import org.jboss.test.web.interfaces.StatelessSessionHome;
40
41 /** A servlet that accesses a stateful session EJB and stores a handle in the session context
42  * to test retrieval of the session from the handle.
43
44  @author Scott.Stark@jboss.org
45  @version $Revision: 58115 $
46  */

47 public class StatefulSessionServlet extends HttpServlet JavaDoc
48 {
49    static private Logger log = Logger.getLogger(StatefulSessionServlet.class);
50
51    static class SessionHandle implements HttpSessionActivationListener JavaDoc
52    {
53       Handle JavaDoc h;
54       SessionHandle(Handle JavaDoc h)
55       {
56          this.h = h;
57       }
58       public void sessionWillPassivate(HttpSessionEvent JavaDoc event)
59       {
60          log.info("sessionWillPassivate, event="+event);
61       }
62
63       public void sessionDidActivate(HttpSessionEvent JavaDoc event)
64       {
65          log.info("sessionDidActivate, event="+event);
66       }
67    }
68
69    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
70          throws ServletException JavaDoc, IOException JavaDoc
71    {
72       HttpSession JavaDoc session = request.getSession();
73       try
74       {
75          StatelessSession localBean = null;
76          // See if there is an existing session
77
if( session.isNew() )
78          {
79             log.info("Creating a new stateful session");
80             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
81             Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
82             StatelessSessionHome localHome = (StatelessSessionHome) enc.lookup("ejb/StatefulEJB");
83             localBean = localHome.create();
84             Handle JavaDoc h = localBean.getHandle();
85             SessionHandle wrapper = new SessionHandle(h);
86             session.setAttribute("StatefulEJB", wrapper);
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 JavaDoc e)
97       {
98          throw new ServletException JavaDoc("Failed to call StatefulEJB", e);
99       }
100       response.setContentType("text/html");
101       PrintWriter JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response)
118          throws ServletException JavaDoc, IOException JavaDoc
119    {
120       processRequest(request, response);
121    }
122
123    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
124          throws ServletException JavaDoc, IOException JavaDoc
125    {
126       processRequest(request, response);
127    }
128 }
129
Popular Tags