This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Session management - cookies / URL rewriting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session management - cookies / URL rewriting" Watch "Session management - cookies / URL rewriting" New topic
Author

Session management - cookies / URL rewriting

Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90

Hi,

Project/application: myWebApp
IDE: netbeans
Server: apache tomcat
browser: firefox

I'm in the session management section of my j2ee courseware and the following code relates to using cookies / URL rewriting for the same. I'm facing the following issues:

a) Unable to view jessionid (& its value) on the message body of the web page
On running the web application the first time, the html page http://localhost:8084/myWebApp/index.html is loaded with
the hyperlinks as given in the below html code. On clicking AddCookies, the cookies - c1 and c2 - in the servlet file "AddCookies"
are deployed with the appropriate message.

So, on subsequent request (i.e clicking the link Home on the page [url]http://localhost:8084
/myWebApp/AddCookies[/url] to go back to the index.html page and then clicking the link ReadCookies), the page
http://localhost:8084/myWebApp/ReadCookies should display, on the body, the jessionid cookie & its value (sent
by server to browser in response to the first request) apart from the servlet ones. However, the jsessionid cookie is not
displayed.


b) append jessionid (& its value) to the url (http://localhost:8084/myWebApp/ReadCookies)
I disabled cookies in the browser, closed the above page and ran the application. From the page [url]http://localhost:8084
/myWebApp/[/url], i again clicked on ReadCookies to go the ReadCookies page. Here, the page gives out the following error



which is fine as i've disabled cookies in the browser (& the servlet cookies message would not be displayed). But the jsessionid & its value - being system generated - should be appended as parameter to the url mentioned in point b). This does not happen.

Could any of the forum experts explain the above.

thanks,
Sudhir

html code



servlets code



xml code

Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Sudhir,

Seems like you are not creating a session.
Change your welcome page from Html to jsp. this will do the trick for you.

Regards,
Hemant

Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90

Hi Hemant,

Thank you for your response........did as suggested. Created a new 'index.jsp' file (under Web Pages>WEB-INF>index.jsp) and changed index.html to jsp in web.xml. However, on running the application, the browser loads & displays the page with a 404 error. What am i doing wrong?

regards,
Sudhir
P.S: how does changing html to jsp help create the cookie session [as the two .java servlet files are used to create and retrieve/display the same]. Please explain.

Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
hi Sudhir,

Place your welcome page above WEB-INF directory. I think it should solve your 404 error.


P.S: how does changing html to jsp help create the cookie session [as the two .java servlet files are used to create and retrieve/display the same]. Please explain.


Actually jsp page itself get transform to servlet when you run your web-app where it checks for the presence of existing session, if not creates it. thus it solve your "create the cookie session" problem.

Regards,
Hemant
Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90


Place your welcome page above WEB-INF directory. I think it should solve your 404 error.


Thank you Hemant! Has solved the first part of my original post.

However, it still does not append the jession id and its value as parameter to the url http://localhost:8084/myWebApp/ReadCookies?ReadURL=Submit+Query. The page still throws up the NullPointerException - point b) of my original post.

The server log shows


Specifically, it highlights the ReadCookies servlet with the code in bold



The code, in AddCookies servlet, for appending parameter to the url is


The steps i took was:
a) disable the cookies in the browser and closed it
b) run the application
c) From the page http://localhost:8084/myWebApp/, i clicked on the AddCookies link and on that page
d) clicked submit button to land on the http://localhost:8084/myWebApp/ReadCookies?ReadURL=Submit+Query page.....as you can see jsessionid is not appended to the url

Hope you can provide some input to this issue also. Awaiting your reply.

thanks and regards,
Sudhir
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Sudhir,

Are you able to see jsessionId on your welcome page URL.
or are you redirecting to some other page from your welcome page.

anyway the point is since you have disabled cookies, you manually have to encode URL to each form and href.
this wont work.

.

as in this case, container does not find any session id attached to it and in your application you have not created session, you are getting this error when trying to access cookie.
best to use JSTL <c:url> tag that automatically take care of URL encoding for you.


Hope this Helps.
-Hemant






Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

My friend,

There is no created unless you make a session object available in your servlet.

modify your code to contain

HttpSession session = request.getSession();
in your ADDCOOKIES servlet in the doGet() method and it will work! and your output will be

url will be


http://localhost:8084/forum/ReadCookies;jsessionid=43D452CD2DF674132EAB7B7B33DB851E?ReadURL=Submit+Query

and
output

Cookie data from request
Name:dbnameValueracle
Name:dbversionValue:10g
Name:JSESSIONIDValue:43D452CD2DF674132EAB7B7B33DB851E
Home
Sudhir Srinivasan
Ranch Hand

Joined: Jun 08, 2011
Posts: 90
Aashima Arora wrote:My friend,

There is no created unless you make a session object available in your servlet.

modify your code to contain

HttpSession session = request.getSession();
in your ADDCOOKIES servlet in the doGet() method and it will work! and your output will be

url will be


http://localhost:8084/forum/ReadCookies;jsessionid=43D452CD2DF674132EAB7B7B33DB851E?ReadURL=Submit+Query


Hi Aashima,

Thank you for your response. Yes, the ADDCOOKIES servlet must contain the session object for the web container to issue a unique session id(jsessionid) in its response to the request from READCOOKIES servlet.


My url now shows

http://localhost:8084/myWebApp/ReadCookies;jsessionid=1A5BF90E1C2288538DCC8C2A894BC89E?encodeURL=Submit


I do have a question about the jsessionid being appended(shown with a semi-colon preceding it). Shouldn't all parameters(& their values), whether they are manually appended or system generated(like the jessionid), be appended after the ? in the url and not before.

regards,
Sudhir




Aashima Arora
Ranch Hand

Joined: Jun 15, 2010
Posts: 59

jsessionID is not a parameter like other request parameters that are appended after the ?.
it is issued by the container and there is a container specific way of displaying it. the tomcat container uses (;) to append the jsessionID in the url , other containers might use other ways.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session management - cookies / URL rewriting
 
Similar Threads
HTTP status 404 error on page
How to delete a cookie from Request (age old question never answered)
Java ee Servlet does not work - error 404
cookie setDomain problem.
whilst fooling around with SessionId and cookies...