Java Servlet : URL Rewriting


 

Java Servlet : URL Rewriting

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

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.

UrlRewritingExample.java


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 {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
String contextPath = request.getContextPath();
String encodedUrl = response
.encodeURL(contextPath + "/WelcomePage.jsp");

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

}

WelcomePage.jsp


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

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">
<display-name>SessionTracking</display-name>
<servlet>
<description></description>
<display-name>UrlRewritingExample</display-name>
<servlet-name>UrlRewritingExample</servlet-name>
<servlet-class>UrlRewritingExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UrlRewritingExample</servlet-name>
<url-pattern>/UrlRewritingExample</url-pattern>
</servlet-mapping>
</web-app>

Output :



Ads