URL rewriting is another way to support state tracking. With URL rewriting, the parameter that we want to pass back and forth between the ...
URL rewriting is another way to support state tracking. With URL rewriting, the parameter that we want to pass back and forth between the Web browser and client is appended to the URL. URL rewriting is the lowest common denominator of session tracking, and is used when a client does not accept cookies. We modified the CookieServlet to implement the same state tracking mechanism technique, but by using URL rewriting. The URLServlet (See Example) demonstrates this technique.
Example
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class URLServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
int calledCount = 0;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>URLServlet</TITLE><BODY>");
out.println("<H2>Servlet URL Rewriting Example:</H2><HR>");
calledCount = getReqURLInt(req, “calledCount”);
out.println("The value of the url-parm calledCount received in the
request:");
if (calledCount == 0) out.println("null - value not received <HR>");
else out.println(calledCount + "<HR>");
calledCount++;
out.println("The value of the url-parm calledCount sent back: " +
calledCount);
out.print("<HR><P><A HREF=\"itso.servjsp.servletapi.URLServlet");
out.print("?calledCount=" + calledCount +
"\"> Click to reload</A>");
out.println("</BODY><HTML>");
out.close();
}
public int getReqURLInt(HttpServletRequest req, String name) {
int val = 0;
if (req.getParameter(name) != null)
val = new Integer(req.getParameter(name)).intValue();
return val;
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class URLServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
int calledCount = 0;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>URLServlet</TITLE><BODY>");
out.println("<H2>Servlet URL Rewriting Example:</H2><HR>");
calledCount = getReqURLInt(req, “calledCount”);
out.println("The value of the url-parm calledCount received in the
request:");
if (calledCount == 0) out.println("null - value not received <HR>");
else out.println(calledCount + "<HR>");
calledCount++;
out.println("The value of the url-parm calledCount sent back: " +
calledCount);
out.print("<HR><P><A HREF=\"itso.servjsp.servletapi.URLServlet");
out.print("?calledCount=" + calledCount +
"\"> Click to reload</A>");
out.println("</BODY><HTML>");
out.close();
}
public int getReqURLInt(HttpServletRequest req, String name) {
int val = 0;
if (req.getParameter(name) != null)
val = new Integer(req.getParameter(name)).intValue();
return val;
}
}