blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

Verifying Session replication on Cluster

We could run a simple Login/Logout servlet to understand check session replication.

Login servlet

public class LoginServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
    public LoginServlet() {
        super();
    }

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.sendRedirect("index.jsp");
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("doPost");
  String pwd = request.getParameter("pwd");
  HttpSession session = request.getSession(true);
  if(pwd.equals("123")) {
   session.setAttribute("loggedIn", true);
   RequestDispatcher dispatcher = request.getRequestDispatcher("secure.jsp");
   dispatcher.forward(request, response);
  }else {
   RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
   dispatcher.forward(request, response);
  }
 }

}

Logout servlet

public class LogoutServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
  
 public LogoutServlet() {
        super();
    }

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  HttpSession session = request.getSession(false);
  if(session!=null){
   session.invalidate();
  }
  PrintWriter writer = response.getWriter();
  writer.println("Local port:" + request.getLocalPort());
  String memberName = (String) getServletContext().getAttribute(
    "com.ibm.websphere.servlet.application.host");
  writer.println("Cluster member:" + memberName);

  writer.flush();
  writer.close();
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }
}


A Filter to check if user is logged-in

public class LoginFilter implements Filter {

    public LoginFilter() {
    }

 public void destroy() {
 }

 public void doFilter(ServletRequest req, ServletResponse res,
   FilterChain chain) throws IOException, ServletException {
  System.out.println("doFilter");
  HttpServletRequest request = (HttpServletRequest) req;
  HttpServletResponse response = (HttpServletResponse) res;
  HttpSession session = request.getSession(false);

  if (session == null || session.getAttribute("loggedIn") == null)    {
   response.sendRedirect(request.getContextPath()+ "/index.jsp"); 
   // No logged-in user found, so redirect to login page.
  } else {
   chain.doFilter(request, response); 
   //Logged-in user found, so just continue request.
  }
 }

 public void init(FilterConfig fConfig) throws ServletException {
 }

}


Deployment descriptor configuration,


<filter>
 <display-name>LoginFilter</display-name>
 <filter-name>LoginFilter</filter-name>
 <filter-class>filters.LoginFilter</filter-class>
</filter>
<filter-mapping>
 <filter-name>LoginFilter</filter-name>
 <url-pattern>/secure.jsp</url-pattern>
</filter-mapping>
<servlet>
 <description>
 </description>
 <display-name>LoginServlet</display-name>
 <servlet-name>LoginServlet</servlet-name>
 <servlet-class>servlets.LoginServlet</servlet-class>
</servlet>
<servlet>
 <description>
 </description>
 <display-name>LogoutServlet</display-name>
 <servlet-name>LogoutServlet</servlet-name>
 <servlet-class>servlets.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>LoginServlet</servlet-name>
 <url-pattern>
 /LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
 <servlet-name>LogoutServlet</servlet-name>
 <url-pattern>
 /LogoutServlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

index.jsp

Pass code:
Local port:<%=request.getLocalPort() %>
Cluster member: <%= getServletContext().getAttribute("com.ibm.websphere.servlet.application.host") %>
Session Id:<%=request.getSession().getId() %>

secure.jsp

This is a secure page.
Local port:<%=request.getLocalPort() %>
Cluster member: <%= getServletContext().getAttribute("com.ibm.websphere.servlet.application.host") %>
Session Id:<%=request.getSession().getId() %>

Deploy this application to cluster.



After logging in,


Let's try to stop MEMBER_002


Now hit Url.

No comments:

Post a Comment