hi I have a java client application which accesses a servlet by establishing a connection using URLConnection class. Now each time the doPost() of the servlet is called by the client, the servlet creates a new session. I want the servlet to reuse the same session it created the first time it was accessed. How can I make this happen? I tried fetching the session id, attached it to the url to enable url rewriting method of session tracking. But it is still not working. Please do advise. Thanks in advance Seema
Sun Certified Java Programmer<br />Sun Certified Web Component Developer<br />Sun Certified Business Component Developer
If your client wants to use URL-rewriting, then you have to make sure the server application supports it properly too, and many don't. I suggest that a more robust solution is to use one of the many "web client" libraries which manage all the tedious stuff like cookies, headers, authentication and redirection for you. My current favourite is HTTPUnit. It's described as a web site test system, but it contains some powerful and easy to use classes for just this sort of thing. If's free, and it can be found at http://httpunit.sourceforge.net/ .
Your client is basically going to have to fake the operation of a browser with respect to recognizing and using cookie data in the http headers. If the client is creating the connection with a URL that starts "http://" then you are getting a HttpURLConnection so you have all of the HttpURLConnection and URLConnection methods to play with. Bill