Using getOutputStream with a servlet redirect?

Discussion in 'Java' started by DiscoStu, Jul 8, 2004.

  1. DiscoStu

    DiscoStu Guest

    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
    DiscoStu, Jul 8, 2004
    #1
    1. Advertising

  2. DiscoStu wrote:

    > 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
    John C. Bollinger, Jul 8, 2004
    #2
    1. Advertising

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

    "John C. Bollinger" <> wrote in message
    news:cckitl$b95$...
    > DiscoStu wrote:
    >
    > > 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
    >
    Java Architect, Jul 9, 2004
    #3
  4. DiscoStu

    Sudsy Guest

    >> 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,
    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!
    Sudsy, Jul 9, 2004
    #4
  5. DiscoStu

    DiscoStu Guest

    > 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.
    DiscoStu, Jul 9, 2004
    #5
  6. Sudsy wrote:

    > 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
    John C. Bollinger, Jul 9, 2004
    #6
  7. DiscoStu

    DiscoStu Guest

    > 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
    DiscoStu, Jul 9, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. b83503104
    Replies:
    1
    Views:
    28,256
    John C. Bollinger
    Dec 1, 2003
  2. circuit_breaker
    Replies:
    2
    Views:
    1,983
    Jack Jia
    Apr 4, 2004
  3. Replies:
    4
    Views:
    8,536
    John Currier
    Jun 29, 2005
  4. John Smith
    Replies:
    2
    Views:
    4,042
    Andrea Desole
    Jun 16, 2006
  5. javadev
    Replies:
    5
    Views:
    12,862
    javadev
    Nov 16, 2006
Loading...

Share This Page