title: struts2 Session maintaining
body: <p>I am developing a web application using struts 2 MVC framework. I am currently working on login module of it. I am new to these technologies. I am facing problems in maintaining sessions. I want that if some one directly hits the url of the profile page(page that is opened on successful login), then he or she is redirected back to the login page. Also if someone login with wrong credentials then again he is redirected back to login page. Also if he enters some login details, then first the credentials must be checked and if credentials r correct, then he session variables must be set. And before profile page is rendered, session variables are checked if they r set. If only the session variables are set, the control passes to the profile page. </p>
  
  <p>Below is my Login form code loginPage.jsp: This page displays the loginpage to the user:</p>
  
  <pre><code>    &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;LOGIN PAGE&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;s:form action="login" method="post"&gt;
      &lt;s:textfield name="login.username" label="Username"/&gt;
      &lt;s:password name="login.password" label="Password"/&gt;
      &lt;s:submit value="SUBMIT" align="center"/&gt;
      &lt;s:reset value="RESET" align="center"/&gt;
    &lt;/s:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>now is my loginAction class: This is my action class corresponding to login action that is produced when the login button is clicked on the login form.</p>
  
  <pre><code>package com.view;
  import java.util.Map;
  import org.apache.struts2.interceptor.SessionAware;
  import com.controller.LoginManager;
  import com.model.Login;
  import com.model.UserDetails;
  public class LoginAction implements SessionAware{
      private Login login;
      private LoginManager loginManager;
      private UserDetails userDetails;
      Map&lt;String,Object&gt; map;
      public LoginAction()
      {
          loginManager=new LoginManager();
      }
      public String loginLink()
      {
          return "loginClicked";
      }
      public String checkLogin()
      {
         try
         {
          //String loggedInUsername=null;
          System.out.println("---------"+login.getUsername());
  
          /*if(map.containsKey("username"))
          {
              loggedInUsername=(String)map.get("username");
          }*/
          userDetails=loginManager.check(login);
          /*if(loggedInUsername!=null &amp;&amp; loggedInUsername==userDetails.getUsername())
          {
              return "loginSuccess";
          }*/
          if(userDetails!=null &amp;&amp; userDetails.getUsername()!=null)
          {
              map.put("login",true);
              map.put("username",userDetails.getUsername());
              map.put("name",userDetails.getName());
              map.put("sex",userDetails.getSex());
              map.put("email",userDetails.getEmail());
              map.put("phoneno",userDetails.getPhone_no());
              System.out.println("Inside session map creation that is Successful login");
              return "loginSuccess"; 
          }
          else
          {
              System.out.println("Inside check login with invalid credentials");
              return "loginClicked";
          }
         }catch(Exception ex)
         {
             System.out.println("Inside exception of checkLogin.");
             return "loginClicked";
         }
      }
      public void setLogin(Login login)
      {
          this.login=login;
      }
      public Login getLogin()
      {
          return login;
      }
      @Override
      public void setSession(Map&lt;String, Object&gt; map) {
          this.map=map;
      }
  }
  </code></pre>
  
  <p>loginManager class: This class handles the database part. Login credentials are checked in this class.</p>
  
  <pre><code>package com.controller;
  import org.hibernate.Query;
  import org.hibernate.classic.Session;
  //import java.util.List;
  import com.model.Login;
  import com.model.UserDetails;
  import com.util.HibernateUtil;
  public class LoginManager extends HibernateUtil{
  
          UserDetails userDetails;
          public UserDetails check(Login login)
          {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            try
            {
              System.out.println("Inside try block to retrieve from db");
              String hql="FROM UserDetails where username='"+login.getUsername()+"' and password='"+login.getPassword()+"' and role='U'";
              Query query = session.createQuery(hql);
              System.out.println("Query Created");
              userDetails=(UserDetails)query.uniqueResult();
              //System.out.println("Returned Username"+userDetails.getUsername());
              //System.out.println("Returned Password"+userDetails.getPassword());
              session.getTransaction().commit();
            }catch(Exception ex){
                System.out.println("Exception generated is "+ex.getMessage());
                session.getTransaction().rollback();
                userDetails=null;
                ex.printStackTrace();
            }
            return userDetails;
          }
  }
  </code></pre>
  
  <p>Below is my struts.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
  
  &lt;struts&gt;
      &lt;constant name="struts.devMode" value="true" /&gt;
      &lt;package name="package2" extends="struts-default"&gt;
  
          &lt;interceptors&gt;
           &lt;interceptor name="myinterceptor" class="interceptors.LoginInterceptor" /&gt;
           &lt;interceptor-stack name="myinterceptorSt"&gt;
              &lt;interceptor-ref name="myinterceptor" /&gt;
              &lt;interceptor-ref name="defaultStack" /&gt;
          &lt;/interceptor-stack&gt;
          &lt;/interceptors&gt;
  
  
  
          &lt;action name="registerLink" class="com.view.RegisterAction" method="registerLink"&gt;
              &lt;result name="registerLinkClicked"&gt;/registerPage1.jsp&lt;/result&gt;
          &lt;/action&gt;
  
          &lt;action name="register" class="com.view.RegisterAction" method="addUser"&gt;
              &lt;result name="registered"&gt;/registrationSuccess.jsp&lt;/result&gt;
          &lt;/action&gt;
  
          &lt;action name="login" class="com.view.LoginAction" method="checkLogin"&gt;
              &lt;interceptor-ref name="myinterceptorSt" /&gt; 
              &lt;result name="loginSuccess" type="redirect"&gt;/profile.jsp&lt;/result&gt;
              &lt;result name="loginFail"&gt;/loginFail.jsp&lt;/result&gt;
              &lt;result name="loginClicked"&gt;/loginPage.jsp&lt;/result&gt;
          &lt;/action&gt;
  
          &lt;action name="sessionCheck" class="com.view.SessionCheckAction"&gt;
              &lt;result name="sessionCheckSuccess"&gt;&lt;/result&gt;
          &lt;/action&gt;
  
          &lt;action name="loginLink" class="com.view.LoginAction" method="loginLink"&gt;
              &lt;result name="loginClicked"&gt;/loginPage.jsp&lt;/result&gt;
          &lt;/action&gt;
  
      &lt;/package&gt;
  &lt;/struts&gt;
  </code></pre>
  
  <p>Can anyone please help me with the interceptor code that should be written in order to implement the functionality specified by me above.</p>