• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

how to create a Http session in JSP page ?

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The session is created on your behalf by the container. You should most certainly not instantiate your own.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic