简体   繁体   English

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

[英]Communication between two web application in same server

All, 所有,

I have 2 web applications, Web1 and Web2, deployed on my tomcat server. 我有两个Web应用程序,Web1和Web2,部署在我的tomcat服务器上。 I want classes in Web1 to call methods on classes in Web2. 我希望Web1中的类在Web2中调用类的方法。 One way to do this is using webservice. 一种方法是使用webservice。 Is there any other way similar to calling a method on class on same web application ?. 有没有其他方法类似于在同一Web应用程序上调用类的方法?

Thanks. 谢谢。

Yes. 是。 It is possible. 有可能的。 It tried for same servlet container by using getServletContext().getContext() method. 它通过使用getServletContext()。getContext()方法尝试使用相同的servlet容器。

First you need to make changes in below file 首先,您需要在下面的文件中进行更改

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

context.xml 的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>

Please note that crossContext="true" . 请注意, crossContext =“true”

Suppose you have two web applications with name InterServletComm1 and InterServletComm2 having servlets Servlet1 and Servlet1 in each web application respectively. 假设您有两个Web应用程序,其名称为InterServletComm1InterServletComm2分别在每个Web应用程序中具有Servlet1Servlet1 Then the code in each servlets goes as follows: 然后每个servlet中的代码如下:

Servlet1.java 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 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
    }

}

Above code sends attribute name from InterServletComm1 and it is received in InterServletComm2 . 上面的代码从InterServletComm1发送属性名称 ,并在InterServletComm2中接收。 Please let me know if this answer is not clear. 如果答案不明确,请告诉我。

Just searched around some articles and the above scenario is certainly possible using CrossContext switching in Tomcat. 刚刚搜索了一些文章,上面的场景肯定可以在Tomcat中使用CrossContext切换。

Set the following element in context.xml in <Context crossContext="true"> <Context crossContext="true">中的context.xml中设置以下元素

And then getServletContext().getContext("/Web2"); 然后是getServletContext().getContext("/Web2"); .

Haven't tried yet, though. 但是还没有尝试过。

Yes you can do it using javax.servlet.ServletContext and javax.servlet.RequestDispatcher API's. 是的,您可以使用javax.servlet.ServletContextjavax.servlet.RequestDispatcher API来完成。 Here it is how it can be done from Web1: 这是它如何从Web1完成:

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

dispatcher.forward(request, response);
//or
dispatcher.include(request, response);

Package the classes you want to share between web applications into a separate jar. 将要在Web应用程序之间共享的类打包到一个单独的jar中。 Put them under common/lib so that the common classloader will load the classes and would be available to both web applications. 将它们放在common / lib下,这样通用类加载器就可以加载这些类,并且可供两个Web应用程序使用。 Then expose the instance in web2 using jndi, look up them from web1 and invoke the methods. 然后使用jndi在web2中公开实例,从web1中查找它们并调用方法。

Pretty much it is not that simple. 几乎不是那么简单。 You can share and import classes from your app1 into app2, but maybe they're all linked with other classes. 您可以将app1中的类共享和导入app2,但也许它们都与其他类链接。 So the idea is not so good except of small services like beans which are used to make calculations for example. 因此,除了用于进行计算的bean之类的小型服务之外,这个想法并不是那么好。 There is a reason ppl are using web services so much ;). ppl有很多原因使用Web服务;)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM