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.



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



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.



Pin It on Pinterest

Share This