Solved

Get new JSP, but maintain session

Posted on 2003-10-27
11
276 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

706 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

19 Experts available now in Live!

Get 1:1 Help Now