Home Servlets Java HttpServletRequest
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java HttpServletRequest

Advertisement
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.

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

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 8, 2013

Ask Questions?    Discuss: Java HttpServletRequest  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com