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 Web Component Certification (SCWCD/OCPJWCD) and the fly likes question on encodeURL() 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "question on encodeURL()" Watch "question on encodeURL()" New topic
Author

question on encodeURL()

Jingh Yi
Ranch Hand

Joined: Mar 23, 2005
Posts: 90
My view.jsp:

My servlet that invokes this view.jsp:

my TestJSP.jsp:


The sequence of invocation of these files are:
servlet invokes view.jsp, then view.jsp shows a link that invokes TestJSP.jsp.

My question:

Since I've blocked the cookies from my browser, I see TestJSP.jsp is invoked with jsessionid in the URL. but how come the view.jsp does not show jsessionid in URL???

Thanks!
Jenny
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
Hi,

My belief is that because your servlet code does not create a session, ie. req.getSession() no session is created in the servlet. Thus the url for the jsp does not have a session id included.

By default jsp's have a session enabled, unless you tell them otherwise, there is a way to do that. So if you have not turned the session off in the jsp the jsp will create one for you by default. Thus the url for test.jsp includes the session id.

if you change your code as per below then the variable test will be false and the url for the view.jsp file will include a session id as a session has now been created.



HTH

Mat
okkadu privt
Ranch Hand

Joined: Feb 07, 2005
Posts: 30
Hi Mat,

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, java.io.IOException { HttpSession session = req.getSession(); String url1 = res.encodeRedirectURL("http://localhost:8080/ELTest/view.jsp"); //Line 14boolean test = "http://localhost:8080/ELTest/view.jsp". equals(url1); //Line 15 System.out.println(test);System.out.println(url1); res.sendRedirect(url1); }


executing with this code, jsessionid is shown in url. But it is not showing when user clicks on Test Link & goes to TestJSP.jsp page.

What is the problem ?
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
Hi,

I am not sure why your code is not working. Can you post what you have, as Jenny did, and I (or another member ) will have a look at it and hopefully someone can point you in the right direction.

Mat
Jingh Yi
Ranch Hand

Joined: Mar 23, 2005
Posts: 90
Mat,

I did you said, adding 'HttpSession session = req.getSession();' in servlet code before it redirects to view.jsp. However, I got exactly what Privt got, view.jsp appears with the jsessionid, but testJSP.jsp doesn't have jsessionid. Why?

Thanks! You've been very helpful!
Jenny
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
Hi all,

Sorry for the late reply.

Here is the code I have used and it works for me on tomcat 5.0.28 on Windows XP and 2K

web.xml


TestServletOne.java


TestServletTwo.java


when I use the url http://localhost:8080/RanchTest/TestServletOne ->
I get redirected to view.jsp with NO jsessionid identified in the url bar

when I use the url http://localhost:8080/RanchTest/TestServletTwo ->
I get redirected to view.jsp with a jsessionid identified in the url bar

NOTE that to do this, and this may be the key thing for both of you, I had to turn off cookies -> I seem to remember that someone had something special they had to do in IE there was a post last week sometime about it. Once cookies are turned off the only way the server has to maintain a session id is put it in the url and therefore you see it.

HTH, sorry it is such a long post.

Matthew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on encodeURL()
 
Similar Threads
Starting when tld files don't need to register in web.xml?
About Session
How to get header?
question on tag files:
Why is the object not bound to session?