Java HttpServletRequest

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

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.


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.


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

package net.roseindia;

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 {
PrintWriter out = response.getWriter();
out.println("Context Path : "+request.getContextPath());
Enumeration<String> headerNames = request.getHeaderNames();
out.println("Request Header Names : ");
out.print(headerNames.nextElement()+", ");
out.println("Method Name : "+request.getMethod());

Download Source Code

Share on Google+Share on Google+

Java HttpServletRequest

Posted on: August 8, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java HttpServletRequest  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image