Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to create a Http session in JSP page ?

 
Frank Sikuluzu
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I want to create a HttpSession in my index.jsp (the first page of application), should I just do

<% HttpSession session = new HttpSession();
session.setAttribute(..);
%>

But it seems "session" is an "implicit" JSP object. So I am wondering do I still need to instantiate it formally before I add attributes to it ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The session is created on your behalf by the container. You should most certainly not instantiate your own.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further, "session" is one of the implicit object variables provided by the container.
Just as with "request", "response", "application"...., all you need to do to access it is call it by name.

 
Frank Sikuluzu
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please allow to ask a follow-up question. If "session" is an implicit thing server creates for me, then in my JSP1-->Servlet-->Action-->JSP2 process, In the "Action" class the "execute(request, response)" method involves adding some new attirbutes to the session. Now the question are --

1. If I use "session.setAttribute("name", myVar) in the JSP1, then in "Action" class I can NOT use "HttpSession session = new HttpSession();" any more, right ? I can only add attributes like "request.getSession().setAttribute("name2," anotherVar);", is this right ?

2. If I don't do anything with session in JSP1, then in "Action", do I need to write "HttpSession session = new HttpSession();" before I add attributes. It seems Yes, right ? Then I am confused, why ? it seems there is already "session" created by the server in JSP1, so why can't I just directly use it in "Action" ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't ever use new to get a session.

In your JSP use the "session" variable created for youby the container.
In the servlet, use request.getSession().

Always let the container handle the creation of the session.
 
Frank Sikuluzu
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Don't ever use new to get a session.

In your JSP use the "session" variable created for youby the container.
In the servlet, use request.getSession().

Always let the container handle the creation of the session.


But if I do not use any session in JSP1, do I need to use "new" to create one in the servlet or Action ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if I do not use any session in JSP1, do I need to use "new" to create one in the servlet or Action ?


No. Regardless of whether you use a session anywhere, the container manages it.

Never create a session on your own. Never. Ever.
 
Frank Sikuluzu
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


No. Regardless of whether you use a session anywhere, the container manages it.

Never create a session on your own. Never. Ever.


Then, under what circumstance should I use "HttpSession = new HttpSession();" ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Sikuluzu:


Then, under what circumstance should I use "HttpSession = new HttpSession();" ?


When you're building your own servlet container.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic