1 22 package org.jboss.test.cluster.web; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.net.InetAddress ; 27 import javax.servlet.http.HttpServlet ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import javax.servlet.http.HttpSession ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.ServletConfig ; 33 34 import org.jboss.logging.Logger; 35 36 40 public class StatefulSessionServlet extends HttpServlet 41 { 42 private static Logger log = Logger.getLogger(StatefulSessionServlet.class); 43 private boolean synchSessionAccess; 44 45 public void init(ServletConfig servletConfig) throws ServletException 46 { 47 super.init(servletConfig); 48 String flag = servletConfig.getInitParameter("synchSessionAccess"); 49 synchSessionAccess = Boolean.valueOf(flag).booleanValue(); 50 } 51 52 protected void doGet(HttpServletRequest request, HttpServletResponse response) 53 throws ServletException , IOException 54 { 55 handleRequest(request, response); 56 } 57 58 protected void doPost(HttpServletRequest request, HttpServletResponse response) 59 throws ServletException , IOException 60 { 61 handleRequest(request, response); 62 } 63 64 private void handleRequest(HttpServletRequest request, HttpServletResponse response) 65 throws IOException 66 { 67 HttpSession 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 pw = response.getWriter(); 111 response.setContentType("text/html"); 112 response.addIntHeader("X-AccessCount", value.accessCount); 113 pw.write("<html>\n"); 114 String 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 |