Java HttpServletRequest

HttpServletRequest instance is passed as an argument to the service method of Servlet.

Java HttpServletRequest

Java HttpServletRequest

In this section we will read about HttpServletRequest.

HttpServletRequest is an interface and extends the ServletRequest interface. By, extending the ServletRequest this interface is able to allow request information for HTTP Servlets. Object of the HttpServletRequest is created by the Servlet Container and then it is passed to the service method (doGet(), doPost(), etc.) of the Servlet.

Syntax

public interface HttpServletRequest
extends ServletRequest

Methods of HttpServletRequest

HttpServletRequest has various methods. Some of them are as follows :

  • String getContextPath() : This method is used to get the portion of the requested URI.
  • Cookie[] getCookies() : This method is used to get all the Cookie the object in an array.
  • java.lang.String getHeader(java.lang.String name) : This method is used to get the given request header.
  • java.util.Enumeration<java.lang.String> getHeaderNames() : This method is used to get all the header names of the current request.
  • int getIntHeader(java.lang.String name) : This method is used to get the given request header value as an int.
  • java.lang.String getMethod() : This method is used to get the HTTP method within which the request was made.
  • HttpSession getSession() : This method is used to get the current session or creates a new session if the request doesn't have session.
  • java.lang.String getRequestedSessionId() : This method is used to get the id of the current session.

To read the all the methods of HttpServletRequest you may refer to the Oracle documentation for HttpServletRequest.

Example

Here an example is being given which will demonstrate you about how to use the HttpServletRequest and its method in web application. In this example I will create a Servlet inside which we will use the various methods of HttpServletRequest.

Directory Structure

HttpServletRequestExample.java

package net.roseindia;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpServletRequestExample extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head></head><body>");
out.println("Context Path : "+request.getContextPath());
out.println("<br/>");
Enumeration<String> headerNames = request.getHeaderNames();
out.println("Request Header Names : ");
while(headerNames.hasMoreElements())
{
out.print(headerNames.nextElement()+", ");
}
out.println("<br/>");
out.println("Method Name : "+request.getMethod());
}
}

Download Source Code