An item of ServletRequest is used to offer the customer request statistics to a servlet inclusive of content material type, content material length, parameter names and values, header information, attributes etc.
Many methods are defined in ServletRequest Interface. Some of them as given below:
Method | Description |
public String getParameter(String name) | is used to acquire the usefulness of a parameter by name. |
public String[] getParameterValues(String name) | Coming again an array of String carries all usefulness of the given parameter name. It is mainly used to obtain values of a Multi select list box. |
java.util.Enumeration getParameterNames() | Coming back an enumeration of all of the appealing parameter names. |
public int getContentLength() | Coming back the size of the request entity data, or -1 if not known. |
public String getCharacterEncoding() | Returns the character set encoding for the input of this request. |
public String getContentType() | Returns the Internet Media Type of the request entity data, or null if not known. |
public ServletInputStream getInputStream() throws IOException | Returns an input stream for scanning binary data in the requesting body. |
public abstract String getServerName() | Returns the host identity of the server that collects the request. |
public int getServerPort() | Coming back to the port number on which this request was received. |
In this example, we're showing the call of the consumer withinside the servlet. For this purpose, we've used the getParameter approach that returns the price for the given request parameter call.
Index.html
<form action="welcome" method="get"> Enter your name<input type="text" name="name"><br> <input type="submit" value="login"> </form>
DemoServ.java
import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class DemoServ extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); PrintWriter pw=res.getWriter(); String name=req.getParameter("name");//will return value pw.println("Welcome "+name); pw.close(); }}
Some other example of ServletRequest interface are:
To display all the header information the example of ServletRequest are
In this example, we are showing the header information of the servlet for example content type, content length, user agent etc.