15

全部,

我有 2 个 Web 应用程序,Web1 和 Web2,部署在我的 tomcat 服务器上。我希望 Web1 中的类调用 Web2 中的类的方法。一种方法是使用网络服务。有没有其他方法类似于在同一个 Web 应用程序上调用类上的方法?

谢谢。

4

5 回答 5

11

是的。有可能的。它通过使用getServletContext().getContext()方法尝试相同的 servlet 容器 。

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

(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-10-09T13:44:41.967 回答
5

刚刚搜索了一些文章,上面的场景当然可以CrossContext在Tomcat中使用切换。

在 context.xml 中设置以下元素<Context crossContext="true">

然后getServletContext().getContext("/Web2");

不过还没试过。

于 2012-01-20T09:22:23.640 回答
2

是的,您可以使用javax.servlet.ServletContextjavax.servlet.RequestDispatcher API 来做到这一点。这是如何从 Web1 完成的:

ServletContext otherContext = servletContext.getContex("/Web2");
RequestDispatcher dispathcer = otherContext.getRequestDispatcher("/a/b.jsp");

dispatcher.forward(request, response);
//or
dispatcher.include(request, response);
于 2012-01-21T06:09:30.393 回答
1

将要在 Web 应用程序之间共享的类打包到单独的 jar 中。将它们放在 common/lib 下,以便公共类加载器将加载类并可供两个 Web 应用程序使用。然后使用 jndi 在 web2 中公开实例,从 web1 中查找它们并调用方法。

于 2012-12-02T15:13:47.587 回答
0

几乎没有那么简单。您可以将 app1 中的类共享和导入到 app2 中,但也许它们都与其他类相关联。所以这个想法不是很好,除了小服务,比如用于进行计算的 bean。人们如此多地使用网络服务是有原因的;)。

于 2012-01-20T09:10:01.333 回答