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;