在web
应用之间转发请求
- 步骤
- 由于每个
web
应用都配备了一个大管家—-ServletContext
,我们可以获得这个web应用的管家来转发请求到这个web应用的某个Servlet
、JSP
、HTML
文档。 - 由于从一个
web
应用访问另一个web
应用资源存在安全问题,所以默认是不能访问的。我们可以在web
应用的目录META-INF
下创建一个context.xml
文件,指定crossContext
的值为true,即可访问。它的值默认是false。 - 具体到代码,要获得一个
web
应用的ServletContext
,可以使用ServletContext
对象的getContext(String uripath)
方法,uripath
指定其他web
应用的URL
入口。
- 由于每个
代码
使用
eclipse
新建两个Dynamin Web Project
项目,名称是TestServlet
、Dispatcher
,分别新建两个类CrossServlet
、OutputServlet
,都继承HttpServlet
前者将用户请求转发到Dispatcher
应用下的outputServlet
。类代码如下:CrossServlet类:
package com.lovo.visitother; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CrossServlet extends HttpServlet { private static final long serialVersionUID = 1L; public CrossServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("username"); response.setContentType("text/plain;charset=utf-8"); if (name == null) { name = "Please Input Username!"; } else { name = "Hello! " + name; } PrintWriter out = response.getWriter(); request.setAttribute("username", name); ServletContext context = this.getServletContext(); //获取web应用Dispatcher的ServletContext ServletContext crossContext = context.getContext("/Dispatcher"); //获取请求转发的对象 RequestDispatcher patcher = crossContext.getRequestDispatcher("/OutputServlet"); patcher.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
OutputServlet类:
package com.lovo.dispatcher; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class OutputServlet extends HttpServlet { private static final long serialVersionUID = 1L; public OutputServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String msg = (String) request.getAttribute("username"); PrintWriter out = response.getWriter(); out.println(msg); out.write("==================="); out.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
- 两个
Servlet
的web.xml
文件
CrossServlet类的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>TestServlet</display-name> <servlet> <servlet-name>CrossServlet</servlet-name> <servlet-class>com.lovo.visitother.CrossServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CrossServlet</servlet-name> <url-pattern>/TestServlet/CrossServlet</url-pattern> </servlet-mapping> </web-app>
OutputServlet类的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Dispatcher</display-name> <servlet> <servlet-name>OutputServlet</servlet-name> <servlet-class>com.lovo.dispatcher.OutputServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>OutputServlet</servlet-name> <url-pattern>/Dispatcher/OutputServlet</url-pattern> </servlet-mapping> </web-app>
配置
CrossServlet
的context.xml
文件- 在
web
应用TestServlet
的webContent
下的META_INF
目录创建context.xml
文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <context reloadable="true" crossContext="true"></context>
- 在
reloadable="true"
的意思是让Tomcat
监测文件是否改动,如果有改动,自动重新加载。crossContext="true"
表示可以访问另一个web
资源- 所有弄好之后,项目的文件如下图所示:
- 运行
- 在
eclipse
运行Tomcat
在浏览器输入localhost:8080/TestServlet/CrossServlet
,即可访.
- 在