Solved

Get new JSP, but maintain session

Posted on 2003-10-27
11
277 Views
Last Modified: 2006-11-17
Hi,

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(
            url.openStream()));

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.

0
Comment
Question by:kerzner
  • 5
  • 4
  • 2
11 Comments
 
LVL 18

Expert Comment

by:bobbit31
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 (...) {
//...
}
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 9630594
at least in my app it doesn't
0
 
LVL 1

Author Comment

by:kerzner
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.
0
 

Expert Comment

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

Accepted Solution

by:
bobbit31 earned 250 total points
ID: 9630948
well i do it from an applet and i use this constructor for my URL:

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" :
          <scheme>://<authority><path>?<query>#<fragment>
 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.
Parameters:
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:kerzner
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.
0
 
LVL 1

Author Comment

by:kerzner
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.

0
 
LVL 18

Expert Comment

by:bobbit31
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 ;)

bob
0
 
LVL 1

Author Comment

by:kerzner
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,
Mark
0
 

Assisted Solution

by:dldnh
dldnh earned 250 total points
ID: 9636016
it goes a little something like this...

URL url = new URL ( urlStr );
URLConnection conn = url.openConnection();
conn.setDoInput(true);
String pageUrl = req.getParameter( PAGEURL );
String cookieStr;
sessionId = getSession();
if ( sessionId != null ) {
  cookieStr = "JSESSIONID=" + sessionId + "; " + cookieStr;
  conn.setRequestProperty( "Cookie", cookieStr );
}
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader in = new BufferedReader( new InputStreamReader(is) );
0
 
LVL 1

Author Comment

by:kerzner
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);
            }
then
in = new BufferedReader(new InputStreamReader(con.getInputStream()));

Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now