1

在servlet中,我们可以使用requestdispatcher,它会从一个servlet转发到同一个项目中具有相同会话的另一个servlet。但是如果我使用不同的项目getRequestDispatcher不起作用。它给出404错误。因为它在url之前附加了servlet名称。如何在同一服务器的不同项目中实现 getRequestDispatcher?

RequestDispatcher rd = request.getRequestDispatcher("/v1/status1/toreply1"); 
rd.forward(request, response);

在同一个项目中 getRequestDispatcher 工作正常。但在不同的项目中它不起作用?谁能解释它为什么会发生..?

4

2 回答 2

3

您可以在同一服务器上的不同项目中实现 getRequestDispatcher。

查看https://stackoverflow.com/a/19273223/1428052

它通过使用 getServletContext().getContext()方法在不同项目的同一服务器上工作。

您可以按照以下步骤进行详细实施。

首先,您需要在以下文件中进行更改

(Windows) C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\context.xml 将 crossContext 的值设置为 true。

上下文.xml

<Context crossContext="true">

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

请注意crossContext="true"

假设您有两个名为InterServletComm1InterServletComm2的 Web 应用程序,每个 Web 应用程序中分别 有 Servlet Servlet1Servlet1。然后每个 servlet 中的代码如下:

Servlet1.java

package interServletComm1;

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;

/**
 * Servlet implementation class Servlet1
 */
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        request.setAttribute("name", "WebApp1");
        ServletContext context = getServletContext().getContext("/InterServletComm2");
        RequestDispatcher rd = context.getRequestDispatcher("/Servlet2");
        rd.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

Servlet2.java

package interServletComm2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Servlet2
 */
@WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();
        String name = (String) request.getAttribute("name");
        pw.println("This is web application 2.");
        pw.println("<br>The value received from web application one is: " + name);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

上面的代码从InterServletComm1发送属性名称,并在InterServletComm2中接收。如果这个答案不清楚,请告诉我。

于 2013-11-23T06:59:31.093 回答
1

You have to use the ServletContext of the other web application to get the RequestDispatcher. The other context can be looked up by ServletContext#getContext(String uri):

ServletContext otherContext = request.getServletContext().getContext("/other");
RequestDispatcher rd = otherContext.getRequestDispatcher("/v1/status1/toreply1");
于 2013-11-22T07:35:36.037 回答