state management using url rewriting

In this post, you will learn:

  • What is URL rewriting?
  • How to manage client’s state using URL rewriting?
  • What are the differences between hidden form fields and URL rewriting?

what is URL rewriting?

URL rewriting is another browser independent approach of maintaining client’s state between requests. In this approach, the information which is to be maintained between requests, is dynamically appended as parameters to the URL in the response page. When a request is submitted using these URL, appended information is made available as request parameter. Following syntax is used for URL rewriting: URL?paramName=value&paramName=value&…

Differences between hidden form fields and URL rewriting

1. URL rewriting can be used with input forms as well as hyper links but hidden form fields can only be used when the response page has input form.

2. In case of hidden form fields, input form can be submitted using either get or post request but in case of URL rewriting, request type of input form must be post. When an input form is submitted using get request, browser appends request parameters to the URL which results in the removal of parameters appended by the programmer.

To show the use of URL rewriting with input form as well as hyper link, I have modified the application of the last example. The modified application, has one extra servlet by the name ConfirmServlet. Following diagram describes the page flow of the modified application.

url_rewriting_page_flow

Description of the diagram:

1.0 User sends a request for WelcomeServlet and provides his name as input.

1.1 Response page containing an input form is generated by the WelcomeServlet. To the URL of the input form, user name is appended so that it can be made available to the ConfirmServlet.

2.0 Using the Response page, the user submits a request to the ConfirmServlet. User name appended to the URL is received as parameter in the ConfirmServlet.

2.1 Response page containing a hyper link is generated by the ConfirmServlet. To the URL of the hyper link, user name is appended so that it can be made available to the TourServlet.

3.0 Using the Response page, the user submits a request to the TourServlet. User name appended to the URL is received as parameter in the TourServlet.

3.1 Response page is generated by the TourServlet.

Practical example of state management using cookie:

First, the index.html :

<form method="post" action="welcomeServlet">
Name <input type="text" name="name"> <br/>
<input type="submit" value="submit">
</form>

Second the WelcomeServlet. It appends the user name to the form URL.

package com.techmentro.learningpad;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class WelcomeServlet extends HttpServlet 
{
	public void doPost(HttpServletRequest request, 
			HttpServletResponse response)
			throws ServletException, IOException {
		//Value of the name request parameter is read
		String user=request.getParameter("name");
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		out.println("Welcome, "+user);
		//An input form is created and user name is appended to its url
		//Request type of the form is set to post so that browser don't modify the url
		out.println("<br><form action=\"confirmServlet?userName="+user+"\" method=post>");
		out.println("<br><input type=submit value=\"Take the Tour\">");
		out.println("<br></form>");
		out.close();
	}
}

Third, the ConfirmServlet. It reads the value of appended form parameter and appends it to the hyper link URL.

package com.techmentro.learningpad;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class ConfirmServlet extends HttpServlet 
{
	public void doPost(HttpServletRequest request, 
			HttpServletResponse response)
			throws ServletException, IOException {
		
		//value of the appended parameter, is read.
		String user=request.getParameter("userName");
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		out.println("Dear "+user);
		//User name is appended to the URL of tourServlet
		out.println("<br> <a href=\"tourServlet?userName="+user+"\">Confirm the tour</a>");
		out.close();
	}
}

Fourth, the TourServlet. It reads the value of appended hyper link parameter.

package com.techmentro.learningpad;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class TourServlet extends HttpServlet 
{
	public void doGet(HttpServletRequest request, 
			HttpServletResponse response)
			throws ServletException, IOException {
		
		//value of the appended parameter is read.
		String user=request.getParameter("userName");
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		out.println("Sorry, "+user);
		out.println("<br> Site is down for routine maintenance. Visit again later.");
		out.close();
	}
}

Fifth, the web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
 <servlet-name>s1</servlet-name>
 <servlet-class>com.techmentro.learningpad.WelcomeServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>s1</servlet-name>
 <url-pattern>/welcomeServlet</url-pattern>
 </servlet-mapping>
 <servlet>
 <servlet-name>s2</servlet-name>
 <servlet-class>com.techmentro.learningpad.ConfirmServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>s2</servlet-name>
 <url-pattern>/confirmServlet</url-pattern>
 </servlet-mapping>
 <servlet>
 <servlet-name>s3</servlet-name>
 <servlet-class>com.techmentro.learningpad.TourServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>s3</servlet-name>
 <url-pattern>/tourServlet</url-pattern>
 </servlet-mapping>
</web-app>

Output screen images of the practical example:

The first screen represents the home page generated by index.html.

url_rewriting_screen1

The second screen represents the response of the WelcomeServlet.

url_rewriting_screen2

The third screen represents the response of the ConfirmServlet.

url_rewriting_screen3

The fourth screen represents the response of the TourServlet.

url_rewriting_screen4

Example Source Code

If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *