0% found this document useful (0 votes)
51 views27 pages

Servlets - 3

The document discusses page redirection in servlets. Page redirection is used to send the client to a new location other than requested, often when a resource moves. The simplest way is to use the sendRedirect() method, which works on the client-side and sends a new request to the given URL. Forwarding works on the server-side and sends the same request object. Other topics covered include using sendRedirect() to redirect to external URLs, and an example of validating a login and forwarding or redirecting based on the result.

Uploaded by

Ritiesh Bhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views27 pages

Servlets - 3

The document discusses page redirection in servlets. Page redirection is used to send the client to a new location other than requested, often when a resource moves. The simplest way is to use the sendRedirect() method, which works on the client-side and sends a new request to the given URL. Forwarding works on the server-side and sends the same request object. Other topics covered include using sendRedirect() to redirect to external URLs, and an example of validating a login and forwarding or redirecting based on the result.

Uploaded by

Ritiesh Bhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Servlets - Page Redirection

• Page redirection is a technique where the


client is sent to a new location other than
requested.
• Page redirection is generally used when a
document moves to a new location or may be
because of load balancing.
• The simplest way of redirecting a request to
another page is using
method sendRedirect() of response object.
• Following is the signature of this method −
• public void
HttpServletResponse.sendRedirect(String
location) throws IOException
• The sendRedirect() method
of HttpServletResponse 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.
Difference between forward() and
sendRedirect() method
• There are many differences between the
forward() method of RequestDispatcher and
sendRedirect() method of
HttpServletResponse interface.
forward() method sendRedirect() method

The forward() method works at server The sendRedirect() method works


side. at client side.

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.*;

public class DemoServlet extends HttpServlet{


public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();

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;

public class MySearcher extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletRespon
se response)
throws ServletException, IOException {

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

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.
• The getRequestDispatcher() method of
ServletRequest interface returns the object of
RequestDispatcher.
• Syntax of getRequestDispatcher method

public RequestDispatcher getRequestDispatcher


(String resource);
• Example of using getRequestDispatcher method

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.*;

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");
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.*;

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);
}
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");

String site = new String("http://www.akgec.in");


response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site); } }
web.xml
....
<servlet>
<servlet-name>PageRedirect</servlet-name>
<servlet-class>PageRedirect</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>PageRedirect</servlet-name>
<url-pattern>/PageRedirect</url-pattern>
</servlet-mapping>
....

You might also like