aspose file tools
The moose likes Servlets and the fly likes How to manage sessions in servlet ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "How to manage sessions in servlet ?" Watch "How to manage sessions in servlet ?" New topic
Author

How to manage sessions in servlet ?

Siva Ram
Ranch Hand

Joined: Apr 04, 2002
Posts: 66
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..........


Thanks and Regards,<br />Siva Ram .NR
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
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

Joined: Apr 04, 2002
Posts: 66
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

Joined: Feb 27, 2002
Posts: 8876

Where are u setting the attribute ,BTW??
WHY DONT U put a debug statement in performTask.


Groovy
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
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

Joined: Apr 04, 2002
Posts: 66
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

Joined: Nov 06, 2000
Posts: 1506
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Paulo Lima
Greenhorn

Joined: Apr 04, 2002
Posts: 10
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..........
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to manage sessions in servlet ?
 
Similar Threads
Unavailable session in Controller Servlet.....
httpsession shared across requests getting Null always
httpsession problem
getting session null instead of creating new session.
session managment