The ServletRequest and ServletResponse interfaces are used in Java Servlet to handle client requests and server responses. ServletRequest reads data sent by the client, while ServletResponse sends the processed response back to the browser.
ServletRequestis used to access form data, headers, and request information.ServletResponseis used to generate and send responses to the client.- Both interfaces help servlets process client-server communication dynamically.
ServletRequest Interface
The ServletRequest interface is used to receive and process client request data in a servlet application.
- Retrieves form data sent by the client.
- Provides methods to access request parameters and headers.
- Reads client information such as protocol, content type, and host details.
- Supports input streams for reading binary request data.
Information Accessible by ServletRequest
- Request parameter names and values.
- HTTP request methods such as GET and POST.
- Client host and server details.
- Content type and content length.
- Binary data using input streams.
ServletResponse Interface
The ServletResponse interface is used to send responses from the servlet to the client browser.
- Sends dynamic responses to the client.
- Sets response content type and content length.
- Provides PrintWriter and output streams for writing data.
- Supports HTTP response handling.
Steps to show Example of ServletRequest to display the name of user
Follow these steps to display the name of user using servlet.
Step 1: Create Dynamic Web Project
- Go to File -> New -> Dynamic Web Project
- Project Name -> ServletExample
- Select Apache Tomcat runtime
- Click Finish
Step 2: Create HTML File
This HTML form takes the user's name as input and sends it to the servlet using the GET method.
<form action="welcome User" method="get">
Enter your name<input type="text" name="Name"><br>
<input type="Submit" value="Login">
</form>
Step 3: Servlet Class (MockServ.java)
This servlet receives the request, extracts the user name using getParameter(), and sends a response back to the browser.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MockServ extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
// Uses throws exception
throws ServletException, IOException
{
resp.setContentType("text/html");
// sets Content Type
PrintWriter ab = resp.getWriter();
String Name = req.getParameter("Name");
// thiswill return value
ab.println(" Welcome User " + Name);
// this will print the Name Welcome User
ab.close();
}
}
Step 4: web.xml Configuration
This configuration maps the servlet URL pattern to the servlet class.
<servlet>
<servlet-name>MockServ</servlet-name>
<servlet-class>MockServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MockServ</servlet-name>
<url-pattern>/welcomeUser</url-pattern>
</servlet-mapping>
Step 5: Run Your Application
- Right-click your project (ServletExample)
- Click Run As -> Run on Server
Output:
Open browser and type:
http://localhost:8080/ServletExample/newindex.html
If the user enters Geeks, the browser displays
Welcome User Geeks
Methods of ServletRequest Interface
| Method | Description |
|---|---|
| public String getParameter(String name) | Returns the value of the specified request parameter |
| public String[] getParameterValues(String name) | Returns all values of the specified parameter as an array |
| java.util.Enumeration getParameterNames() | Returns all request parameter names |
| public int getContentLength() | Returns the length of the request content |
| public String getCharacterEncoding() | Returns the character encoding of the request |
| public String getContentType() | Returns the MIME type of the request |