Forums Register Login

Session management - cookies / URL rewriting

+Pie Number of slices to send: Send

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

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send

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.

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send


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
+Pie Number of slices to send: Send
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






+Pie Number of slices to send: Send
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:dbnameValue:oracle
Name:dbversionValue:10g
Name:JSESSIONIDValue:43D452CD2DF674132EAB7B7B33DB851E
Home
+Pie Number of slices to send: Send
 

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




+Pie Number of slices to send: Send
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.
this is supposed to be a surprise, but it smells like a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5170 times.
Similar Threads
HTTP status 404 error on page
Unable to get the desired result as Hello UserName
First helloWorld servlet program
where to look for the error...
Problem with simple HTML - Servlet login
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:58:53.