Solved

JSP Sessions & URL Rewriting

Posted on 2001-07-19
15
2,101 Views
Last Modified: 2013-11-24
I am running Tomcat on top of Apache. An HTTP request for a URL that has 'CSP' is passed by Apache to Tomcat. This works fine for http://localhost/CSP/Test.jsp. Now I want to add sessions to my JSP pages. As an experiment, I disabled cookies on my browser, and I have
Test.jsp rewritten as follows:

<html>
<body>
<a href="<%= response.encodeURL("/CSP/Test.jsp") %>">Test</a>
</body>
</html>

Because cookies are disabled, URL rewriting is used, and the anchor becomes:
http://localhost/CSP/Test.jsp;jsessionid=2ygm1jiad1

According to the JSP specifications, the session information is encoded and assigned to 'jsessionid', and this is appended to the URL following a semi-colon. However, when I click on the link above, Apache returned 404 Not Found. It seems that the semi-colon and extra string at the end of the URL has made it an invalid URL. Apache did not even pass the request to Tomcat even though the URL contains 'CSP'.

On this EE website, I noticed that with URL rewriting, the 'jsessionid' is appended following a question mark, not a semi-colon. How do I make my JSP page work?
0
Comment
Question by:yongsing
  • 10
  • 5
15 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
> this is appended to the URL following a semi-colon

DO you have a (online) reference where you read this?
That certainly looks like an invalid url to me, should be a ?.
0
 
LVL 9

Author Comment

by:yongsing
Comment Utility
Actually, I read that in the Java Servlet Specification v2.2, section 7.1.1 (page 35):

===================================================================
7.1.1 URL Rewriting
URL rewriting is the lowest common denominator of session tracking. In cases where a client will not accept a cookie, URL rewriting may be used by the server to establish session tracking. URL rewriting involves adding data to the URL path that can be interpreted by the container on the next request to associate the request with a session. The session id must be encoded as a path parameter in the resulting URL string. The name of the parameter must be jsessionid. Here is an example of a URL containing encoded path information:

http://www.myserver.com/catalog/index.html;jsessionid=1234

===================================================================

You can obtain the specification at:
http://java.sun.com/products/servlet/download.html#specs

I tried this http://localhost/index.html;jsessionid=1234 , but Apache returned 404 Not Found.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Thanks for that, will have a read.

So what's writing the ';' ?

Have you tried with '?' ?
0
 
LVL 9

Author Comment

by:yongsing
Comment Utility
I can't control the ';'. The following code:

response.encodeURL("/CSP/Test.jsp");

will translate into the absolute URL:

http://localhost/CSP/Test.jsp;jsessionid=2ygm1jiad1

The semi-colon is added by encodeURL(). I don't know why it doesn't add '?' instead.


Perhaps the developers of this EE site can answer my question, since they use Apache, Tomcat and JSP.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
The ; is used in the meta tag, not in the href tag.
Will see what else I can find.

What version of Tomcat are you using?


0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
Comment Utility
You need to modify your apache configuration:

http://www.jguru.com/faq/view.jsp?EID=53878
0
 
LVL 9

Author Comment

by:yongsing
Comment Utility
I am using Apache 1.3.14, and Tomcat 3.2.1. I added the following to the Apache configuration file, httpd.conf:

<IfModule mod_rewrite.c>
  RewriteEngine     on
  # Force URLs with a jsessionid to go to Tomcat. Necessary because
  # Apache doesn't recognise that the semi-colon is special.
  RewriteRule       ^(/.*;jsessionid=.*)$   $1 [T=jserv-servlet]
</IfModule>

After rebooting the system, it still doesn't work. I am not sure if there is any error in the above lines.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 92

Expert Comment

by:objects
Comment Utility
Is it getting to Tomcat yet?
Are you using jserv?

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Try asking on the Apache topic.
The problem definitely seems to be Apache not understanding the ;, so it doesn't pass it on to tomcat.
The Apache experts probably know all about this one.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Actually I'll ask on your behalf, I've got some spare points :)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 9

Author Comment

by:yongsing
Comment Utility
Thanks for the help, objects.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
No worries :)
0
 
LVL 9

Author Comment

by:yongsing
Comment Utility
I also need to uncomment the below line in the httpd.conf file, in order to use the functionality of a module.

LoadModule rewrite_module modules/ApacheModuleRewrite.dll

objects, thanks for your help. BTW, you can delete away the question in the Apache area, to reclaim back your question points.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Thanks for the points :)
Good to hear u got it sorted.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

10 Experts available now in Live!

Get 1:1 Help Now