Servlet Request Interface

An object to provide client request information to a servlet. The servlet container creates a ServletRequest object and passes it as an argument to the servlet's service method.

A ServletRequest object provides data including parameter name and values, attributes, and an input stream. Interfaces that extend ServletRequest can provide additional protocol-specific data (for example, HTTP data is provided by HttpServletRequest..

Method Description
public String getParameter(String name) Is used returns value of parameter by name
public String[] getParameterValues(String name) returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
java.util.Enumeration getParameterNames() returns an enumeration of all parameter names.
public int getContentLength() Returns the size of the request entity data, or -1 if not known.
public String getCharacterEncoding() Returns the character set encoding for the input of this request.
public String getContentType() Returns the Internet Media Type of the request entity data, or null if not known.
public ServletInputStream getInputStream() throws IOException Returns an input stream for reading binary data in the request body.
public abstract String getServerName() Returns the host name of the server that received the request.
public int getServerPort() returns the port number to which the request was sent.

Example of ServletRequest

We are displaying the name of the user in the servlet. We have used the getParameter method that returns the value for the given request parameter name.

index.html
	

 <form method="post" action="check">

Name <input type="text" name="user" >

<input type="submit" value="submit">

</form>

FirstProg.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;



public class FirstProg extends HttpServlet {



  protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

       response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        try {

           

            String user=request.getParameter("user");

            out.println("<h2> Welcome "+user+"</h2>");

        } finally {            

            out.close();

        }

    }

}




web.xml
	

<servlet>

        <servlet-name>first</servlet-name>

        <servlet-class> FirstProg </servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name> first </servlet-name>

        <url-pattern>/ first </url-pattern>

    </servlet-mapping>


Servlet API provides two important interfaces

javax.servlet.ServletRequest and javax.servlet.http.HttpServletRequest.

HttpServletRequest interface


httpservletrequest interface

Some important methods of HttpServletRequest


Methods Description
StringgetContextPath() returns the portion of the request URI that indicates the context of the request
Cookies getCookies() returns an array containing all of the Cookie objects the client sent with this request
StringgetQueryString() returns the query string that is contained in the request URL after the path
HttpSessiongetSession() returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session
String getMethod() Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
PartgetPart(String name) gets the Part with the given name
String getPathInfo() returns any extra path information associated with the URL the client sent when it made this request.
StringgetServletPath() returns the part of this request's URL that calls the servlet