This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
There's no need to pass the session from one servlet to the next - sessions are application-wide. So if a session has been created for a user in servlet A, then it will be available in servlet B as well.
Could you give a hint as how to make URL (or rather, HttpURLConnection) work in this case?
I tried URL class and could not make it work.
Joined: Mar 22, 2005
It depends a bit on how sessions are handled in your web app. If you have enabled cookies, then the session ID would be transported in a cookie. In that case you'd grab the cookie from the incoming request, and set its value as the value of a header named "Cookie" in the HttpURLConnection.
Try Apache HttpClient, it has clean API to read cookies from the request and set it again for a new request...
Joined: Sep 20, 2001
It seems to me that the very first time when the servlet is run there is no cookie in the Request. So either HttpURLConnection or Apache HttpClient will not be able to call another servlet and pass the session info to it. So the first time A and B will have different sessions. In subsequent access however A and B will share the same session (B steals a session from A).