Maintain Session using URL Rewriting

Google Sponsored Links

In this method, we rewrite the URL and append the information to the URL to keep the track of the values. URL Rewriting can be used to maintain the session. We can maintain session using URL rewriting.

There are several methods to maintain session between client and server, see all types of method here

Lets see an example

Google Sponsored Links


<%-- Document : index Created on : 16 Aug, 2012, 4:22:58 PM Author : Abhishek --%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
How to maintain Session?

How to maintain Session?


URL rewriting

Once you click on the URL rewriting link class will append codedbug in the URL. See below code of



import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = "/url_rewriting")
public class URLRewriting extends HttpServlet{

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
		//HttpSession session =request.getSession(true);
		//session.setAttribute("name", "codedbug");
		String name="codedbug";

This rewrite.jsp page is the response page. This page will display the value that we have sent in the URL.

<%-- Document : rewrite Created on : 16 Aug, 2012, 5:51:23 PM Author : Abhishek --%>

<%@ page language="java"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
        URL Rewriting
        <%= request.getParameter("name") %>


Index page
URL Rewriting
Rewritten URL
URL Rewriting 1

Google Sponsored Links