URL rewriting in servlets ( Servlet session tracking )

URL rewriting is the alternative for cookies in servlet session tracking.
In some situations, the browser device does not accept cookies,in that case the URL rewriting is a solution to maintain session tracking.
URL rewriting involves encoding the session ID into the hyper-links on the Web pages that our servlet sends back to the browser.
When the user subsequently clicks these links, the Server extracts the ID from the URL address and finds the appropriate HttpSession when our servlet calls the getSession() method.
Example for servlet URL rewriting :
Directory Structure in eclipse IDE:
Source code :
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<%@page contentType="text/html" 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>GlassFish JSP Page</title> </head> <body> <form action="URLRewrite" method="POST"> <table> <tr> <td> Enter Your name : </td> <td colspan="2" align="center"><input type="text" name="name" value=""></td> </tr> <tr> <td> Email Address : </td> <td colspan="2" align="center"><input type="text" name="mail" value=""></td> </tr> <tr> <td> Phone Number : </td> <td colspan="2" align="center"><input type="text" name="phone" value=""></td> </tr> <tr> <td> Age : </td> <td colspan="2" align="center"><input type="text" name="age" value=""></td> </tr> <tr> <td> Gender : </td> <td colspan="2" align="center"><input type="text" name="sex" value=""></td> </tr> <tr> <td colspan="2" align=""><input type="submit" value="Submit"></td> </tr> </table> </form> </body> </html> |
URLRewriteServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package urlRewriting; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Date; public class URLRewriteServlet extends HttpServlet { private static final long serialVersionUID = 1L; String Name = ""; String Email = ""; String Telephone = ""; String Age = ""; String Gender = ""; public void doPost(HttpServletRequest request, HttpServletResponse response) { try { // Get form data Name = request.getParameter("name"); Email = request.getParameter("mail"); Telephone = request.getParameter("phone"); Age = request.getParameter("age"); Gender = request.getParameter("sex"); // Set form data to HttpSession HttpSession session = request.getSession(); session.setAttribute("NAME", Name); session.setAttribute("EMAIL", Email); session.setAttribute("TELEPHONE", Telephone); session.setAttribute("AGE", Age); session.setAttribute("GENDER", Gender); //Create dynamic form with URLrewrite technique response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.println("<html>"); writer.println("<body>"); writer.println("<form action="+response.encodeURL("result")+" method=POST>"); writer.println("<table>"); writer.println("<tr><td><h3>Address :</h3></td></tr>"); writer.println("<tr><td>Place:</td><td><input type=text name=address></td></tr>"); writer.println("<tr><td>City :</td><td><input type=text name=city></td></tr>"); writer.println("<tr><td>State :</td><td><input type=text name=state></td></tr>"); writer.println("<tr><td>Country :</td><td><input type=text name=country></td></tr>"); writer.println("<tr><td><input type=submit value=submit></td></tr>"); writer.println("</table>"); writer.println("</form>"); writer.println("</body>"); writer.println("</html>"); } catch(Exception exception) { exception.printStackTrace(); } } } |
ResultServlet.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package urlRewriting; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class ResultServlet extends HttpServlet { private static final long serialVersionUID = 1L; String Name = ""; String Email = ""; String Telephone = ""; String Age = ""; String Gender = ""; String Address = ""; String City = ""; String State = ""; String Country = ""; public void doPost(HttpServletRequest request, HttpServletResponse response) { try { //Get form data from the HttpSession HttpSession session = request.getSession(); Name = (String)session.getAttribute("NAME"); Email = (String)session.getAttribute("EMAIL"); Telephone = (String)session.getAttribute("TELEPHONE"); Age = (String)session.getAttribute("AGE"); Gender = (String)session.getAttribute("GENDER"); // Get data from request Address = request.getParameter("address"); City = request.getParameter("city"); State = request.getParameter("state"); Country = request.getParameter("country"); response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.println("<h3><font color=green>Entered Details :</font></h3>"); writer.println("Name : "+Name+"<br>"); writer.println("Email : "+Email+"<br>"); writer.println("Phone : "+Telephone+"<br>"); writer.println("Age : "+Age+"<br>"); writer.println("Sex : "+Gender+"<br>"); writer.println("Address : "+Address+"<br>"); writer.println("City : "+City+"<br>"); writer.println("State :"+State+"<br>"); writer.println("Country :"+Country+"<br>"); writer.println("<font color=red> Click to go home page</font>"); writer.println("<a href=index.jsp> home </a>"); } catch(Exception exception) { exception.printStackTrace(); } } } |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>Servlet1</servlet-name> <servlet-class>urlRewriting.URLRewriteServlet</servlet-class> </servlet> <servlet> <servlet-name>Servlet2</servlet-name> <servlet-class>urlRewriting.ResultServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet1</servlet-name> <url-pattern>/URLRewrite</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Servlet2</servlet-name> <url-pattern>/result</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
Execution Result :
Enter the initial URL (In our example it is http://localhost:8080/URLrewritingApp/index.jsp) to load home page and enter the detail.
Click the submit button , the below response will come from the server with new URL ( Encoded url with session Id).Enter the details and click submit button.
The final result will come with all entered details.

Related Posts : |
![]() |
AES Password Encryption in javascript and decryption in java – servlet |
![]() |
Kill or invalidate session using session id ( concurrent login ) |
![]() |
Servlet Filter ( Request and Response filters in servlets ) example |
![]() |
Servlet Cookies ( Servlets session tracking with cookie ) |
![]() |
Hidden form field in servlets ( Servlet session tracking ) |
![]() |
Request attributes in servlet with an example ( Attribute scope ) |
![]() |
Session attributes in servlet with example (Attribute scope) |
![]() |
ServletConext Attributes in servlet ( Attribute scope ) |