Sample project to replicate spring session issue
Running project: This project can be run on ebmedded jetty server by invocing run-jetty.bat Below are app urls http://localhost:8083/addhttp://localhost:8083/viewhttp://localhost:8083/error
Add session attribute and forward to url which will result in 404. On 404 error servlet gets called but not have access to session created in Add call.
Add servlet code:
publicclassSessionAttributeServletextendsHttpServlet {
publicstaticfinalStringSESSION_ATTRIBUTE = "SESSION_ATTRIBUTE";
protectedvoiddoGet(HttpServletRequestreq, HttpServletResponseresp)
throwsServletException, IOException
{
req.getSession().setAttribute(SESSION_ATTRIBUTE,"Session argument");
RequestDispatcherrequestDispatcher = req.getRequestDispatcher("/dispatched");
requestDispatcher.forward(req,resp);
}
}View servlet code:
publicclassViewSessionAttributeextendsHttpServlet {
protectedvoiddoGet(HttpServletRequestreq, HttpServletResponseresp)
throwsServletException, IOException
{
HttpSessionhttpSession = req.getSession(false);
Stringvalue = null;
if (httpSession!=null){
value = httpSession.getAttribute(SessionAttributeServlet.SESSION_ATTRIBUTE)+":: class ::"+httpSession.getClass()
+":: "+httpSession.getId();
}
resp.getWriter().println("Session details -> "+value);
}
}Error servlet code:
publicclassErrorServletextendsHttpServlet {
protectedvoiddoGet(HttpServletRequestreq, HttpServletResponseresp)
throwsServletException, IOException
{
HttpSessionhttpSession = req.getSession(false);
Stringvalue = null;
if (httpSession!=null){
value = httpSession.getAttribute(SessionAttributeServlet.SESSION_ATTRIBUTE)+":: class ::"+httpSession.getClass()
+":: "+httpSession.getId();
}
resp.getWriter().println("Session details in error page -> "+value);
}
}Session details can be found by calling view or error directly.
Demo