Web browser in java

A

Arne Vajhøj

It depends.

If the object of the exercise is to understand the mechanics of cookie
handing then 'doing it yourself' is a great learning exercise.

Given that the page used as starting point does
not mention the word cookies and the code does
not use any network code it all (it just use
the setPage method with an URL), then that is not
very likely.

Arne
 
A

Arne Vajhøj

Firstly it is not the browser that maintains a session it is the server.

It needs info from the client to do so.
When you make a request for a web page your browser looks in the place
where it stores cookies (if you are writing a web browser this can be
anywhere you like, a browser usually writes cookies to disk) and adds
any cookies it finds that match the URL you are requesting to the
request. Only then will it send the request

So, you need a way of searching your cookie store, finding any cookies
that 'belong' to the URL you are requesting and adding the resulting
cookies to the request before you send it.

Cookies with session id should never be stored on disk.
When your request hits the server, you access the cookies by extracting
them from the request, actually if you are using a Java servlet
container like Tomcat you don't need to bother with explicitly managing
session cookies, If a session exists (in other words if a valid session
cookie was sent by the browser) Tomcat will retrieve the session using
the contents of the cookie to 'lookup' the session in it's session
store. If a session doesn't exist or if it has timed out, Tomcat will
make a new one, create a cookie with a lookup key in it (amongst other
things) and add the cookie to the response.

Depends. JSP pages do so. Servlets do not.
If you are making a request to a server that understands how to use
cookies to maintain state then all you have to do is extract cookies
from a response to your original request, store them somewhere safe, and
add them to your next request, the server will take care of the rest.

The simplified sequence of events goes something like this

1. Make a request to foo.bar.com

Before you actually send the request but after you know the target

2. Search your browser cookie store for any cookies that belong to
foo.bar.com, if you find them, add them to the request.

3. send the request

4. When the server replies, parse the response and extract any cookies
written by the server, one of these is probably your session key. Save
them to your cookie store.

5. make a new request to foo.bar.com adding the session cookie to the
request.

bingo! you have a session

You can skip #2 (unless you need other info than
session that are stored in cookies).

But I would let #5 add all cookies retrieved in #4,
because they are most likely all needed (if not then
there were no reason to set them).

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top