Home Tutorial Servlet Java ServletRequest Interface


Share on Google+Share on Google+
Java ServletRequest Interface
Posted on: October 5, 2012 at 12:00 AM
In this tutorial, we will discuss about Servlet Request Interface.

Java ServletRequest Interface

In this tutorial, we will discuss about Servlet Request Interface.

ServletRequest Interface :

ServletRequest interface defines object to handle client request and provide it to the servlet.object of ServletRequest is created by the servlet container and works as argument of the servlet's service method. It is defined in javax.servlet. Packages that use ServletRequest are javax.servlet and javax.servlet.http.

Following are some methods ServletRequest provides -

  • getAttribute(java.lang.String name) : It returns value as object of the named attribute. If there is no attribute of specified name then it will return null.
  • getAttributeNames() : It returns names of all available attribute to the request in form of an Enumeration.
  • getContentLength() : It returns length of requested body in bytes. If it is not known will return -1.
  • getParameter(String name) : It returns the value of a request parameter as a String. If there is no parameter, return null. This method is used when parameter has single value.
  • getParameterNames() : it returns an enumeration of String objects containing complete list of all parameters in the given request.
  • getParameterValues() : It returns array of String objects containing all the values of the requested parameter. If there is no parameter, return null.

Other methods are - getAsyncContext(), getCharacterEncoding(), getContentType(), getDispatcherType(), getDispatcherType(), getInputStream(),
getLocalAddr(), getLocale(), getLocales() , getLocalName(), getLocalPort(), getParameterMap(), getProtocol(), getReader(), getReader(),
getRealPath(java.lang.String path), getRemoteAddr(), getRemoteHost(), getRemotePort(), getRequestDispatcher(java.lang.String path), getScheme(),
getServerName(), getServerPort(), getServletContext(), isAsyncStarted(), isAsyncSupported(), isSecure(),  removeAttribute(java.lang.String name),
setAttribute(java.lang.String name, java.lang.Object o), setCharacterEncoding(java.lang.String env), startAsync(),
startAsync(ServletRequest servletRequest, ServletResponse servletResponse)

Example :

ServletRequestExample.java -

package net.roseindia;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletRequestExample extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		out.println("Server Name : " + request.getServerName());


web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<!-- servlet request mapping-->
<servlet-class>net.roseindia.ServletRequestExample </servlet-class>
<servlet-name>ServletRequestExample </servlet-name>
<url-pattern>/servletRequestExample </url-pattern>


Output :


Related Tags for Java ServletRequest Interface:

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: October 5, 2012

Recommend the tutorial

Advertisements Advertisements