aspose file tools
The moose likes Spring and the fly likes org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Reply locked New topic
Author

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

Shwetha Jagadish
Greenhorn

Joined: Mar 28, 2011
Posts: 11
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

Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Please post code


===Vyas Sanzgiri===
My Blog
Shwetha Jagadish
Greenhorn

Joined: Mar 28, 2011
Posts: 11

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();


}
..........
..........
}
Miku Ranjan
Ranch Hand

Joined: Oct 11, 2011
Posts: 98
Hi,
Most probably you are trying to retrieving user from session while it is not there please debug this thing properly.
Shwetha Jagadish
Greenhorn

Joined: Mar 28, 2011
Posts: 11
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

Joined: Mar 28, 2011
Posts: 11

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.
Ashish Agre
Ranch Hand

Joined: Jan 22, 2011
Posts: 73

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.


| B.E IT | SCJP 6.0 98 % |
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17225
    
    1

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1282

This thread was resurrected from Nov 2011. I am not sure the OP is still following


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found
 
Similar Threads
Problem with LazyInitializationException
com.sun.mail.smtp.SMTPSendFailedException: 451 4.7.1 Please try again later
Many to many self join using hibernate annotation
NullPointerException
IllegalArgumentException: Cannot convert value of type [java.lang.String] to required