JSP Redirect Example

JSP Redirect Example describes about How To Redirect a JSP page using response.sendRedirect() function

How To Use  JSP SendRedirect?

For redirect a page in JSP, we are calling response.sendRedirect(), By using this method, the server return back the response to the client, from where next request comes and it displays that url.

Here we are using the implicit object "response" to redirect the browser to a different resource.

When calling a sendRedirect method, web container will inform the browser that a new URL is requested. Here browser issues entirely new request so any data that are stored as request attributes will be lost after the redirect happens

If we called sendRedirect() from JSP #1 to JSP #2 the browser's location bar is changed to show JSP #2, since a sendRedirect() is a new request,

Note

You can also find following related tutorials on this blog

1) JSP Include Example 2) JSP Forward Example

JSP Redirect Example

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
    <title>JSP Redirect Example</title>
</head>
<body>
    <%
        String redirectURL = "http://www.javatips.net/";
        response.sendRedirect(redirectURL);
    %>
</body>
</html>


301 Redirect In JSP Example

301 redirect is the most efficient and Search Engine Friendly method for redirecting a web page

For 301 redirect in JSP page simple modify the following code for your own purposes and place it on the page you want to redirect

If you need a permanent redirect, you can't able to use "response.sendredirect()", Because calling this method will override the response status. so you can use permanent redirect following way. 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>JSP 301 Redirect Example</title>
</head>
<body>
    <%
        response.setStatus(301);
        response.setHeader("Location", "http://www.new-url.com/");
        response.setHeader("Connection", "close");
    %>
</body>
</html>

 









1 Responses to "JSP Redirect Example"
  1. abhijeet indalkar 2016-08-16 03:54:46.0

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