同一服务器中两个Web应用程序之间的通信

时间:2012-01-20 09:03:41

标签: java tomcat servlets tomcat6 cross-context

所有

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

感谢。

5 个答案:

答案 0 :(得分:10)

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

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

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

<强> context.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”

假设您有两个名为 InterServletComm1 InterServletComm2 的Web应用程序 在每个Web应用程序中分别具有servlet Servlet1 Servlet1 。然后每个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 发送属性 name ,并在 InterServletComm2 中接收。 如果答案不明确,请告诉我。

答案 1 :(得分:5)

只是搜索了一些文章,使用Tomcat中的CrossContext切换肯定可以实现上述场景。

<Context crossContext="true">

中的context.xml中设置以下元素

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

但是还没试过。

答案 2 :(得分:2)

是的,你可以使用 javax.servlet.ServletContext javax.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);

答案 3 :(得分:1)

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

答案 4 :(得分:0)

实际上并非如此简单。您可以将app1中的类共享和导入app2,但也许它们都与其他类链接。因此,除了用于进行计算的bean之类的小型服务之外,这个想法并不是那么好。 ppl有很多原因使用Web服务;)。