在servlet中我们可以使用requestdispatcher,它将从一个servlet转发到另一个servlet,在同一个项目中具有相同的会话。但是如果我使用不同的项目,则getRequestDispatcher不起作用。给出404错误。因为它附加的servlet名称在网址。如何在同一台服务器的不同项目中实现getRequestDispatcher?
RequestDispatcher rd = request.getRequestDispatcher("/v1/status1/toreply1");
rd.forward(request, response);
在同一项目中getRequestDispatcher正常工作。但是在不同的项目中它不起作用?任何人都可以解释它为什么会发生......?
答案 0 :(得分:3)
您可以在同一台服务器上的不同项目中实现getRequestDispatcher。
查看https://stackoverflow.com/a/19273223/1428052
通过使用它可以在不同项目的同一台服务器上运行 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 :(得分:1)
您必须使用其他网络应用程序的ServletContext
来获取RequestDispatcher
。可以通过ServletContext#getContext(String uri)
:
ServletContext otherContext = request.getServletContext().getContext("/other");
RequestDispatcher rd = otherContext.getRequestDispatcher("/v1/status1/toreply1");