Monday 14 March 2016

SERVLETREQUEST INTERFACE

ServletRequest Interface

An object of ServletRequest is used to provide the client request information to a servlet such as content type, content length, parameter names and values, header informations, attributes etc.



Methods of ServletRequest interface

There are many methods defined in the ServletRequest interface. Some of them are as follows:
Method
Description
public String getParameter(String name)
is used to obtain the value of a parameter by name.
public String[] getParameterValues(String name)
returns an array of String containing all values of given parameter name. It is mainly used to obtain values of a Multi select list box.
java.util.Enumeration getParameterNames()
returns an enumeration of all of the request parameter names.
public int getContentLength()
Returns 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 reading binary data in the request body.
public abstract String getServerName()
Returns the host name of the server that received the request.
public int getServerPort()
Returns the port number on which this request was received.



Example of ServletRequest to display the name of the user

In this example, we are displaying the name of the user in the servlet. For this purpose, we have used the getParameter method that returns the value for the given request parameter name.

index.html

  1. <form action="welcome" method="get">  
  2. Enter your name<input type="text" name="name"><br>  
  3. <input type="submit" value="login">  
  4. </form>  

DemoServ.java

  1. import javax.servlet.http.*;  
  2. import javax.servlet.*;  
  3. import java.io.*;  
  4. public class DemoServ extends HttpServlet{  
  5. public void doGet(HttpServletRequest req,HttpServletResponse res)  
  6. throws ServletException,IOException  
  7. {  
  8. res.setContentType("text/html");  
  9. PrintWriter pw=res.getWriter();  
  10.   
  11. String name=req.getParameter("name");//will return value  
  12. pw.println("Welcome "+name);  
  13.   
  14. pw.close();  
  15. }}  

RequestDispatcher in Servlet


The RequestDispatcher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp. This interface can also be used to include the content of another resource also. It is one of the way of servlet collaboration.
There are two methods defined in the RequestDispatcher interface.


Methods of RequestDispatcher interface

The RequestDispatcher interface provides two methods. They are:
  1. public void forward(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
  2. public void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException:Includes the content of a resource (servlet, JSP page, or HTML file) in the response.

forward() method of RequestDispatcher interface
As you see in the above figure, response of second servlet is sent to the client. Response of the first servlet is not displayed to the user.



include() method of RequestDispatcher interface
As you can see in the above figure, response of second servlet is included in the response of the first servlet that is being sent to the client.



How to get the object of RequestDispatcher

The getRequestDispatcher() method of ServletRequest interface returns the object of RequestDispatcher. Syntax:

Syntax of getRequestDispatcher method


  1. public RequestDispatcher getRequestDispatcher(String resource);  

Example of using getRequestDispatcher method


  1. RequestDispatcher rd=request.getRequestDispatcher("servlet2");  
  2. //servlet2 is the url-pattern of the second servlet  
  3.   
  4. rd.forward(request, response);//method may be include or forward  



Example of RequestDispatcher interface

In this example, we are validating the password entered by the user. If password is servlet, it will forward the request to the WelcomeServlet, otherwise will show an error message: sorry username or password error!. In this program, we are cheking for hardcoded information. But you can check it to the database also that we will see in the development chapter. In this example, we have created following files:
  • index.html file: for getting input from the user.
  • Login.java file: a servlet class for processing the response. If password is servet, it will forward the request to the welcome servlet.
  • WelcomeServlet.java file: a servlet class for displaying the welcome message.
  • web.xml file: a deployment descriptor file that contains the information about the servlet.
RequestDispatcher interface


index.html

  1. <form action="servlet1" method="post">  
  2. Name:<input type="text" name="userName"/><br/>  
  3. Password:<input type="password" name="userPass"/><br/>  
  4. <input type="submit" value="login"/>  
  5. </form>  


Login.java

  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5.   
  6. public class Login extends HttpServlet {  
  7.   
  8. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  9.         throws ServletException, IOException {  
  10.   
  11.     response.setContentType("text/html");  
  12.     PrintWriter out = response.getWriter();  
  13.           
  14.     String n=request.getParameter("userName");  
  15.     String p=request.getParameter("userPass");  
  16.           
  17.     if(p.equals("servlet"){  
  18.         RequestDispatcher rd=request.getRequestDispatcher("servlet2");  
  19.         rd.forward(request, response);  
  20.     }  
  21.     else{  
  22.         out.print("Sorry UserName or Password Error!");  
  23.         RequestDispatcher rd=request.getRequestDispatcher("/index.html");  
  24.         rd.include(request, response);  
  25.                       
  26.         }  
  27.     }  
  28.   
  29. }  


WelcomeServlet.java

  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4.   
  5. public class WelcomeServlet extends HttpServlet {  
  6.   
  7.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  8.         throws ServletException, IOException {  
  9.   
  10.     response.setContentType("text/html");  
  11.     PrintWriter out = response.getWriter();  
  12.           
  13.     String n=request.getParameter("userName");  
  14.     out.print("Welcome "+n);  
  15.     }  
  16.   
  17. }  


web.xml

  1. <web-app>  
  2.  <servlet>  
  3.     <servlet-name>Login</servlet-name>  
  4.     <servlet-class>Login</servlet-class>  
  5.   </servlet>  
  6.   <servlet>  
  7.     <servlet-name>WelcomeServlet</servlet-name>  
  8.     <servlet-class>WelcomeServlet</servlet-class>  
  9.   </servlet>  
  10.   
  11.   
  12.   <servlet-mapping>  
  13.     <servlet-name>Login</servlet-name>  
  14.     <url-pattern>/servlet1</url-pattern>  
  15.   </servlet-mapping>  
  16.   <servlet-mapping>  
  17.     <servlet-name>WelcomeServlet</servlet-name>  
  18.     <url-pattern>/servlet2</url-pattern>  
  19.   </servlet-mapping>  
  20.   
  21.   <welcome-file-list>  
  22.    <welcome-file>index.html</welcome-file>  
  23.   </welcome-file-list>  
  24. </web-app>  
Program of RequestDispatcher interface Example of RequestDispatcher interface Program of RequestDispatcher Example of RequestDispatcher

No comments:

Post a Comment