# Java URL Rewriting

In 
Published 2022-12-03

This tutorial explains to you what a Java URL rewriting is and the advantages and disadvantages of URL Rewriting in Java.

By default, in Java, session tracking is performed using cookies, but when the client browser is not accepting cookies, you can use URL-rewriting in order to send/ receive session information.
URL rewriting involves placing a Session ID in the URL.

Here is a servlet which rewrite an URL, by adding a session ID parameter to the URL:

package servlets;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class UrlRewritingExample
 */
@WebServlet("/UrlRewritingExample")
public class UrlRewritingExample extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UrlRewritingExample() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html");
            java.io.PrintWriter out = response.getWriter();
            String contextPath = request.getContextPath();
            String encodedUrl = response.encodeURL(contextPath + "/ServletDestination?sid=5475671");
             
            out.println("");
            out.println("");
            out.println("<title>URL Rewriting</title>");
            out.println("");
            out.println("<center>");
            out.println("<h2>URL rewriting Example</h2>");
            out.println("Click to test the URL-rewrite in Java <a href="\""" +="" encodedurl="" "\"=""> Click Here</a>.");
            out.println("</center>");
            out.println("");
            }
 
}

When another servlet receive the request, it can read the new ID parameter:

package servlets;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class ServletDestination
 */
@WebServlet("/ServletDestination")
public class ServletDestination extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletDestination() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        HttpServletRequest req = (HttpServletRequest) request;
        java.io.PrintWriter out = response.getWriter();
        String sidValue = request.getParameter("sid"); 
         
        out.println("");
        out.println("");
        out.println("<title>URL Rewriting: Example : Read ID</title>");
        out.println("");
        out.println("<center>");
        out.println("<h2>URL rewriting Example: Read ID</h2>");
        out.println("<a>This page receve the SID = " + sidValue+ " </a>.");
        out.println("</center>");
        out.println("");
         
    }
 
}

When you run the UrlRewritingExample servlet you will see:

When you click on the link you will be redirected to the following page:

Note that you will see the SID (Session ID) into the new page.

Here are some of the URL rewriting advantages:

  • the mechanism is browser independent: you can use it even if the cookies usage is disabled in the browser
  • easy to debug as the parameter are passed in the URL

Here are some of the URL rewriting disadvantages:

  • is less secure than in the case of cookies
  • generate more network traffic
  • It will work only with links.