This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes URLConnection && session state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "URLConnection && session state" Watch "URLConnection && session state" New topic
Author

URLConnection && session state

sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
I am using URLConnection to connect to a servlet.

With any browser it keeps the HTTPSession.
I wonder how do browsers keep session?
once when i was working with cookies i saw a cookie named SESSION_ID.
I think they do it like that,with a cookie and id.

I want to know, how do they retrived in server ?Where are they kept?

Can i reach any Session object by that cookie ID?

and how can i keep state with URLConnection?

Can i keep state by sending the same Session ID ,as Cookie to server?

Thx.
Daniel Rhoades
Ranch Hand

Joined: Jun 30, 2004
Posts: 186
You actually don't need to know how HTTPSession keeps state in order to use it.

*BASICLY* When a new session is created by a servlet (or any other J2EE component) a reference is passed back to the client (in the form of a cookie or session id in the URL - which then gets sent back to the server (by the browser) on subsequent requests).

So you get a session like this:

HttpSession session = request.getSession();

Add stuff to a session like this:

HttpSession session = request.getSession();
session.setAttribute("something", "some text or an object");

Get stuff out of a session like this using a cast:

HttpSession session = request.getSession();
String text = (String) session.getAttribute("something");


Drinking more tea is the key...
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67

*BASICLY* When a new session is created by a servlet (or any other J2EE component) a reference is passed back to the client (in the form of a cookie or session id in the URL - which then gets sent back to the server (by the browser) on subsequent requests).



Thx for answer.
Alright i know how to use session object.

I want to know how can i keep session state with URLConnection from an applet to servlet?

Its not like browser.so once we connected to server it will open a session for the connection.But what will happen when second connection.URLConnection doesnt send session infos to servlet like browsers do.

Any other advice?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

URLConnection has a getHeaderField(String name) method that can be used to get headers. In the cookies header look for JSESSIONID.
If you send that back as a request header in all subsequent requests, you will be able to maintain your session.

In this case the context name was "hrp".

From the server:
Set-Cookie: JSESSIONID=BDD9BDF5A38DCB97E6911919CDBDC362; Path=/hrp


From the Browser:
Cookie: JSESSIONID=BDD9BDF5A38DCB97E6911919CDBDC362



A great tool for watching all of this is the LiveHttpHeaders plugin for Firefox:
http://livehttpheaders.mozdev.org/

[Addition]
You use the setRequestProperty to set the header when making a request:
con.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
[ August 19, 2005: Message edited by: Ben Souther ]

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

Joined: Jun 30, 2004
Posts: 186
Ah I didn't know of the URLConnection class...

Ben's methods looks like it would work for maintaining state on the server.

Do you need to access the Session object in the applet? How about using a stateful SessionBean - at least you can get the actual object back via JNDI
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
I want to use the session id of the page which applet is loaded.

In the cookies header look for JSESSIONID.
If you send that back as a request header in all subsequent requests, you will be able to maintain your session.


I think to do my aim,i need to print JSESSIONID in applet's parameters.
And in servlet i will get the cookie including JSESSIONID and i will print to applet paramater field of client.

I will try this.

Stateful session bean,you mean ejb?Good idea! thanks for that too. it will remain somewhere in my mind for later.
But now We're using Tomcat for application server.
You know,classical boss orders
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLConnection && session state
 
Similar Threads
Global Variable Vs Cookie
web-inf vs meta-inf
Why session destroyed when browser closed
Use of Cookie
URLConnection and Java HttpSession's