Advertisement



< Prev
Next >



URL Rewriting



In this article, we are going to understand how to perform URL rewriting, a technique used to maintain a client session.

URL Rewriting means adding the session data such as request parameters to the URL path of a request, which is going to be passed to a Servlet. The called Servlet reads the parameters passed to it in the URL, which helps it in identifying the client and sending back an appropriate response to the client.

For example, an URL link on a webpage -
http://www.decodejava.com/MyServlet?name="admin"&message="never give up!" 


In the above mentioned URL, the two parameters named name and message are appended to the URL path and are passed to the calling Servlet - MyServlet. These parameters are session data, used by the calling Servlet to identify a user, maintain a session and send back an appropriate response.




Creating a webpage which calls the Servlet


We are creating a webpage which asks the user to enter his/her name and city and press the submit button, which when submitted calls the Servlet with the <url-pattern> Welcome, specified in the deployment descriptor file(web.xml), passing this Servlet the parameters named username and cityname.

Webpage1.jsp
<html>

<head>
<title> URL Rewriting Demo </title>
</head>


<body>
<b>Welcome to Disneyland! Please enter your details.</b>
<br/>
<br/>
<br/>

<form action = "Welcome" method="get" >
Name : <input  type = "text" name = "username"/>
City : <input  type = "text" name = "cityname"/>
<input  type = "submit" name = "submit"/>
</form>

</body>
</html>





Using RequestDispatcher to forward a request


We are creating a Servlet by extending the HttpServlet abstract class. HttpServlet class extends GenericServlet abstract class. This class named MyServlet1.java, implements doGet() method of HttpServlet class to handle get request made by the client after filling the details on Webpage1.jsp.


MyServlet1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class MyServlet1 extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();


//Reading parameters
String name = request.getParameter("username");
String city = request.getParameter("cityname");


//Welcoming the user
out.println("<b>" + "Welcome, " + name + " from " + city + "</br>");
out.println("<br/> <br/>");


//Passing the request parameters in a call to the next Servlet
out.println("<a href = 'Rides?username="+name+"'>rides</a>");
out.println("<a href = 'Attraction?username="+name+"'>attraction</a>");

out.println("<br/> <br/> <br/>");


out.println("<b>Click on the links above  for the information about Disneyland. </b>");

}
}



Another Servlet displaying information about the Attractions in Disneyland.

MyServlet2.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class MyServlet2 extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();


//Reading parameters
String name = request.getParameter("username");
String city = request.getParameter("cityname");

//Welcoming the user
out.println("<b>"+ "Welcome, " + name + "</b>");
out.println("<br/> <br/>");

//Passing the request parameters in a call to the next Servlet
out.println("<a href = 'Rides?username="+name+" '>rides</a>");
out.println("<a href = 'Attraction?username="+name+" '>attraction</a>");

out.println("<br/> <br/>");


out.println("<h2>Attractions in Disneyland! </h2> <br/>");
out.println("<b> Alice in wonderlands</b> <br/>");
out.println("<b> Animation Academy </b> <br/>");
out.println("<b> Big Thunder Mountain Railroad </b> <br/>");
out.println("<b> Chip 'n Dale TreeHouse </b> <br/>");

}

}



Another Servlet displaying information about the Rides in Disneyland.

MyServlet3.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class MyServlet3 extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();


//Reading parameters
String name = request.getParameter("username");
String city = request.getParameter("cityname");


//Welcoming the user
out.println("<b>"+ "Welcome, " + name + "</b>");
out.println("<br/> <br/>");


//Passing the request parameters in a call to the next Servlet
out.println("<a href = 'Rides?username="+name+" '>rides</a>");
out.println("<a href = 'Attraction?username="+name+" '>attraction</a>");

out.println("<br/> <br/>");


out.println("<h2>Rides in Disneyland! </h2> <br/>");
out.println("<b> Disneyland Monorail </b> <br/>");
out.println("<b> Disneyland Railroad </b> <br/>");
out.println("<b> Dumbo The Flying Elephant </b> <br/>");
out.println("<b> Donald's Boat </b> <br/>");
out.println("<b> Disney Flyers </b> <br/>");

}

}




Advertisement




Directory Structure of Servlet files




The diagram above depicts how to arrange the Servlet files in a specific directory structure, as per Java Servlet Specification-






Creating the Deployment Descriptor file


As per the Java Servlet specifications, every web application based on Servlet must have a Deployment Descriptor file(an XML file) named web.xml. So, let's create one -


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

  <display-name>Welcome tomcat</display-name>
  <description>
     Welcome tomcat
  </description>

<servlet>
 	<servlet-name>Servlet1Welcome</servlet-name>
	<servlet-class>MyServlet1</servlet-class>
</servlet>

<servlet>
 	<servlet-name>ServletAttraction</servlet-name>
	<servlet-class>MyServlet2</servlet-class>
</servlet>

<servlet>
 	<servlet-name>ServletRides</servlet-name>
	<servlet-class>MyServlet3</servlet-class>
</servlet>


<servlet-mapping>
	<servlet-name>ServletWelcome</servlet-name>
	<url-pattern>/Welcome</url-pattern>
</servlet-mapping>


<servlet-mapping>
	<servlet-name>ServletRides</servlet-name>
	<url-pattern>/Rides</url-pattern>
</servlet-mapping>

<servlet-mapping>
	<servlet-name>ServletAttraction</servlet-name>
	<url-pattern>/Attractions</url-pattern>
</servlet-mapping>



</web-app>


In deployment descriptor file, <servlet> has two child tags <servlet-name> and <servlet-class> :






Note


The child tag <servlet-name> of <servlet> tag is matched with the <servlet-name> child tag of <servlet-mapping>. The <url-pattern> child tag is used to specify the URL to access Servlet, Hence -




Setting the classpath


Much of the support for developing the web applications based on the Java Servlet technology does not come with the core Java. Hence, in order to compile the Servlet programs, we have to set the classpath to a jar file named servlet-api.jar.

This jar file provides all the classes that are required for the Servlet programming and it comes within the lib Folder of Tomcat installation folder.

For example, in our case we have installed Tomcat Web Server within the C: Drive, hence the path to our lib folder containing the servlet-api.jar is - C:\apache-tomcat-9.0.2\lib

There are two ways to set the classpath -




Compiling the Servlet class


After setting the classpath, you need to compile the both of the Servlet class by entering the command at the folder where you've stored the Servlet class file.

javac -d WEB-INF/classes MyServlet1.java


javac -d WEB-INF/classes MyServlet2.java


javac -d WEB-INF/classes MyServlet3.java





Executing the Servlet


First we execute the webpage containing the form, asking the user to enter the username and cityname and press the submit button.




On pressing the submit button, these details are passed to the Servlet with a <url-pattern> Welcome specified in the web.xml. In call to this Servlet, it is also passed the parameters like username and cityname with the request and displayed in URL. Using these parameters, a personalised messages is displayed to the user with links to get more information about Disneyland.




When the user clicks the link named Rides, a Servlet with the <url-pattern> Rides(as specified in web.xml) is called and the parameters like username of the user are also passed to this Servlet with the request, as shown in the URL.




When the user clicks the link named Attractions, a Servlet with the <url-pattern> Attractions (as specified in web.xml) is called and the parameters like username of the user are also passed to this Servlet with the request, as shown in the URL.






Please share this article -




< Prev
Next >
< Forwarding a Servlet
Hidden fields >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement