jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Session Related Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Related Question" Watch "Session Related Question" New topic
Author

Session Related Question

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Is it possible to do the following:

and then
B Mampilli
Ranch Hand

Joined: Aug 05, 2002
Posts: 61
Hi,
Yes it should work.
HttpSession session = request.getSession(false);This does not invalidate the current session.
request.getSession with the parameter as false returns the currently valid session if any otherwise it returns null.
As, there is a session existing, it will return it and so the values in the session would be available.


Boney
SCJP, SCWCD
vijaykumar shah
Greenhorn

Joined: Jul 02, 2003
Posts: 23
Hello,
Yes it will definately work..... and it should work irrespective of
HttpSession session = request.getSession(true/false).
vijay
Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
You can try this servlet for better understanding.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SessionServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

PrintWriter out = response.getWriter();
response.setContentType("text/html");

HttpSession session = request.getSession();

session.setAttribute("name", "1");

//session.invalidate();

HttpSession session1 = request.getSession(false);
System.out.println("Session1** : " + session1);
HttpSession session2 = request.getSession(true);
System.out.println("Session2** : " + session2);
String name = null;
try

{
name = (String)session1.getAttribute("name");
if (name == null)
{
name = "2";
}
}
catch(NullPointerException nu)
{

if (name == null)
{
name = "3";
}
}

out.println("Name " + name);
}
}

Regards,
M.S.Raman
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Session Related Question
 
Similar Threads
How to create Session
Problem in Using Servlet Context
java.lang.NullPointerException
Session tracking in servlets
change password