Using the ServletResponse , we can not only write back an appropriate response to the user request but can also provide the details about the response to the user
by setting the response headers.
This information about the response could be -
The buffer size specified for the body of response.
The content-type (MIME type) of the body of response.
The specified locale associated with the response.
The character encoding of the response.
On the arrival of a user request, the Servlet Container creates the ServletResponse object or it may maintain a pool of ServletResponse objects and selects one response object
to pass it to the service() method of Servlet class, thereby allowing
the ServletResponse object to respond to the user request.
ServletResponse is an interface and it is a part of the Servlet API. The full path to import and access all the methods provided by ServletResponse is
javax.servlet.ServletResponse.
ServletResponse methods
Let's take a look at the some of the most used methods part of javax.servlet.ServletResponse class.
Methods
Description
StringgetCharacterEncoding()
This method returns the character encoding for the response.
LocalegetLocale()
This method returns the preferred Locale of response.
StringgetContentType()
This method returns the MIME type of the response body.
intgetBufferSize()
This method gets the buffer size for the response body.
voidsetCharacterEncoding(String )
This method sets the character encoding of the response.
voidsetLocale(Locale)
This method sets the preferred locale of response.
voidsetContentType(String )
This method returns the MIME type of the body of request.
voidsetBufferSize(int )
This method set the buffer size for the body of response.
PrintWritergetWriter()
This method returns the PrintWriter object to be able to write back to the user.
Creating a webpage which calls the Servlet
We are creating a webpage which asks the user to type in a survey through 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 fill the survey :
<form action = "MyServlet">
Favorite color : <input type = "text" name = "color" />
Favorite food : <input type = "text" name = "food" /> <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 by accessing
the form data(parameters) sent along the request and using ServletResponse object to
write back a response and providing some details about the response.
We have named this Servlet class MyServlet1.java.
MyServlet1.java
import java.io.*;
import javax.servlet.*;
import java.util.*;
public class MyServlet1 extends GenericServlet
{
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
//Using response object to set character encoding
response.setCharacterEncoding("UTF-8");
//Using response object to create a PrintWriter object to write back to client.
PrintWriter out = response.getWriter();
//Using response object to set the ContentType.
response.setContentType("text/html");
//Using response object to set the Locale
response.setLocale(new Locale("English", "India"));
//Using response object to set the buffer size of response body
response.setBufferSize(30000);
out.println("<h2>using ServletResponse</h2>");
//Using PrintWriter created by ServletResponse object to write back to the user
out.println("Hey, "+ request.getParameter("color") + " color is a good choice." +"<br/>");
out.println("So you like " + request.getParameter("food") + "?" + " That's great!" + "<br/>");
out.println("Response Locale : " + response.getLocale() + "<br/>");
out.println("Response ContentType : " + response.getContentType() + "<br/>");
out.println("Response Character Encoding : " + response.getCharacterEncoding() + "<br/>");
out.println("Response Buffer Size : " + response.getBufferSize() + "KB");
}
}
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-
Project Folder - We have created a project folder named ServletResponseEx within
the webapps folder of Tomcat installation folder. This folder contains the Servlet class(.java) file and WEB-INF folder.
WEB-INF - Within your project folder, you create another folder named WEB-INF and this folder should contain the deployment descriptor file(web.xml) of your Java Servlet program.
classes - Within the WEB-INF folder you should create a folder named classes. This folder should contain the compiled(.class) form of your Java Servlet class.
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 -
In deployment descriptor file, <servlet> has two child tags <servlet-name> and <servlet-class> :
<servlet-name> tag is used to specify a unique name for our Servlet class, we have given it a unique name ServletResponseExample.
<servlet-class> tag is used to specify the full qualified name of the Servlet class and in our example our Servlet class is named MyServlet1.
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 -
You could set the classpath by entering this command at the Command Prompt, but this only temporarily sets the classpath to the path of servlet-api.jar file. If you restart your system, you will have to set the classpath again.
set classpath=C:\apache-tomcat-9.0.2\lib\servlet-api.jar
In order to set the classpath permanently, you need to set your classpath system variables. A simple tutorial on how to permanently set the classpath system variables, you may read
How to permanently set the classpath system variable
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 -
The project folder - ServletResponseEx
The url-pattern of the Servlet class declared in web.xml - MyServlet
The parameter and its value sent along the request - color = Blue & food = Pasta
This Servlet writes back a personalised welcome message to the user,
by creating a PrintWriter object and by accessing the value of parameter color, food through the ServletRequest object. The Servlet also
displays the information about the response i.e. locale, character set encoding, buffer size of response body, content-type using the ServletResponse object.