KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > web > 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.cluster.web;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import javax.servlet.http.HttpServlet JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.ServletConfig JavaDoc;
33
34 import org.jboss.logging.Logger;
35
36 /**
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 58115 $
39  */

40 public class StatefulSessionServlet extends HttpServlet JavaDoc
41 {
42    private static Logger log = Logger.getLogger(StatefulSessionServlet.class);
43    private boolean synchSessionAccess;
44
45    public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc
46    {
47       super.init(servletConfig);
48       String JavaDoc flag = servletConfig.getInitParameter("synchSessionAccess");
49       synchSessionAccess = Boolean.valueOf(flag).booleanValue();
50    }
51
52    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
53       throws ServletException JavaDoc, IOException JavaDoc
54    {
55       handleRequest(request, response);
56    }
57
58    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
59       throws ServletException JavaDoc, IOException JavaDoc
60    {
61       handleRequest(request, response);
62    }
63
64    private void handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
65       throws IOException JavaDoc
66    {
67       HttpSession JavaDoc session = request.getSession();
68       log.info("handleRequest, session="+session.getId());
69       SessionValue value = null;
70       if( synchSessionAccess )
71       {
72          synchronized( session )
73          {
74             value = (SessionValue) session.getAttribute("TheSessionKey");
75          }
76       }
77       else
78       {
79          value = (SessionValue) session.getAttribute("TheSessionKey");
80       }
81
82       SessionValue prevValue = null;
83       if( value == null )
84       {
85          value = new SessionValue();
86       }
87       else
88       {
89          prevValue = new SessionValue();
90          prevValue.accessCount = value.accessCount;
91          prevValue.username = value.username;
92          prevValue.lastAccessHost = value.lastAccessHost;
93       }
94       value.accessCount ++;
95       value.username = request.getRemoteUser();
96       value.lastAccessHost = request.getServerName();
97       if( synchSessionAccess )
98       {
99          synchronized( session )
100          {
101             session.setAttribute("TheSessionKey", value);
102          }
103       }
104       else
105       {
106          session.setAttribute("TheSessionKey", value);
107       }
108       log.info(value);
109
110       PrintWriter JavaDoc pw = response.getWriter();
111       response.setContentType("text/html");
112       response.addIntHeader("X-AccessCount", value.accessCount);
113       pw.write("<html>\n");
114       String JavaDoc node = InetAddress.getLocalHost().getHostName();
115       pw.write("<head><title>StatefulSessionServlet on: "+node+"</title></head>\n");
116       pw.write("<body><h1>StatefulSessionServlet on: "+node+"</h1>\n");
117       pw.write("<h2>SessionID: "+session.getId()+"</h2>\n");
118       pw.write("<pre>\n");
119       pw.write(value.toString());
120       pw.write("\n</pre>\n");
121       if( prevValue != null )
122       {
123          pw.write("<pre>\n");
124          pw.write(prevValue.toString());
125          pw.write("\n</pre>\n");
126       }
127       pw.write("</body>\n");
128       pw.write("</html>\n");
129    }
130 }
131
Popular Tags