RequestDispatcher Interface
The RequestDispacher 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 antoher 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:
|
|
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. |
|
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: |
public RequestDispatcher getRequestDispatcher(String resource);
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
<form action=”Login” method=”post”>
Name:<input type=”text” name=”userName”/><br/>
Password:<input type=”password” name=”userPass”/><br/>
<input type=”submit” value=”login”/></form>
Login.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Login extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
String n=request.getParameter(“userName”);
String p=request.getParameter(“userPass”);
if(p.equals(“secret”){
RequestDispatcher rd=request.getRequestDispatcher(“WelcomeServlet”);
rd.forward(request, response);
}
else{
out.print(“Sorry UserName or Password Error!”);
RequestDispatcher rd=request.getRequestDispatcher(“/index.html”);
rd.include(request, response);
}
}
}
WelcomeServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class WelcomeServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
String n=request.getParameter(“userName”);
out.print(“Welcome “+n);
} }
SendRedirect in servlet
The sendRedirect() method ofHttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file.It accepts relative as well as absolute URL.
It works at client side because it uses the url bar of the browser to make another request. So, it can work inside and outside the server.
Difference between forward() and sendRedirect() method
There are many differences between the forward() method of RequestDispatcher and sendRedirect() method of HttpServletResponse interface. They are given below:
|
forward() method |
sendRedirect() method |
|
The forward() method works at server side. |
The sendRedirect() method works at client side. |
|
It sends the same request and response objects to another servlet. |
It always sends a new request. |
|
It can work within the server only. |
It can be used within and outside the server. |
|
Example: request.getRequestDispacher(“servlet2”).forward(request,response); |
Example: response.sendRedirect(“servlet2”); |
Syntax of sendRedirect() method
- public void sendRedirect(String URL)throws IOException;
Example:
|
In this example, we are redirecting the request to the google server. Notice that sendRedirect method works at client side, that is why we can our request to anywhere. We can send our request within and outside the server. |
DemoServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoServlet extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType(“text/html”);
PrintWriter pw=res.getWriter();
response.sendRedirect(“http://www.google.com”);
pw.close();
}}
Creating custom google search using sendRedirect
In this example, we are using sendRedirect method to send request to google server with the request data.
index.html
<html><head>
<title>sendRedirect example</title>
</head><body>
<form action=”MySearcher”>
<input type=”text” name=”name”>
<input type=”submit” value=”Google Search”>
</form></body></html>
MySearcher.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySearcher extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter(“name”);
response.sendRedirect(“https://www.google.co.in/#q=”+name);
}
}