0

Am trying to get value from session but fails. A value in Session is successfully set in JSP page but same is unable to retrieve in Java class. Please help to resolve:

Below is code snippet :

Interceptor entry in servlet-context.xml file :

<interceptor>
    <mapping path="/**" />
    <exclude-mapping path="/tabservice/pdfCreation/**"/>
    <beans:bean class="com.mastek.ems.filter.ExecuteTimeInterceptor"></beans:bean>
</interceptor>

Set the session value in jsp page : index.jsp

//intiate session and set value
 request.getSession(true).setAttribute("user", "my_user");
 System.out.println("inside jsp : ssn.getAttrbute : " + request.getSession(false).getAttribute("user"));
 errorPage = false;

 if(!errorPage){    
    String encStr = "encodeValue";
    response.sendRedirect("http://localhost/Test/index.html#detailsPage?en1="+encStr);
 } else {   
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Error.jsp");
    dispatcher.forward(request, response); 
 } 

Try to get the value in Interceptor : ExecuteTimeInterceptor :

System.out.println("Session Object:"+request.getSession(false)); // Prints null
    System.out.println("Session ObjectID1:"+request.getSession(false).getId()); // Throws NullPointerException

    long startTime = System.currentTimeMillis();
    if(request.getSession(false) != null){
        System.out.println("inside if : Ssn Data:"+request.getSession(false).getAttribute("user"));
        if( request.getSession(false).getAttribute("user") != null ){
         request.setAttribute("startTime", startTime);
            System.out.println("Start Time::with Valid Session:1:"+(new SimpleDateFormat("dd/MM/YYYY:hh:mm:ss:SSSS")).format(new Date(System.currentTimeMillis())));
            return true;
        }
     } else{
        System.out.println("Start Time::with Invalid Session::"+(new SimpleDateFormat("dd/MM/YYYY:hh:mm:ss:SSSS")).format(new Date(System.currentTimeMillis())));
    }
    request.setAttribute("startTime", startTime);
    Utility.setResponseError(response, "sessiontimedout");
    return false;
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
AmolKumar
  • 223
  • 1
  • 2
  • 5
  • 1
    Hard to guess, but I assume that a page triggered the interceptor before the session was created. You should display `request.getRequestURL()` in the interceptor when the session is null. – Serge Ballesta Jun 29 '16 at 10:15

1 Answers1

0

There is a chance of session getting lost when you use redirect. If you use redirect URL, ensure 2 things.

The redirect URL should be the same as the URL used to load the current page.

Also, use the redirect code like this -

response.sendRedirect(response.encodeRedirectURL(Your URL));

Check the Javadocs for more info. https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletResponse.html#encodeRedirectURL(java.lang.String)

See a similar issue here - Session is lost and created as new in every servlet request

Community
  • 1
  • 1
Shankar
  • 2,625
  • 3
  • 25
  • 49