The ServletRequest Interface defines an object which is used to encapsulate information about the user’s request, including parameter name/value pairs, attributes, and an input stream.
The ServletRequest interface provides important methods which enables you to access information about the user. For example, the getParameterNames() method returns an Enumeration containing the parameter names for the current request. To get the value of each parameter, we can use the the getParameter() method of the ServletRequest interface.
Lets take a look on all the method of the ServletRequest Interface in the below example.
RequestInterfaceExample.java
package com.javainterviewpoint; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class RequestInterfaceExample implements Servlet { public void init(ServletConfig config) throws ServletException { } public void destroy() { } public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { //Obtaining the Server Port System.out.println("Server Port: " + request.getServerPort()); //Getting the Server Name System.out.println("Server Name: " + request.getServerName()); //Getting the Protocol Used System.out.println("Protocol: " + request.getProtocol()); //Character Encoding which is set System.out.println("Character Encoding: " + request.getCharacterEncoding()); //Getting the Content Type System.out.println("Content Type: " + request.getContentType()); //Getting the Remote Address System.out.println("Remote Address: " + request.getRemoteAddr()); //Getting Remote Host System.out.println("Remote Host: " + request.getRemoteHost()); //Obtaining the Scheme System.out.println("Scheme: " + request.getScheme()); //Get all the parameters passed from index.jsp Enumeration parameters = request.getParameterNames(); while (parameters.hasMoreElements()) { String parameterName = (String) parameters.nextElement(); System.out.println("Parameter Name: " + parameterName); System.out.println("Parameter Value: " + request.getParameter(parameterName)); } //Setting the request attribute request.setAttribute("requestAttribute1", "requestAttribute1"); request.setAttribute("requestAttribute2", "requestAttribute2"); //Retrieving all the request attributes Enumeration attributes = request.getAttributeNames(); while (attributes.hasMoreElements()) { String attribute = (String) attributes.nextElement(); System.out.println("Attribute name: " + attribute); System.out.println("Attribute value: " + request.getAttribute(attribute)); } } public String getServletInfo() { return null; } public ServletConfig getServletConfig() { return null; } }
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>ServletsTutorial</display-name> <servlet> <servlet-name>RequestInterfaceExample</servlet-name> <servlet-class>com.javainterviewpoint.RequestInterfaceExample</servlet-class> </servlet> <servlet-mapping> <servlet-name>RequestInterfaceExample</servlet-name> <url-pattern>/RequestInterfaceExample</url-pattern> </servlet-mapping> </web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>RequestInterfaceExample</title> </head> <body> <form action="./RequestInterfaceExample" method = "POST"> FirstName : <input type="text" name="firstName"><br> LastName : <input type="text" name="lastName"><br> <input type="submit" value="Submit Form"> <input type="reset" value="Reset Form"> </form> </body> </html>
Output
Hit on the Url : http://localhost:8080/ServletTutorial/index.jsp
Fill in the Form and hit on submit, in the console you will get the below output.
Server Port: 8080 Server Name: localhost Protocol: HTTP/1.1 Character Encoding: null Content Type: application/x-www-form-urlencoded Remote Address: 0:0:0:0:0:0:0:1 Remote Host: 0:0:0:0:0:0:0:1 Scheme: http Parameter Name: lastName Parameter Value: InterviewPoint Parameter Name: firstName Parameter Value: Java Attribute name: requestAttribute2 Attribute value: requestAttribute2 Attribute name: requestAttribute1 Attribute value: requestAttribute1
Leave a Reply