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