使用ServletContext来启用同一个应用程序中的Servlet之间的通信非常好,我想知道是否有类似的方法来启用来自不同应用程序(部署在同一个Servlet-Container上的servlet)之间的通信?
背景是我有两个可以分发到多个服务器的应用程序。在这种情况下,他们应该通过SOAP进行通信 - 但是如果它们都托管在同一个服务器(和Servlet-Container)中,我希望避免SOAP开销并直接进行通信。
有什么建议吗?
干杯
答案 0 :(得分:1)
此类API存在于Servlet API的第一个版本中。您可以从servlet上下文中找到其他servlet并调用它们。我怀疑你可以从其他Web应用程序调用servlet。
无论如何,此API已弃用,并且不再受支持。原因是引入了EJB。 Servlet只扮演Web前端的角色。它们不应该实现业务逻辑,因此不应该相互通信。
在不同应用程序之间执行通信的现代方法是使用Web服务或EJB,或者如果您使用的是Spring - 它们的远程机制或消息传递。
答案 1 :(得分:0)
我不确定不同的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 中接收。 如果答案不明确,请告诉我。