探究方式有二:
- jsp页面
- servlet实例
目录:
a. 探究过程及源代码
b. 最后总结
**
a.探究过程及源代码
**
1.jsp探究
原理方法:
创建4张jsp页面,index.jsp、f1.jsp、f2.jsp、end.jsp。
index.jsp:首页链入f1.jsp、f2.jsp。
f1.jsp:jsp转发页面。
f2.jsp:jsp重定向页面。
end.jsp:转发、重定向的最终页面。
源代码:
index.jsp 代码如下(写在了一起):
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<a href="f1.jsp" >request.jsp转发</a><br/>
<a href="f2.jsp" >response.jsp重定向</a><br/>
<a href="serreq" >request.servlet转发</a><br/>
<a href="serresp" >response.servlet重定向</a><br/>
</body>
</html>
f1.jsp 代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'f1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%request.getRequestDispatcher("end.jsp").forward(request, response); %>
</body>
</html>
f2.jsp 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'f2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%response.sendRedirect("end.jsp"); %>
</body>
</html>
end.jsp 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'end.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my end JSP page. <br>
</body>
</html>
首先运行项目。之后在页面单击,你将会看到效果。
重定向后的URL:
请求转发后的URL:
比较结果:请求转发的URL是不变的。重定向的URL是改变的。
2.servlet探究
原理:通过对index.jsp中的超链接单击,测试servlet的请求转发与重定向。
源代码:
ServletRequest.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletResponse extends HttpServlet {
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.sendRedirect(request.getContextPath()+"/end.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void init() throws ServletException {
}
}
ServletResponse.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletRequest extends HttpServlet {
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/end.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void init() throws ServletException {
}
}
web.xml配置(插入代码在之间):
<servlet>
<servlet-name>serresp</servlet-name>
<servlet-class>servletForR.ServletResponse</servlet-class>
</servlet>
<servlet>
<servlet-name>serreq</servlet-name>
<servlet-class>servletForR.ServletRequest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>serresp</servlet-name>
<url-pattern>/serresp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>serreq</servlet-name>
<url-pattern>/serreq</url-pattern>
</servlet-mapping>
servlet请求转发结果:
servlet重定向结果:
比较:servlet的请求转发URL不变,重定向URL改变。
b.最后总结
请求转发的URL是不改变的,重定向后的URL是改变的。
可是? servlet中的转发与重定向的URl是否可以随意?
请看第二篇。URL的探究