Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi All,

I am trying to run my spring mvc application getting below error,

org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found in session
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException(AnnotationMethodHandlerAdapter.java:550)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:365)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:210)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:132)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
com.sams.membership.mcats.web.util.SessionFilter.doFilter(SessionFilter.java:50)


Please let me know how to resolve this.

Thanks in advance,
Shwetha

 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please post code
 
Shwetha Jagadish
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

My jsp file login.jasp has below detail for form tag,
==================================


<form:form modelAttribute="user" id="loginForm" method="POST" action="logon.htm">
......
</form:form>

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Login Action Class
===============


@Controller
@RequestMapping("/logon.htm")
@SessionAttributes({ "clubNumberList", "user", "club" })
public class LoginFormController {

......
......
@RequestMapping(method = RequestMethod.POST)
public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("user") User userParam, ModelMap modelMap) throws ServletException, Exception {

User user = setUserInfo(request, (User) userParam);
final HttpSession session = request.getSession(true);

UserAccessStatus accessStatus = loginHelper.getAuthorization(user);
if (accessStatus.isAuthorized()) {
List<String> storeNumberList = loginHelper.getClubNumberList();
session.setAttribute("clubNumberList", storeNumberList);
session.setAttribute("user", userParam);
session.setAttribute("club",loginHelper.getClub(Integer.parseInt(user.getClubNumber())));
DefaultViewBean pageBean = new DefaultViewBean();
pageBean.setUser(user);
//loginRedirectView = new ModelAndView("main");
loginRedirectView = new ModelAndView("main", "command", pageBean);
return loginRedirectView;
} else {
status = accessStatus.getErrorMessage();
user.setCtHeaderMessage(status);
user.setClubNumber(null);
session.setAttribute("AuthenticationFailMsg", user.getCtHeaderMessage());
return loginRedirectView = new ModelAndView("logon");
}
}

..........
..............
}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Once I login, i will get a home page where i type member info and click 'GO' submit button. When I click GO button, am getting the errors which i mentioned in my last post.

homepage.jsp
=============

<form:form method="POST" commandName="command" action="lookupCashBack.html">
......
......
<button type="submit" class="Active-button-small">
GO
</button>
.....
.....
</form:form>

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

home page action class CashBackController
=============================


@Controller
@SessionAttributes({ "user" })
public class CashBackController {

..........
..........
@RequestMapping("/lookupCashBack.htm")
public ModelAndView lookupCashBack(HttpServletRequest request, HttpServletResponse response,@ModelAttribute("command") DefaultViewBean defaultViewBean, BindingResult result){
ModelAndView mv = new ModelAndView();
HttpSession session = request.getSession();


}
..........
..........
}
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,
Most probably you are trying to retrieving user from session while it is not there please debug this thing properly.
 
Shwetha Jagadish
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The code absolutely works fine if i don't configure SessionFilter to my application but if i put SessionFilter Code and after session gets expired and then if i do some action(POST request), am getting exception
org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found in session.

Below is my code for SessionFilter

web.xml
=========


<filter>
<description>Session Filter</description>
<display-name>SessionFilter</display-name>
<filter-name>SessionFilter</filter-name>
<filter-class>com.web.util.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<servlet-name>member</servlet-name>
</filter-mapping>
<session-config>
<session-timeout>5</session-timeout>
</session-config>

SessionFilter.java
=============


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* This class works for Session Time out. If the session is timed-out and if there is some action,
* the flow will navigate to login page.
*/
public class SessionFilter implements Filter {


/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;

//String path = req.getServletPath();
HttpSession session = req.getSession(false);

if(session!=null){
long l_oCurrentTime = System.currentTimeMillis()/1000;
long l_olastSessionAccessedTime = session.getLastAccessedTime()/1000;
int inactiveInterval= (int) (l_oCurrentTime - l_olastSessionAccessedTime);
//if(session.getMaxInactiveInterval()<inactiveInterval){
if(inactiveInterval>session.getMaxInactiveInterval()){
RequestDispatcher rd= request.getRequestDispatcher("logon.htm");
rd.forward(request, response);
}else{
filterChain.doFilter(request,response);
}
}else{
RequestDispatcher rd= request.getRequestDispatcher("logon.htm");
rd.forward(request, response);
//filterChain.doFilter(request,response);
}
}

/* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig config) throws ServletException {

}

/* (non-Javadoc)
* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy() {
// TODO Auto-generated method stub

}

}
 
Shwetha Jagadish
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

After putting below entry in web.xml, the problem has been resolved. It will be great pleasure if some one suggest me the below solution is correct or not.

<error-page>
        <exception-type>org.springframework.web.HttpSessionRequiredException</exception-type>
        <location>/index.jsp</location>
    </error-page>


Thanks in advance,
Shwetha.
 
Ranch Hand
Posts: 73
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This might solves the problem but it isn't a proper way as you are hiding the error and then redirecting to your main page. However this error will still be present in the web server console.
To solve this follow these steps.
1. Before going to the server you must have this has parameter @ModelAttribute ("User")User user to the Controller Class's Method
and this Declaration
@SessionAttributes("User") above Controller class

means it will check User named attribute in the session.

I'm new to Spring and i am doing session management in this way. Please rectify me if i am wrong and provide the solution.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Whenever posting code. PLEASE use the CODE tags so that we can read the code. There is a button labeled Code. Just click it and it will add Code tags, then you can past your code in between those tags.

Thanks

Mark
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This thread was resurrected from Nov 2011. I am not sure the OP is still following
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic