Solved

Get new JSP, but maintain session

Posted on 2003-10-27
11
280 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 80
Java JRE greater than 1.6 5 62
Custom Java Application connects to database: Which one of the 20? 4 27
arguments to jar 5 26
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

776 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