在web应用之间转发请求

5 篇文章 0 订阅

web应用之间转发请求

  1. 步骤
    • 由于每个web应用都配备了一个大管家—-ServletContext,我们可以获得这个web应用的管家来转发请求到这个web应用的某个ServletJSPHTML文档。
    • 由于从一个web应用访问另一个web应用资源存在安全问题,所以默认是不能访问的。我们可以在web应用的目录META-INF下创建一个context.xml文件,指定crossContext的值为true,即可访问。它的值默认是false。
    • 具体到代码,要获得一个web应用的ServletContext,可以使用ServletContext对象的getContext(String uripath)方法,uripath指定其他web应用的URL入口。
  2. 代码

    • 使用eclipse新建两个Dynamin Web Project项目,名称是TestServletDispatcher,分别新建两个类CrossServletOutputServlet,都继承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);
        }
    }
    
    • 两个Servletweb.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>
    • 配置CrossServletcontext.xml文件

      • web应用TestServletwebContent下的META_INF目录创建context.xml文件,文件内容如下:
      <?xml version="1.0" encoding="UTF-8"?>
      <context reloadable="true" crossContext="true"></context>
    • reloadable="true"的意思是让Tomcat监测文件是否改动,如果有改动,自动重新加载。

    • crossContext="true"表示可以访问另一个web资源
    • 所有弄好之后,项目的文件如下图所示:
      这里写图片描述
  3. 运行
    • eclipse运行Tomcat在浏览器输入localhost:8080/TestServlet/CrossServlet,即可访.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值