Duffey Kevin wrote:
> Hello,
>
> I am wondering, using JSP and JavaBeans with HttpSession tracking, if I
> want to support URL Rewriting, do I just do something like so on every link
> in every JSP page:
>
> <a href="<%= response.encodeURL("/somepath/somefile.jsp") %>">click here</a>
>
Yes, this is all you need to do. Don't forget to do the same thing in your form
elements as well:
<form action="<%= response.encodeURL("/somepath/somefile.jsp") %>">
...
</form>
If you are using response.sendRedirect(), you need a variation on this same theme:
<%
response.sendRedirect(response.encodeRedirectURL("/somepath/somefile.jsp"));
%>
>
> Is that all that is needed? Does the servlet engine (Servlet 2.1 and 2.2,
> JSP 1.0 and 1.1) automatically append the session id to every link for me,
> and on the server side, it automatically gets the right session?
>
The servlet engine will append the session id (suitably encoded) if it discovers
that the user's browser does not support cookies (or has them turned off). If it
knows that cookies are enabled, it will just return the URL unchanged, but the
cookie stuff will take care of passing the id back and forth.
>
> Is there ANY work I need to do on the server side, in my code, to GET the
> session, or pass it back to the response?
>
Nope -- in JSP pages the system gives you the default user variable "session" which
contains a reference to this session; in a servlet you would execute:
HttpSession session = request.getSession(true);
to create the session the first time, or refer to its properties on subsequent
requests.
One nit-picky quibble -- the session itself is not passed back and forth to the
browser; only the session identifier is. The data stored in the session (either by
the system or by you) is *not* passed back and forth. The servlet engine sees that
session identifier come in on a subsequent request (by URL or cookie doesn't
matter, although you can find out which was used if you care) and looks up that
session for you, when you want it.
>
> I would like to support both cookies and URL Rewriting, and I just want to
> know what lies ahead for me to do so. If its just a matter of adding the
> response.encodeURL() to every link, that is small potatoes compared to
> doing some tedious work on the server code.
>
That's all you've got to do. Just be sure you don't forget a link -- this can
cause mysterious problems because the next request creates a new session instead of
accessing an old one.
>
> Thanks.
>
> Kevin Duffey
> Software Engineer
> [EMAIL PROTECTED]
>
Craig McClanahan
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html