JavaBeat

  • Home
  • 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)
  • Privacy
  • Contact Us

JSP Redirect

January 20, 2014 by Krishna Srinivasan Leave a Comment

  • 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

[code lang=”java”]
Public void sendRedirect(String url)throws IOException
[/code]

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

[code lang=”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>
[/code]

Listing 2: pageredirectexample.jsp

[code lang=”html”]
<%@ 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>
[/code]

Listing 3: success.html

[code lang=”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>
[/code]

Listing 4: failure.html

[code lang=”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>
[/code]

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

Filed Under: Java EE Tagged With: 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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved