Get new JSP, but maintain session


I need to get a JSP output in the servlet using InputStreamReader, as follows

String url = "http://localhost:7001/myjsp.jsp";
URL url = new URL(urlStr);
            BufferedReader in = new BufferedReader(
            new InputStreamReader(

But this looses the session info in the servlet where I am.

What I really need  is to open the connection but keep the session. (If you wonder why I need all this and do not simple use "forward", this is because I am writing a compression servlet, but can not use filters).

Thank you.

Who is Participating?
bobbit31Connect With a Mentor Commented:
well i do it from an applet and i use this constructor for my URL:

public URL(URL context,
           String spec)
    throws MalformedURLExceptionCreates a URL by parsing the given spec within a specified context. The new URL is created from the given context URL and the spec argument as described in RFC2396 "Uniform Resource Identifiers : Generic Syntax" :
 The reference is parsed into the scheme, authority, path, query and fragment parts. If the path component is empty and the scheme, authority, and query components are undefined, then the new URL is a reference to the current document. Otherwise the any fragment and query parts present in the spec are used in the new URL. If the scheme component is defined in the given spec and does not match the scheme of the context, then the new URL is created as an absolute URL based on the spec alone. Otherwise the scheme component is inherited from the context URL. If the authority component is present in the spec then the spec is treated as absolute and the spec authority and path will replace the context authority and path. If the authority component is absent in the spec then the authority of the new URL will be inherited from the context. If the spec's path component begins with a slash character "/" then the path is treated as absolute and the spec path replaces the context path. Otherwise the path is treated as a relative path and is appended to the context path. The path is canonicalized through the removal of directory changes made by occurences of ".." and ".". For a more detailed description of URL parsing, refer to RFC2396.
context - the context in which to parse the specification.
spec - the String to parse as a URL.

and so yes, if you use your ServletContext as your first parameter using the constructor above, it should work the same as mine.
i don't think this would destroy your session:
BufferedReader in = null

try { ...
   URL myURL = new URL (urlStr);
   URLConnection con = myURL.openConnection();

   in      = new BufferedReader(new InputStreamReader(con.getInputStream()));

   String s;

   // read in result
   while ((s = in.readLine()) != null) {
      strReturn += s;
} catch (...) {
at least in my app it doesn't
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

kerznerAuthor Commented:
What is your urlStr? Is it the complete string like in mine, or relative.

Also, when I say session, I mean that this urlStr, which is a jsp, should have the the servlet's session? Is that your situation?

Thank you.
what about pushing the jsessionid cookie into the jsp request
kerznerAuthor Commented:
what about pushing the jsessionid cookie into the jsp request ?

This might work. First of all, does this transfer all of the session objects? I happen to jave a whole bunch. Secondly, how do you do this?

Thank you.
kerznerAuthor Commented:
to bobbit31

This sounds very reasonable, and thank for providing such great detail. I need to checks this, if it works for me, tomorrow morning, and then I know if I can mark the answer as accepted.

Thank you again.

if you pass the session id... it might work but also could cause problems if the session no longer exists... try my proposal in which you pass the context when you create the URL object... it should work.

if it works for me, it should work for you shouldn't it ;)

kerznerAuthor Commented:
Still not working. The session is not inherited in the new connection. You say,

"if you use your ServletContext as your first parameter using the constructor above"

but the constructor wants a URL, not a ServletContext

Thank you,
dldnhConnect With a Mentor Commented:
it goes a little something like this...

URL url = new URL ( urlStr );
URLConnection conn = url.openConnection();
String pageUrl = req.getParameter( PAGEURL );
String cookieStr;
sessionId = getSession();
if ( sessionId != null ) {
  cookieStr = "JSESSIONID=" + sessionId + "; " + cookieStr;
  conn.setRequestProperty( "Cookie", cookieStr );
InputStream is = conn.getInputStream();
BufferedReader in = new BufferedReader( new InputStreamReader(is) );
kerznerAuthor Commented:
I hope I successfully split the 500 between the two answers from bobbit31 (250) and dldnh (250), if not, please fix that

It did not quite work by simply opening a connection (bobbit31), but the idea of getting the connection and setting request parameters before opening it was useful.

I had to set the cookies by setting the headers. I copied all headers from the servlet request to the new request I was opening. This has effectively trasferred the session also, which was hiding as WebLogSession - not like dldnh suggested, but close.

This is the real code
            url = new URL(urlContext, urlStr);
            URLConnection con = url.openConnection();
            Enumeration enum = req.getHeaderNames();
            while (enum.hasMoreElements()) {
                String name = (String) enum.nextElement();
                String value = req.getHeader(name);                
                con.setRequestProperty(name, value);
in = new BufferedReader(new InputStreamReader(con.getInputStream()));

Thank you.
All Courses

From novice to tech pro — start learning today.