Servlets Request & Response Interface

ServletRequest Interface

The ServletRequest Interface is used to handle client request to access a servlet. It provides the information of a servlet like content type, content length, parameter names and values etc.

ServletRequest Interface Methods

Following are the important methods of ServletRequest Interface:

MethodsDescription
public Object getAttribute(String name)Returns the value of the named attribute as an Object.
public int getContentLength( )Returns the length of the request body in bytes.
public String getContentType( )Returns the MIME type of the request body.
public String getParameter(String name)Returns the value of a request parameter as a string.
public String getProtocol( )Returns the name and version of the request protocol.
public int getRemotePort( )Returns the Internet Protocol (IP) port of the client.
public String getServerName( )Returns the host name of the server to which the request was sent.

Example : Request to take username & password. Display username.

//index.html

<form action="test" method="post">
     User Name: <input type="text" name="uname"><br>
     Password: <input type = "password name = "password"><br>
     <input type="submit" value="Log In">
</form>

//web.xml

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/test</url-pattern>
</servlet-mapping>

//ServletDemo.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletDemo extends HttpServlet
{
      protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
      {
          response.setContentType("text/html");
          PrintWriter pw = response.getWriter();
          try
          {
              String username = req.getParameter("username");
              String password = req.getParameter("password");
              pw.println("<h1> Hello"+username+"<h1>");
          }
          finally
          {
              pw.close();
          }
      }
}

ServletResponse Interface

The ServletResponse interface defines an object to help a Servlet in sending a response to the client. It has various methods that help a servlet to respond to the client requests.

ServletResponse Interface Methods

MethodsDescription
public void flushBuffer( )It forces the content in the buffer to be written to the client.
public int getBufferSize( )Returns the actual buffer size.
public String getContentType( )Returns the content type used to send this response.
public PrintWriter getWriter( )Returns the PrintWriter object that can be used to send character text to the client.
public void reset( )Clears the buffer as well as status code and header data.
public void setBufferSize(int size)Sets the specified buffer size for the body of the response.
public void setContentType(String type)Sets the content type of the response being sent to the client.