Advertisement



< Prev
Next >



ServletRequest



Using the ServletRequest object , we can access the data sent along the user request i.e. request parameters and data within the request headers. This helps the Servlet to send back an appropriate response. Using ServletRequest object, we could also access the information about the request made by the user.

This information about the user request could be -
On the arrival of a user request, the Servlet Container creates the ServletRequest object or it may maintain a pool of ServletRequest objects and selects one request object to pass it to the service() method of Servlet class, thereby allowing a Servlet to access the user request details.

ServletRequest is an interface and it is a part of the Servlet API. The full path to import ServletRequest is javax.servlet.ServletRequest.




ServletRequest methods


Let's take a look at the methods part of javax.servlet.ServletRequest class.

Methods Description
String getParameter(String name) This method returns the value of a parameter with the request.
Enumeration getParameterNames() This method returns all the names of parameters with the request.
Object getAttribute(String str) This method returns the value of an attribute str with the request.
Locale getLocale() This method returns the preferred Locale of client.
String getProtocol() This method returns the name of the Protocol used by the request.
String getContentType() This method returns the MIME type of the body of request.
String getLocalAddr() This method the local IP address on which the request is received.
String getServerName() This method returns the server name with the request.
String getServerPort() This method returns the server port on which the request is received.
String isSecure() This method is the request was sent using a secure protocol(HTTPS)or not.





Creating a webpage which calls the Servlet


We are creating a webpage which asks the user to enter some information in a form. This form calls the Servlet when user submits a form and sends the form data within the request to the server.

Form1.jsp
<html>

<head>
<title> ServletRequest Demo </title>
</head>


<body>
Please enter your details :

<form action = "MyServlet">
Name : <input type = "text"   name = "username" />
City : <input type = "text"   name = "cityname" /> <br/>


<input type = "submit" value = "submit" />
</form>

</body>
</html>



Advertisement




Using the ServletRequest to access the user request


We are creating a Servlet extending GenericServlet abstract class. In this Servlet class, we are accessing the information about the user request and the form data(parameters) sent along the request. We have named this Servlet class MyServlet1.java.

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

public class MyServlet1 extends GenericServlet
{

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();

out.println("Accessing request parameters using ServletRequest object");

out.println("Welcome "+ request.getParameter("username"));
out.println("How is weather in " + request.getParameter("cityname") + "?");

out.println("Request Locale : " + request.getLocale());
out.println("Request Protocol : " + request.getProtocol());
out.println("Request ContentType : " + request.getContentType());
out.println("Request Local IP : " + request.getLocalAddr());
out.println("Request Server Name : " + request.getServerName());
out.println("Request Port : " + request.getServerPort());
out.println("Is request secure : " + request.isSecure());
}

}






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>ServletRequestExample</servlet-name>
	<servlet-class>MyServlet1</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>ServletRequestExample</servlet-name>
	<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

</web-app>


In deployment descriptor file, <servlet> tag 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 and we have named this URL MyServlet.




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 Servlet class by entering the command at the folder where you've stored the Servlet class file.

javac -d WEB-INF/classes MyServlet1.java





Executing the Servlet


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



As soon as the user enters the name and submits the form, the request goes to the Servlet.

Hence, you will see the URL containing the full path to the file with the request parameter -




This Servlet displays a personalised welcome message to the user, by accessing the value of parameter username, cityname through the ServletRequest object and it also displays some more information about the user request.




Please share this article -




< Prev
Next >
< Tomcat Environmnt Setup
ServletResponse >



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