Java如何使用 转发到其他页面?
该<jsp:forward/>标签转发用户的请求到其它页面。例如,一个用户请求page1.jsp,服务器在此页面中找到<jsp:forwardpage="page2.jsp"/>。服务器立即停止对的处理page1.jsp并跳转到page2.jsp。
让我们看一个使用<jsp:forward/>标签的例子。
page1.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Page 1</title>
</head>
<body>
<strong>This is page 1</strong>
<jsp:forward page="page2.jsp"/>
</body>
</html>page2.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Page 2</title>
</head>
<body>
<strong>This is page 2</strong>
</body>
</html>当您尝试通过访问URL运行上述示例时,http://localhost:8080/forward/page1.jsp您将看到的内容,page2.jsp而不是page1.jsp。这是因为在服务器端page1.jsp将您的请求转发到page2.jsp。但是,如果您查看浏览器的URL地址,它将仍然指向page1.jsp。
这是我们示例的目录结构:
.
├── pom.xml
└── src
└── main
└── webapp
├── WEB-INF
│ └── web.xml
└── forward
├── page1.jsp
└── page2.jsp该web.xml配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- Config here. -->
</web-app>