aspose file tools*
The moose likes JSP and the fly likes how to create a Http session in JSP page ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to create a Http session in JSP page ?" Watch "how to create a Http session in JSP page ?" New topic
Author

how to create a Http session in JSP page ?

Frank Sikuluzu
Ranch Hand

Joined: Dec 16, 2003
Posts: 116
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

Joined: Jan 10, 2002
Posts: 61589
    
  67

The session is created on your behalf by the container. You should most certainly not instantiate your own.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.



Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Frank Sikuluzu
Ranch Hand

Joined: Dec 16, 2003
Posts: 116
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 16, 2003
Posts: 116
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

Joined: Jan 10, 2002
Posts: 61589
    
  67

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

Joined: Dec 16, 2003
Posts: 116
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

Joined: Dec 11, 2004
Posts: 13410

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
 
subject: how to create a Http session in JSP page ?