• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URLConnection && session state

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
 
sinasi susam
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


*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?
 
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
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 ]
 
Daniel Rhoades
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic