Get new JSP, but maintain session

Posted on 2003-10-27
Last Modified: 2006-11-17

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.

Question by:kerzner
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
LVL 18

Expert Comment

ID: 9630591
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 (...) {
LVL 18

Expert Comment

ID: 9630594
at least in my app it doesn't

Author Comment

ID: 9630815
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.
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.


Expert Comment

ID: 9630936
what about pushing the jsessionid cookie into the jsp request
LVL 18

Accepted Solution

bobbit31 earned 250 total points
ID: 9630948
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.

Author Comment

ID: 9630950
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.

Author Comment

ID: 9630969
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.

LVL 18

Expert Comment

ID: 9631480
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 ;)


Author Comment

ID: 9635895
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,

Assisted Solution

dldnh earned 250 total points
ID: 9636016
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) );

Author Comment

ID: 9638032
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.

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question