URL Rewriting in Java

The tutorial highlights the facts regarding URL rewriting in Java and the mechanism by which the requested URL is modified to include extra information.

URL Rewriting in Java

URL rewriting a session management technique and in this tutorial, you will be informed about URL Rewriting in Java.

URL writing is a way, which is used to hide the original URL. It is commonly handle by filter, You can use another way of session tracking that is URL rewriting where your browser does not support cookies. It is mechanism by which the requested URL is modified to include extra information.

Since for URL rewriting, provided space is limited so the extra information is limited to a unique session ID.

During URL writing, we use a token or identifier to the URL of the next Servlet or the next. For the same, we can sent parameter of name and value pairs using the selected format.

Example: In this example we are using URL rewriting concept of Session tracking.


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UrlRewritingExample extends HttpServlet {

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
java.io.PrintWriter out = response.getWriter();
String contextPath = request.getContextPath();
String encodedUrl = response
.encodeURL(contextPath + "/WelcomePage.jsp");

out.println("<title>URL Rewriter</title>");
out.println("<h2>URL rewriting Example</h2>");
out.println("For welcome page - <a href=\"" + encodedUrl
+ "\"> Click Here</a>.");



<h2 align="center">Welcome to the Roseindia world.</h2>

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">