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