In this tutorial you will learn about the Servlet ServletRequest and its application with practical example.
Servlet ServletRequest Interface
ServletRequest is an interface which provide the client request to a servlet. The servlet container creates a ServletRequest and passes it as an argument to the service method of servlet. It provides data to servlet which includes parameter name and values,attributes and input stream.
ServletRequest Interface Methods
There are some of ServletRequest methods are discussed below.
public String getParameter(String name) : This method is used to get the value of Request Parameter as a string, or null if the parameter not exist.
java.util.Enumeration getParameterNames(): This method is used to Returns an Enumeration of String objects containing the names of the parameters contained in this request.
public String[] getParameterValues(String name) : This is the method which returns array of String object or null if there is no parameter
public int getServerPort():This method is used to returns the port number to which the request was sent.
boolean isSecure() : This method is used tor eturns a boolean which indicate whether this request was made using a secure channel, such as HTTPS.
void removeAttribute(java.lang.String name) : This method is used to remove an attribute from this request.
void setAttribute(java.lang.String name, java.lang.Object o) : This method is used to store an attribute in this request.
public int getContentLength(): This method returns the length of request enitity data or
-1 if the length is not known.
public String getCharacterEncoding(): This method is used to returns the name of the character encoding which is used in the body of this request.
public String getContentType(): This is the method which returns the MIME type of the body of the request, or null if the type is not known.
public ServletInputStream getInputStream() throws IOException:This method returns the body of the request as binary data using a ServletInputStream.
public abstract String getServerName(): This method returns the host name of the server to which the request was sent .
Example :-
Let’s create an example to understand servlet request interface. Please read “Servlet Example with Netbeans” before creation this.
Step 1:-
We have to modify our index.html file like below for sending request to servlet. Check below code for this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>W3Adda.com</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="TestServlet"> <p>Name:<input type="text" name="userName"></p> <p>Site:<input type="text" name=siteName></p> <p><input type="submit" value="Submit"></p> </form> </body> </html> |
Here I have send 2 request parameter to servlet. One is username and second is siteName. In form action you have to put servlet name.
Step 2:-
The servlet code you can check in this step. By using request.getParamter() method we will get parameter value which is coming from index.html file. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ String strName=request.getParameter("userName"); String strSiteName=request.getParameter("siteName"); out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet TestServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet Test DEMO On :"+strSiteName+" By "+ strName + "</h1>"); out.println("</body>"); out.println("</html>"); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> } |
Output :-
The output of this code is shown in below two images.
Screen 1:-
Screen 2:-