- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is JSP page redirection?
Page redirection is generally used when a document moves to a new location and we need to send the client 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 −
public 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); ....
This example shows how a JSP performs page redirection to an another location −
Example
<%@ 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.
- Related Articles
- Explain page redirection in ES6?
- What is a page object in JSP?
- What is a page directive in JSP?
- What is an exception Object in JSP? What type of exceptions can occur in a JSP page?
- What is the use of page object in JSP? Need an example.
- What are various attributes Of page directive in JSP?
- How to avoid Java code in jsp page?
- How a JSP page works. Can somebody explains the JSP architecture in simpler terms
- How to write a comment in a JSP page?
- How to create a common error page using JSP?
- How to refresh a JSP page at regular interval?
- I want to use %> literal in JSP page. But it is throwing error. How to escape this syntax in JSP?
- Redirection in PHP
- How can I create custom tag in JSP which can accept attribute from parent jsp page?
- How to write a for loop in a JSP page?
