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