Java ServletRequest Interface

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


Share on Google+Share on Google+

Java ServletRequest Interface


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 : -

package net.roseindia;

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="" xmlns="" xmlns:web="" xsi:schemaLocation="" 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 :


Posted on: October 5, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles