Using getOutputStream with a servlet redirect?

D

DiscoStu

Hello,

I want to pass information from my servlet into one of my JSP
pages but the Session object doesen't seem to be working for my
application. I was wondering, can I get the output stream of my
HttpServletResponse object and attach some data to it before I call
sendRedirect("NewPage.jsp");

I want NewPage.jsp to getInputStream on its HttpServletRequest object
and retrieve the information the servlet attached. Is this feisable? I
would normally just put the object in my HttpSession but it keeps
getting reset because I have to web-servers talking to each other and
not a web-server and a browser.

Thanks
 
J

John C. Bollinger

DiscoStu said:
Hello,

I want to pass information from my servlet into one of my JSP
pages but the Session object doesen't seem to be working for my
application. I was wondering, can I get the output stream of my
HttpServletResponse object and attach some data to it before I call
sendRedirect("NewPage.jsp");

No. That is to say, you can write data to the output stream, and as
long as you're buffering and the data are not enough to cause the buffer
to be flushed then you'll be able to send the redirect. HOWEVER,
whatever you have written to that output stream will have dropped into a
black hole by the time the new request arrives.
I want NewPage.jsp to getInputStream on its HttpServletRequest object
and retrieve the information the servlet attached. Is this feisable? I

No. See above.
would normally just put the object in my HttpSession but it keeps
getting reset because I have to web-servers talking to each other and
not a web-server and a browser.

You are getting a new session for each request because the requests do
not contain the session cookie or any other form of session ID. You
might be able to fix it by enabling and using URL rewriting to pass the
session ID around.


John Bollinger
(e-mail address removed)
 
J

Java Architect

To the OP:

Rather than trying a workaround, why not figure out what's wrong with your
session handling. Also, if you are trying to get info to the JSP, you should
use the request object instead of the session.

Hope this helps
 
S

Sudsy

would normally just put the object in my HttpSession but it keeps
You are getting a new session for each request because the requests do
not contain the session cookie or any other form of session ID. You
might be able to fix it by enabling and using URL rewriting to pass the
session ID around.

John,
I resisted replying earlier. What I believe the OP needs to do is
map out, on paper or a white board, precisely how the protocol works.
If one doesn't understand cookies and URL-rewriting as a mechanism
to maintain session state then obviously they won't be able to
arrive at a suitable architecture.
Only once you comprehend the nature of the underlying protocol,
and particularly the roles of client and server, can you map your
requirements appropriately.
If you search the list archives you'll rapidly locate a jakarta
package which can emulate a browser in application-to-application
scenarios. Trying to use examples as boilerplate in place of actual
empirical testing won't save you time in the longer term.

Translation:
It's doable, but you're going to have to do a bit of digging into
the documentation and get your hands dirty.
'Tis always the best way!
 
D

DiscoStu

Translation:
It's doable, but you're going to have to do a bit of digging into
the documentation and get your hands dirty.
'Tis always the best way!

More simply: RTFM.
 
J

John C. Bollinger

Sudsy said:
I resisted replying earlier. What I believe the OP needs to do is
map out, on paper or a white board, precisely how the protocol works.
If one doesn't understand cookies and URL-rewriting as a mechanism
to maintain session state then obviously they won't be able to
arrive at a suitable architecture.

Yes, you're quite right. Perhaps I should have resisted a little harder
myself.
Only once you comprehend the nature of the underlying protocol,
and particularly the roles of client and server, can you map your
requirements appropriately.

I can't argue with that.


John Bollinger
(e-mail address removed)
 
D

DiscoStu

Translation:
It's doable, but you're going to have to do a bit of digging into
the documentation and get your hands dirty.
'Tis always the best way!

So I took your advice and looks carefully through the servlet spec. I
never knew the Request object had get/setAttribute() methods, I only
thought it had get/setParameter() methods which take strings. I needed
objects however. That worked out well, but left me stumped on how to
pass the Request object with the attributes in it to my
SearchResults.jsp page. Until I found the RequestDispatcher class,
thats a handy little class but it seems oddly packaged, along with
SingleThreadModel. Both are tiny classes just kind of hanging out
there.

Thanks everyone
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top