package examples.servlets; import examples.utils.common.ExampleUtils; import java.io.IOException; import java.io.PrintWriter; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.*; import weblogic.management.MBeanHome; public class SessionServlet extends HttpServlet { public void doGet HttpServletRequest(req, HttpServletResponse res) throws ServletException, IOException { //Get the session object HttpSession session = req.getSession(true); // Local variables String serverName; String failoverMessage = ""; String str = (String) session.getAttribute("simplesession.servername"); PrintWriter out = res.getWriter(); Integer ival; // set content type and other response header fields first res.setContentType("text/html"); // Retrieve the count value from the session ival = (Integer) session.getAttribute("sessiontest.counter"); if (ival==null) ival = new Integer(1); else ival = new Integer(ival.intValue() + 1); session.setAttribute("sessiontest.counter", ival); out.println(ExampleUtils.returnHtmlHeader("Session Servlet")); try { serverName = getServerName(); if (str != null && str.equals(serverName)) failoverMessage = ""; else failoverMessage = " (failing over from server" +str+ ")"; session.setAttribute("simplesession.servername", serverName); // then write the data of the response out.println("You have hit this page <b> + ival + "</b> times.<p>); out.println("The server currently hosting this session is <B> + serverName + "</B> + failoverMessage + "<p>); // when the user clicks on the link in the next line, // the SessionServlet is called again, // but now URL rewriting is turned on out.println("Click <a href=" + res.encodeURL("SessionServlet") + ">here</a>); out.println(" to ensure that session tracking is working even " + "if cookies aren't supported.<br>); out.println("Note that by default URL rewriting is not enabled " + "because of its expensive overhead"); out.println("<p>); out.println("<h4>Request and Session Data:</h4>); out.println("Session ID in Request: " + req.getRequestedSessionId()); out.println("<br>Session ID in Request from Cookie: " + req.isRequestedSessionIdFromCookie()); out.println("<br>Session ID in Request from URL: " + req.isRequestedSessionIdFromURL()); out.println("<br>Valid Session ID: " + req.isRequestedSessionIdValid()); out.println("<h4>Session Data:</h4>); out.println("New Session: " + session.isNew()); out.println("<br>Session ID: " + session.getId()); out.println("<br>Creation Time: " + session.getCreationTime()); out.println("<br>Last Accessed Time: " + session.getLastAccessedTime()); } catch (Exception ex) { out.println("<p><b>!! Example Failed !!<br><br> The following exception occur:</b><br><br>); ex.printStackTrace(new PrintWriter(out)); ex.printStackTrace(); } finally { out.println(ExampleUtils.returnHtmlFooter()); } } private String getServerName() throws Exception { String toReturn = null; Context myCtx = new InitialContext(); MBeanHome mbeanHome = (MBeanHome)myCtx.lookup("weblogic.management.home.localhome"); toReturn = mbeanHome.getMBeanServer().getServerName(); if (toReturn == null) return ""; else return toReturn; } }