Google News
logo
JSP - Interview Questions
How Do I Perform Browser Redirection From A JSP Page?
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,
<%@ 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>
Advertisement