Latest web development tutorials

redirecciones de página JSP

Cuando tenga que mover el documento a una nueva ubicación, es necesario utilizar JSP redirección.

La forma más sencilla es utilizar el método de respuesta de redirección objeto sendRedirect (). La firma de este método son los siguientes:

public void response.sendRedirect(String location)
throws IOException 

Este método se indicará el código y la nueva ubicación de la página como una respuesta de vuelta al navegador. También puede utilizar setStatus () y el método setHeader () para obtener el mismo efecto:

....
String site = "http://www.w3big.com" ;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site); 
....

Los ejemplos de demostración

Este ejemplo muestra cómo una página JSP vuelve a dirigir:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<html>
<html>
<head>
<title>页面重定向</title>
</head>
<body>

<h1>页面重定向</h1>

<%
   // 重定向到新地址
   String site = new String("http://www.w3big.com");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site); 
%>

</body>
</html>

PageRedirecting.jsp código anterior se guarda en un archivo y, a continuación, visite http: // localhost: 8080 / PageRedirect.jsp , que le llevará a http://www.w3big.cc/ .