Unable to get session.getAttribute in RequestMethod.POST - java

I have tow method and using Spring MVC, first is a method=RequestMethod.GET and there I set session.setAttribute("clientId", "abc").
Second method is a method=RequestMethod.POST where I do this:
HttpSession session = request.getSession();
System.out.println("-----" + (String)session.getAttribute("clientId"));
But always get null.
[Edit]
The thing here is the post method is not called by ModalandView("postpage"), its called by Http callout by Apache oltu internally

In Get method do
Session session = request.getSession(true);
session.setAttribute("clientId", "abc");
In Post do
String s = request.getSession().getAttribute("clientId");

Related

GET Method Is Not Calling In IE

I am new to AngularJs & Spring. I am calling Spring MVC GET Method from AngularJs function. Sometime GET method is not called up and giving old session values. If i use POST its working fine.
Please comment if need more details about it.
Spring MVC method :
#RequestMapping(value="/getAccessDetails", method=RequestMethod.GET)
public #ResponseBody ProcessDO getAccessDetFromSession(HttpServletRequest request){
AccessDO accessDO = null;
HttpSession session=request.getSession();
if(session.getAttribute("accessDetail")!=null) {
accessDO =(AccessDO) session.getAttribute("accessDetail");
}
return accessDO ;
}
AngularJS Function :
$scope.loadDetails = function(){
$http.get(CONTEXT+'/getAccessDetails').then(function(resp){
alert(resp.data); // Getting old value
});
};
Targets of caching operations
I think this explains your issue.
This is happening because your response is getting cached and when you are trying again you are getting cached response in case of GET.
While post method doesn't get cached neither it get saved in browser history.
you can also referhttp_methods_get_post_difference this link

HttpSession as a parameter in the controller

I am trying to understand how to sending HttpSession as a parameter in the spring controller works.
I have a jsp which does a post request on clicking the submit button. In the controller, reading the sessions as follows
In the controller:
public ModelAndView viewEditFundClass(HttpServletRequest request,HttpServletResponse response,Model model){
HttpSession session = (HttpSession)request.getSession();
java.util.Date startDate = sesseion.getAttribute("startDate");
However, when I just change the controller to the following, I am still able to access the session
public ModelAndView viewEditFundClass(HttpServletRequest request,HttpServletResponse response, HttpSession session,Model model)
I would like to know how this is done in Spring, ie how did the post request pass the HttpSession as a parameter? will this session be valid?
Assuming you're using Spring 3+ #Controller and #RequestMapping handler methods, Spring defines a default set of supported argument types for your handlers
Session object (Servlet API): of type HttpSession. An argument of
this type enforces the presence of a corresponding session. As a
consequence, such an argument is never null.
Spring uses the strategy pattern to accomplish this, using the interface HandlerMethodArgumentResolver. It checks the parameter types of your handler methods and, for each type, tries to find a HandlerMethodArgumentResolver that will be able to resolve an argument for it.
For HttpSession, that implementation is ServletRequestMethodArgumentResolver.

In JSF HttpServletRequest object getAttribute() returning Null

I have Two different JSF page Let Us suppose A.jsf and B.jsf But Both calling same managed bean different methods ManagedBean.java
A.jsf is calling a SessionScoped Managed bean method where i set some attribute in Request class object
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
request.setAttribute("token", requestToken.getToken());
request.setAttribute("tokenSecret", requestToken.getTokenSecret());
Then redirecting some other side like this
response.sendRedirect(requestToken.getAuthorizationURL());
Now after successful login i am opening another JSF page of my website suppose b.jsf and from this page i am calling method like this
<f:event listener="#{ManagedBean.redirectLogin2}" type="preRenderView" />
and calling same Managedbean but another method
public String redirectLogin2() throws TwitterException {
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
}
But when i am doing in above method redirectLogin2()
request.getAttribute("token")
request.getAttribute("tokenSecret")
Both giving Null. What cab be the issue here?
Request scoped attribute life span will be lost on sendRedirect. You should set value on session scope.
HttpSession session=request.getSession();
session.setAttribute("token", requestToken.getToken());
session.setAttribute("tokenSecret", requestToken.getTokenSecret());
After setting value to session. You can access that from request like
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
request.getSession().getAttribute("token");
request.getSession().getAttribute("tokenSecret");
Although, above code will work but that is not good practice. JSF has #SessionScoped annotation which will make available of your variable access with login session.

accessing object from previous ModelAndView

From the reference link: How do you redirect to another URI and access an object from the previous modelAndView, if the public ModelAndView sendEmail() {} had been modified and say, other objects like Collection or HashMap would have added in the Model Object, then how would you have accessed in the /nextPageclass method ?
If possible I would just stick it into the HttpSession object:
public ModelAndView something(HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("name", "object");
return new ModelAndView(new RedirectView("nextPageclass"));
}
This should make the values available during the entire user's sessions rather than just on the individual page request. You will end up having to manage the state of Session attributes but this provides a way to make the data available on the next page or any page thereafter without having to consistent get() and set() the value.

How to get session information in Spring MVC 3

I am new to spring MVC and I am trying to get the session information in my controller class
Right now I am using
HttpSession objHttpSession = request.getSession(true);
if I want to get session creation time and session Id I am using
objHttpSession.getCreationTime();
objHttpSession.getId();
I want to know is there any spring MVC specific way to get the session details?
Thanks in advance
I usually declare a parameter of type HttpSession in my Spring MVC controller method when I need to access session details.
For example:
#RequestMapping("/myrequest")
public void handleMyRequest(HttpSession session, ...) {
...
}
I think it's the simplest way, but don't know if it fits your needs.
You can get the session in Spring MVC like this:
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession session = attr.getRequest().getSession();
The currentRequestAttributes method return the RequestAttributes currently bound to the thread in which there is a current request and from that request you can get the session. This is useful when you need to get hold of the session in non-spring class. Otherwise you can just use:
#RequestMapping(...)
public void myMethod(HttpSession session) {
}
Spring will inject HttpSession instance into your controller's method.

Categories

Resources