Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to manage sessions in servlet ?  RSS feed

 
Siva Ram
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am getting the current session in my servlet using request.getsession(). Then I am passing the request object to another helper class in which I am getting some session object for my computation.
While doing so, I am getting the null as the session object value in the helper class. But if I am using request.getsession(false) in the servlet, I am getting the correct session object in the helper class also.
But as per API if we are using request.getsession() and request.getsession(false) will be the same.In this case why I am getting the session object values to null in the helper class which is using the same request object sent from the servlet ?
Can any one please help me to solve this..........
 
Shashi Kanta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post your code on the forum. i need to know how u r passing the request object and at the same time if you ccreating a session object again in the second servlet.
 
Siva Ram
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My servlet is as follows.
public class Myservlet extents javax.servlet.http.HttpServlet{
/**
* Process incoming HTTP GET requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
}
/**
* Process incoming HTTP POST requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
performTask(request, response);
}
/**
* Process incoming requests for information
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void performTask(HttpServletRequest request, HttpServletResponse response) {
try
{
javax.servlet.http.HttpSession ss = null;

ss = request.getSession();

String ssloginName = (String)ss.getAttribute("sesLoginName");
MyHelper m = new MyHelper();
m.meth(request,response);
}
}
this is the servlet code .
The helper class code is as follows.
public class MyHelper{
public meth(javax.servlet.http.HttpRequest req, javax.servlet.http.HttpResponse res){
javax.servlet.http.HttpSession ss = null;

ss = request.getSession(false);

String ssloginName = (String)ss.getAttribute("sesLoginName");
MW_Debug.println("Login Name : "+ssloginName);
}
}
In the above helper class the session object loginname is null if the above code exist. If I am placing request.getsession(false) in the servlet , I am getting the correct result.
.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are u setting the attribute ,BTW??
WHY DONT U put a debug statement in performTask.
 
Shashi Kanta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Siva,
as pradeep pointed out, I too don't know if u had set the attribute in a previous servlet.
now assuming that, u have set the attribute in some other servlet, i compiled the same code u had posted (though after modifying spelling mistakes and using the correct request object in the helper class )
and it printed out the value of the "sesLoginName"
in both the cases ie when i use getSession() or getSession(false) in the Myservlet class.
i used System.out instead for printing out the value.
now, i have to tell u that, both the methods ie getSession() and getSession(false) will work the same way if a session object has already been created!!
however, if u r creating a session object for the first time, using getSession(false) won't create a new session object, so u cannot store the attributes!!
rgds,
Shashi
 
Siva Ram
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shasi & Pradeep,
I set the loginName in session while I login to my application in the attribute "sesLoginName". In my application a static method MW_Debug is used instead of using System.out controlled by means of a flag.
In my login servlet I am creating a session explicitly by specifying request.getsession(true).
Even then the session is creating the above mentioned problem.
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva, getSession() and getSession(false) are not the same. getSession() AND getSession(true) return the current session or create one if there is none, while getSession(false) does not create noen if there is none, it returns null.
 
Paulo Lima
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Siva !
Why don't you try use: request.getsession(true);
It's will create the session object at first time user acess the server. Than you can pass the session object instead of the request.
I hope that this can help you.
Regards,
Paulo Lima.
Originally posted by Siva Ram:
Hi,
I am getting the current session in my servlet using request.getsession(). Then I am passing the request object to another helper class in which I am getting some session object for my computation.
While doing so, I am getting the null as the session object value in the helper class. But if I am using request.getsession(false) in the servlet, I am getting the correct session object in the helper class also.
But as per API if we are using request.getsession() and request.getsession(false) will be the same.In this case why I am getting the session object values to null in the helper class which is using the same request object sent from the servlet ?
Can any one please help me to solve this..........
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!