Servlet Request and Response

Servlet handles various client requests and provides their responses. It provides two interfaces i.e ServletRequest and ServletResponse for this purpose. Let’s discuss about these interfaces in detail.

ServletRequest interface

ServletRequest is an interface whose object is used to provide the information of each request to servlet. This information may be any name, type, value or other attribute. This interface is present in javax.servlet package. Servlet container is responsible to create ServletRequest object which is given with service() method argument.

Methods of ServletRequest

These are some important methods provided by ServletRequest interface.

                  Method

                  Description

String getParameter(String name)

This method returns the value given in request as a String.

Object getAttribute(String name)

This method provides the attribute of request as an Object.

String getServerName()

This method provides the server name to which request is sent.

int getServerPort()

This method returns the port number to which request is sent.

boolean isSecure()

This method indicates whether the server is secure or not.

ServletResponse interface

The object of ServletResponse interface is used to send the responses to the clients. The information send in responses can be a binary or character data.

ServletResponse interface is present in javax.servlet package and passes as an arguement of service() method.

Methods of ServletResponse

               Method

                  Description

PrintWriter getWriter()

This method is used to send character data in response.

int getBufferSize()

This method returns the capacity of buffer in sent response.

ServletOutputStream getOutputStream()

This method is used to send binary data in response.

boolean isCommited()

This method indicates the completion of response.

void reset()

This method is used to remove the data present in buffer.

void setContentType(String type)

This method is used to set the type of content.

Servlet Request and Response example with HTML

In this example, the name is given as a request in HTML form. The object of HttpServletRequest interface is used to handle the request and HttpServletResponse interface is used to provide the response.

index.html

<form action="serv" method="get">
<h3>Enter your Name:</h3>
<input type="text" name="username">
<input type="submit" value="submit">
</form>

HttpServletReq.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(“/serv”)
public class HttpServletReq extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
 res.setContentType("text/html");
 PrintWriter pw=res.getWriter();
 String s1=req.getParameter("username");
 pw.println("<h1>Welcome:"+s1+"</h1>");
 String s2=req.getServerName();
 pw.println("<h1>Server name:"+s2+"</h1>");
 int i=req.getServerPort();
 pw.println("<h1>Server Port:"+i+"</h1>");
 boolean b=req.isSecure();
 pw.println("<h1>Is the server secure:"+b+"</h1>");
 } 
 }
Output: