Share on Google+Share on Google+

Java Servlet : URL Rewriting

In this tutorial, we will discuss about URL Rewriting. It is one of session tracking technique.

Java Servlet : URL Rewriting

In this tutorial, we will discuss about URL Rewriting. It is one of session tracking technique.

URL Rewriting :

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. This extra information can be in the form of extra path info, added parameters or some other URL change.
Since for URL rewriting, provided space is limited so the extra information is limited to a unique session ID.

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

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 {
response.setContentType("text/html"); 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=""
xmlns="" xmlns:web=""
id="WebApp_ID" version="2.5">

Output :


Posted on: October 9, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.