• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problems in URL rewriting

 
Manish Shinde
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if cookies are disabled.
then will it append jsession id with url on second time ?

Regards,
Manish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic