RedirectPage Servlet Example

RedirectPage Servlet Example


Posted in : Java Posted on : June 23, 2011 at 8:43 PM Comments : [ 0 ]

In this example we will discuss how a page can be redirected.

RedirectPage Servlet Example

In this example we will discuss how a page can be redirected.

In the example given below, to accomplish this task I used the sendRedirect() method of HttpServletResponse interface which redirects the client response temporarily where the redirect URL is specified.

Example :

SendRedirect.html

<html>
<head>
<title>Send Redirect Example</title>
</head>
<body>
<form method="get" action="/SendRedirectExample/RedirectServletExample">
<p>Name <input type="text" name="name"></input></p>
<p>Password <input type="text" name="psw"></input></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit" value="submit"></input></p>
</form>
</body>
</html>

RedirectServletExample.java

import java.io.IOException;
import java.util.Enumeration;
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 RedirectServletExample extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
PrintWriter pw = res.getWriter();
res.setContentType("text/html");
if(req.getParameter("name").equals("abc")&& req.getParameter("psw").equals("xyz"))
{
res.sendRedirect("/SendRedirectExample/Welcome");
}
else
{
res.sendError(res.SC_BAD_REQUEST, "Please fill the right information");
}
}
}

Welcome.java

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 Welcome extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println("<html><body>");
pw.println("<h3>Welcome Reader's</h3>");
pw.println("</body></html>");
}
}

web.xml

<web-app>
<display-name>SendRedirectExample</display-name>
<servlet>
<servlet-name>RedirectServletExample</servlet-name>
<servlet-class>RedirectServletExample</servlet-class>
</servlet>
<servlet>
<servlet-name>Welcome</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RedirectServletExample</servlet-name>
<url-pattern>/RedirectServletExample</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Welcome</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
</web-app>

Output :

When you will execute this example you will find the following outputs.

When you will Enter the name and password incorrect then,

When you will enter the correct name and password then,

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics