一、创建两个jsp文件
jspforward.jsp:实现请求转发
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>forword page</title>
</head>
<body>
<jsp:forward page="welcome.jsp" />
</body>
</html>
welcome.jsp:显示当前时间
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome page</title>
</head>
<body>
你好,欢迎进入首页,当前访问时间是:
<%
out.print(new java.util.Date());
%>
</body>
</html>
二、访问
http://localhost:8080/chapter06/jspforward.jsp
三、分析源代码
jspforward_jsp.java 核心代码如下:
out.write("\r\n");
out.write("<!DOCTYPE html>\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("<title>forword page</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\t");
if (true) {
_jspx_page_context.forward("welcome.jsp");
return;
}
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
调试分析结果如下: