Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Session Tracking using HttpSession  RSS feed

 
Roof Age
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to temporarily store objects so that it is available for use by other jsp/servlet pages.
PAGE4.JSP
<%@ page session="true" %>
<%
String s = "yes";
session.setAttribute("isLogged", s);
%>
<a href="page10.jsp">end</a>
PAGE10.JSP
state=
<%
String s = (String)session.getAttribute("isLogged");
%>
<%= s %>
I am not able to pass around the variables around in the session. PAGE10.JSP displays nothing(null) instead of "yes". Do you have any ideas?
Thanks,
Roof.
 
Andrew Shafer
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Add <%@ page session="true" %> at the top of PAGE10.JSP
 
Roof Age
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It didn't help. It is really frustrating when things that books say work doesn't work for you, especially when it is simple :-(
Roof.
 
Cynthia Yao
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what web server are u using? Did u enable session? Also check your browser, does it allow cookie, because that is how the session is maintained
 
Maky Chopra
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this..
PAGE4
session.putValue("isLogged", s);
PAGE10.JSP
String s = (String)session.getValue("isLogged");

[This message has been edited by Mak Bhandari (edited May 08, 2001).]
 
Andrew Shafer
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cynthia is correct, sessions are tracked by setting cookies.
This is the default behavior.
Roof, try this:
In PAGE4.JSP change

to

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on it right now. I am using WebLogic 6.0 SP1 and IE6.0 Beta.
The response.encodeURL seems to work. But why should I have to do this all the time. Is this the only way??
I will post any new findings as and when I discover.
Thanks a bunch.
Roop.
Originally posted by Cynthia Yao:
what web server are u using? Did u enable session? Also check your browser, does it allow cookie, because that is how the session is maintained

 
Sakthivel Palanivelu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know that session tracking uses cookies by default to associate a session identifier with a
unique user. If the browser does not support cookies, or if cookies are disabled, you can still
enable session tracking using URL rewriting.
URL rewriting essentially includes the session ID within the link itself as a name/value pair.
However, for this to be effective, you need to append the session ID for each and every link that
is part of your servlet response.
Adding the session ID to a link is greatly simplified by means of of a couple of methods:
response.encodeURL() associates a session ID with a given URL, and if you are using redirection,
response.encodeRedirectURL() can be used by giving the redirected URL as input.
Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by
the browser; if so, the input URL is returned unchanged since the session ID will be persisted as
a cookie.
Consider the following example, in which two JSP files, say hello1.jsp and hello2.jsp, interact with
each other. Basically, we create a new session within hello1.jsp and place an object within this
session. The user can then traverse to hello2.jsp by clicking on the link present within the
page.Within hello2.jsp, we simply extract the object that was earlier placed in the session and
display its contents. Notice that we invoke the encodeURL() within hello1.jsp on the link used to
invoke hello2.jsp; if cookies are disabled, the session ID is automatically appended to the URL,
allowing hello2.jsp to still retrieve the session object.
Try this example first with cookies enabled. Then disable cookie support, restart the brower, and
try again. Each time you should see the maintenance of the session across pages.
Do note that to get this example to work with cookies disabled at the browser, your JSP engine
has to support URL rewriting.
hello1.jsp
<%@ page session="true" %>
<%
Integer num = new Integer(100);
session.putValue("num",num);
String url =response.encodeURL("hello2.jsp");
%>
<a href='<%=url%>'>hello2.jsp</a>
hello2.jsp
<%@ page session="true" %>
<%
Integer i= (Integer )session.getValue("num");
out.println("Num value in session is "+i.intValue());
%>
 
Roof Age
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, thanks to all of you who are in this thread. Second, I have some more related queries at the end. In relation to the topic, here is what weblogic6.0 has to say:
"How does WebLogic Server know which session is associated with each client? When an HttpSession is created in a servlet, it is associated with a unique ID. The browser must provide this session ID with its request in order for the server to find the session data again. The server attempts to store this ID by setting a cookie on the client. Once the cookie is set, each time the browser sends a request to the server it includes the cookie containing the ID. The server automatically parses the cookie and supplies the session data when your servlet calls the getSession() method.
If the client does not accept cookies, the only alternative is to encode the ID into the URL links in the pages sent back to the client. For this reason, you should always use the encodeURL() method when you include URLs in your servlet response. WebLogic Server knows whether the browser accepts cookies and does not unnecessarily encode URLs. WebLogic automatically parses the session ID from an encoded URL and retrieves the correct session data when you call the getSession() method. Using the encodeURL() method ensures no disruption to your servlet code, regardless of the procedure used to track sessions.
You can add any Java descendant of Object as a session attribute and associate it with a name. However, if you are using session persistence, your attribute value objects must implement java.io.Serializable"
How about fine-grained java beans? Are they persistent over sessions by default. If not, how do we make them session persistent?
Thanks,
Roof
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roof Age:
How about fine-grained java beans? Are they persistent over sessions by default. If not, how do we make them session persistent?

The servlet container doesn't give a hoot whether your class is a JavaBean, an ordinary Java class, fine-grained or coarse-grained. The only requirement is that the class should be serializable (as in java.io.Serializable); this is especially important in clustered servers with session fail-over, and for servers which will serialize session objects to temporary storage if they are running out of memory.
The scope and lifetime of your object is determined by just one thing: the scope and lifetime of the object you bind it in. If you bind it as a ServletContext attribute, it has application-wide visibility and stays around indefinitely. If you bind it as an attribute in the HttpSession object, it is only visible within that single user session, and lives until the session expires. Bind it as a request attribute (not to be confused with a request parameter, which comes from the URL query string) and its life ends as soon as you have finished processing that request. Don't bind it anywhere and you'll lose it as soon as the variable you stored the object reference in goes out of scope.
If you're a JSP user, this corresponds to the "application", "session", "request" and "page" scopes in <jsp:useBean/>, respectively.
In case you're wondering what the difference between "request" and "page" scope is in practice: nothing, unless you use forwarding or (dynamic) inclusion to delegate some or all of the processing to other servlets or JSPs. In the case of "page" scope, your object won't be accessible to those other servlets, but with "request" scope it will.
- Peter

[This message has been edited by Peter den Haan (edited May 10, 2001).]
 
Roof Age
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent exposition Peter!
I really wish I could answer people's queries just the way you did! I was just wondering how one can become a moderator?
To everybody:
One more thing regarding session tracking using encodeURL is that if I use:
<a href="<% response.encodeURL("page10.jsp") %>">page10.jsp</a>
then the relative url images in page10.jsp don't showup!!
Regards,
Roof.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!