[英]Set session value in JSP and retrieve in java class using response.sendRedirect
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. 在JSP页面中成功设置了Session中的值,但无法在Java类中检索。 Please help to resolve: 请协助解决:
Below is code snippet : 下面是代码片段:
Interceptor entry in servlet-context.xml file : servlet-context.xml文件中的拦截器条目:
<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 在jsp页面中设置会话值: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 : 尝试获取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;
There is a chance of session getting lost when you use redirect. 使用重定向时,会话可能会丢失。 If you use redirect URL, ensure 2 things. 如果您使用重定向URL,请确保两件事。
The redirect URL should be the same as the URL used to load the current page. 重定向URL应与用于加载当前页面的URL相同。
Also, use the redirect code like this - 另外,使用这样的重定向代码-
response.sendRedirect(response.encodeRedirectURL(Your URL));
Check the Javadocs for more info. 查看Javadocs以获得更多信息。 https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletResponse.html#encodeRedirectURL(java.lang.String) 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 在此处看到类似的问题- 会话丢失,并且在每个Servlet请求中都作为新会话创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.