• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JSP Redirect

January 20, 2014 //  by Krishna Srinivasan

  • Java EE Tutorials
  • JSP Tutorials
  • Recommended Books for Java Server Pages (JSP)

Redirect is used to move or redirect response to another resource. The resource may be servlet , jsp or html file. In this tutorial we will see an example of how to redirect in JSP. We can do PageRedirect using the sendRedirect() method, which works at client side. It always sends a new request. It can be used within and outside the server.

sendRedirect() Syntax

Public void sendRedirect(String url)throws IOException

sendRedirect Example

This example performs following things:

  • Write hello.html which contains forms to enter username and password. Enter userid and password and click on submit button. The control will be forwarded to pageredirectexample.jsp
  • In pageredirectexample.jsp, a if username=”hello” and password=”world”, then the page is redirected to success.html
  • If the username and password do not match then control goes failure.html.

Listing 1: hello.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="pageredirectexample.jsp" method="POST">
Enter Name: <input type="text" name="name"><br />
Enter password: <input type="password" name="pwd" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

Listing 2: pageredirectexample.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page Redirect Example</title>
</head>
<body>
<%
    String name = request.getParameter("name");
    String password = request.getParameter("pwd");
    if(name.equals("hello") && password.equals("world"))
    {
       response.sendRedirect("success.html");
    }
    else
    {
        response.sendRedirect("failure.html");
     }
        %>
</body>
</html>

Listing 3: success.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Successful Login</title>
</head>
<body>
<h2>Welcome!!!!!.Successful login</h2>
</body>
</html>

Listing 4: failure.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Failure Login</title>
</head>
<body>
Sorry...name and password are not correct!!!!!
</body>
</html>

Execute the hello.html. Right click on hello.html and select Run > Run As. Following output would be seen:

JSP Redirect 1Enter correct user name password and following success screen would appear.
jsp_pageredirect_2

Enter invalid user name password and following failure screen would appear.
jsp_pageredirect_3

Previous Tutorial :  JSP Implicit Objects   ||  Next Tutorial : JSP API

Category: Java EETag: JSP Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « OCPJP 6 Mock Exam -15
Next Post: JSP API »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Implement getActiveCount() Method of ThreadPoolExeceutor in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact