Servlet URL Rewriting in Session Tracking

In this approach, the token is embedded in each URL. In each dynamically generated page, the server embeded an extra query paramete, or extra path information, in each URL int he page. When the client submits requests using such URLs, the token is retransmitted to the server.
With URL rewriting, you append a token or identifier to the URL of the next servlet or the next resource. You can send parameter name/value pairs using the following format: url?name1=value1&name2=value2&…
A name and a value is separated using an equal sign (=); a parameter name/value pair is separated from another parameter name/value pair using the ampersand (&). When the user clicks the hyperlink, the parameter name/value pairs will be passed to the server. From a servlet, you can use the HttpServletRequest interface's getParameter method to obtain a parameter value. For instance, to obtain the value of the second parameter, you write the following:
request.getParameter(username);
The use of URL rewriting is easy. When using this technique, however, you need to consider several things:
  • The number of characters that can be passed in a URL is limited. Typically, a browser can pass up to 2,000 characters.
  • The value that you pass can be seen in the URL. Sometimes this is not desirable. For example, some people prefer their password not to appear on the URL.
  • You need to encode certain characters—such as & and ? characters and white spaces— that you append to a URL.
Note :-
  • It doesn’t work well if a servlet has to use the extra path  information as true path information, however.
  • Using an added parameter works on all servers too, but it fails as a target for forms that use the POST method, and it can cause parameter naming collisions.
  • Using a custom, server-specific change works under all conditions for servers that support the change. Unfortunately, it doesn’t work at all for servers that don’t support the change.
Example of URL Rewriting
index.html
<html> <head> <title>URL Rewritting Demo in Servlet </title> </head> <body> <form action="FirstServlet" method="post"> User Name <input type="text" name="userName"> &nbsp; <input type="submit" value="Submit"> <br> </form> </body> </html>
FirstServlet.java
import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String uname=request.getParameter("userName"); out.print("Welcome "+uname); //Creating query String and set the value out.print("<a href=SecondServlet?uname="+uname+">"+ "Login </a> "); out.close(); }catch(Exception e){System.out.println(e);} } }
SecondServlet.java
import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SecondServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String uname=request.getParameter("uname"); out.print("User Name is : "+uname); out.close(); } catch(Exception e){ System.out.println(e); } } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app > <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>SecondServlet</servlet-name> <servlet-class>SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SecondServlet</servlet-name> <url-pattern>/SecondServlet</url-pattern> </servlet-mapping> </web-app>
Output
Download this example
1) Deployed using Tomcat 6.0
2) Deployed using eclipse IDE
 



 

Latest Tutorial


Most Visited Tutorial