"Grewal, Gary" wrote:

>
>
> in forwarding using RequestDispatcher, try String url =
> encodeRedirectURL(url); and send this url as your forward url for
> session tracking. May be you need to do a similiar thing on the JSP
> side too while trnsferring the control back.
>
> ===
> Gary Grewal
> Contractor NEC America
> Phone: 214-262-5911
> E-Mail: [EMAIL PROTECTED]
>
> -----Original Message-----
> From: SEEMANTINI GODBOLE [mailto:[EMAIL PROTECTED]]
> Sent: Monday, March 19, 2001 10:23 AM
> To: '[EMAIL PROTECTED]'
> Subject: problems with forward in a servlet
>
> Hi,
>
> I am running into a problem when using "forward" method in my servlet
> to
> forward the HttpRequest to a JSP page.
>
> The servlet looks like this
>
> public void doGet(HttpServletRequest request, HttpServletResponse
> response)
>       throws IOException, ServletException
> {
>
>     HttpSession session = request.getSession(true);
>     System.out.println("ID " + session.getId());
>
>     //do bunch of business logic here
>     //do more things
>     .
>     .
>     .
>
>
> etServletConfig().getServletContext().getRequestDispatcher(theJSPPage).forw
>
> ard(request,
> response);
>
> }
>
> everything works except the forward part. Every time a JSP is
> displayed
> as a result of forward method, the user is supposed to fill in stuff
> in
> the page and get back to the same servlet after he submits on the
> page.
>
> However, when the servlet takes over the second time, it has to create
> a
> new session, and the SessionID is changed.
> The expected behavior was that the session would have been maintained
> though the control was passed from servlet->JSp->back to servlet.
>
> If I comment the forward code statement and replace it by some code
> such
> as
>
>         PrintWriter out = response.getWriter();
>         out.println("<html><head><title>Alternative
> methods</title></head>");
>         out.println("<body>");
>
> and give a href to bring back the users to the same servlet, in that
> case the session is retained.
>
> Any ideas? Anyone? Anyone?
>
> Thanks,
> Seemantini Godbole



Hi :-)  from a email in Servlet-List, I also suggest  try both the
following:
- HttpServletResponse.encodeURL()
- HttpServletResponse.encodeRedirectURL()

the following is that email:
...
I am assuming that both the servlets belong to same Context.
If your browser does not support cookies, try the method
HttpServletResponse.encodeURL() for the output URL.
...
Regds,
Gokul



the following is from Servlet API doc:

encodeURL
public java.lang.String encodeURL(java.lang.String url)
Encodes the specified URL by including the session ID in it, or, if
encoding is not needed, returns the URL unchanged. The implementation of
this method includes the logic to determine whether the session ID needs
to be encoded in the URL. For example, if the browser supports cookies,
or session tracking is turned off, URL encoding is unnecessary.
For robust session tracking, all URLs emitted by a servlet should be run
through this method. Otherwise, URL rewriting cannot be used with
browsers which do not support cookies.
--------------------------------------------------------------------------------

encodeRedirectURL
public java.lang.String encodeRedirectURL(java.lang.String url)
Encodes the specified URL for use in the sendRedirect method or, if
encoding is not needed, returns the URL unchanged. The implementation of
this method includes the logic to determine whether the session ID needs
to be encoded in the URL. Because the rules for making this
determination can differ from those used to decide whether to encode a
normal link, this method is seperate from the encodeURL method.
All URLs sent to the HttpServletResponse.sendRedirect method should be
run through this method. Otherwise, URL rewriting cannot be used with
browsers which do not support cookies.




Bo
Mar.19, 2001


Reply via email to