Java ServletRequest Interface


 

Java ServletRequest Interface

In this tutorial, we will discuss about Servlet Request 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 :

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 {
		response.setContentType("text/html");
		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">
<display-name>ServletExample</display-name>

<!-- servlet request mapping-->
<servlet>
<servlet-name>ServletRequestExample</servlet-name>
<servlet-class>net.roseindia.ServletRequestExample </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletRequestExample </servlet-name>
<url-pattern>/servletRequestExample </url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>form.jsp</welcome-file>

</welcome-file-list>
</web-app>

Output :

Ads