wood burning stoves 2.0*
The moose likes Servlets and the fly likes problems in URL rewriting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "problems in URL rewriting" Watch "problems in URL rewriting" New topic
Author

problems in URL rewriting

Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
I have some doubts in URL rewriting.
I have made servlet UrlRewrite.java where I m using encodeURL() and then going to
Page Hi.jsp.
When clicked on link, I got Hi.jsp page and jsession id appended to the url.
Then I m clicked on link & went to bye.jsp
But this time I didn’t get jsession id as appended to the url.
But when I did getId() ,I got same jsession id as appended with Hi.jsp

My question is when we go from second to third page in spite of using encodeURL() why didn’t we get jsession id as appended to the URL of the third page.

Here is code.
1) UrlRewrite.java
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UrlRewrite extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
String contextPath = request.getContextPath();
HttpSession hs=request.getSession();

String encodedUrl = response.encodeURL(contextPath+"/Hi.jsp");
System.out.println(encodedUrl);
out.println("<html>");
out.println("<head>");
out.println("<title>URL Rewriter</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>This page will use URL rewriting if necessary</h2>");
out.println("Go to the Hi.jsp page <a href=\"" + encodedUrl+ "\">here</a>.");
//out.println("Go to the Hi.jsp page <a href=\"/Hi.jsp\">here</a>.");
out.println("</body>");
out.println("</html>");
}


}

2)Hi.jsp
<%@page contentType="text/html" session="true" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h2>Hello World!</h2>
</body>
</html>
<%
String encodeURL=response.encodeURL(request.getContextPath()+"/Bye.jsp");
out.println(encodeURL);
%>
<a href ="<%=encodeURL%>">Bye</a>

3)Bye.jsp
<%@page contentType="text/html" session="true" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
HttpSession hs=request.getSession();


if (hs.isNew())
{
out.println("session is new");
}
else
{
out.println("session is not new");
String id=hs.getId();
out.println(id);
}

%>
<h2>Bye World!</h2>
</body>
</html>



Regards,
Manish








xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
Manish - First time you got Jsession id in your URL but second time not. When first request came to your server, at that point server is not aware of whether you have enabled the cookies or not. So first time, it will send cookies as well as Jsession id. This is why you got Jsession id first time. On second time and onward, server is now capable to know, Cookies are enabled and it is not sending Jsession id in URL. but you can get this value from request object.

This is internal implementation of server to handle session.

Hope it is clear to you now.

-Sunil
Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
what if cookies are disabled.
then will it append jsession id with url on second time ?

Regards,
Manish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems in URL rewriting