Servlets - 3
Servlets - 3
It sends the same request and response It always sends a new request.
objects to another servlet.
It can work within the server only. It can be used within and outside
the server.
Example: Example:
request.getRequestDispacher("servlet2") response.sendRedirect("servlet2")
.forward(request,response); ;
• In this example, we are redirecting the request
to the google.com.
• 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.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
res.sendRedirect("http://www.google.com");
pw.close();
}}
Example
• Creating custom google search using
sendRedirect
• In this example, we are using sendRedirect
method to send request to google server with
the request data.
Html file
<html>
<body>
<form action="MySearcher">
<input type="text" name="name">
<input type="submit" value="Google Search">
</form>
</body>
</html>
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
String name=request.getParameter("name");
response.sendRedirect("https://www.google.co.in/#q="+name);
}
}
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.
1. public void forward(ServletRequest
request,ServletResponse response)throws
ServletException,java.io.IOException
RequestDispatcher rd=request.getRequestDispatcher
("servlet2");
//servlet2 is the url-pattern of the second servlet
rd.forward(request, response);
Example
• 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!
Following files are used
• 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.
Index.html
<html>
<form action="servlet1" method="post">
Name:<input type="text" name="userName"/>
<br/>
Password:<input type="password" name="userPass"/>
<br/>
<input type="submit" value="login"/>
</form>
</html>
Login.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
String p=request.getParameter("userPass");
Login.java…
if(p.equals("servlet"){
RequestDispatcher rd=request.getRequestDispatcher("servlet2");
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.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
}
web.xml
<web-app>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
• You can also use setStatus() and setHeader()
methods together to achieve redirection.
import java.io.*;
import java.sql.Date;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PageRedirect extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html");
<servlet-mapping>
<servlet-name>PageRedirect</servlet-name>
<url-pattern>/PageRedirect</url-pattern>
</servlet-mapping>
....