- JSP - Sending Email
- JSP - Auto Refresh
- JSP - Hits Counter
- JSP - Page Redirect
- JSP - Handling Date
- JSP - File Uploading
- JSP - Session Tracking
- JSP - Cookies Handling
- JSP - Writing Filters
- JSP - Form Processing
- JSP - Http Status Codes
- JSP - Server Response
- JSP - Client Request
- JSP - Implicit Objects
- JSP - Actions
- JSP - Directives
- JSP - Syntax
- JSP - Lifecycle
- JSP - Architecture
- JSP - Environment Setup
- JSP - Overview
- JSP - Home
Advanced JSP Tutorials
- JSP - Internationalization
- JSP - Security
- JSP - Debugging
- JSP - Exception Handling
- JSP - Expression Language
- JSP - Custom Tags
- JSP - Java Beans
- JSP - XML Data
- JSP - Database Access
- JSP - Standard Tag Library
JSP Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JSP - Page Redirecting
In this chapter, we will discuss page redirecting with JSP. Page redirection is generally used when a document moves to a new location and we need to send the cpent to this new location. This can be because of load balancing, or for simple randomization.
The simplest way of redirecting a request to another page is by using sendRedirect() method of response object. Following is the signature of this method −
pubpc void response.sendRedirect(String location) throws IOException
This method sends back the response to the browser along with the status code and new page location. You can also use the setStatus() and the setHeader() methods together to achieve the same redirection example −
.... String site = "http://www.newpage.com" ; response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); ....
Example
This example shows how a JSP performs page redirection to an another location −
<%@ page import = "java.io.*,java.util.*" %> <html> <head> <title>Page Redirection</title> </head> <body> <center> <h1>Page Redirection</h1> </center> <% // New location to be redirected String site = new String("http://www.photofuntoos.com"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); %> </body> </html>
Let us now put the above code in PageRedirect.jsp and call this JSP using the URL http://localhost:8080/PageRedirect.jsp. This would take you to the given URL http://www.photofuntoos.com.
Advertisements