gpt4 book ai didi

java - getRequestDispatcher 如何在同一服务器的不同项目中为 servlet 工作?

转载 作者:行者123 更新时间:2023-11-29 03:27:46 25 4
gpt4 key购买 nike

在 servlet 中,我们可以使用 requestdispatcher,它将从一个 servlet 转发到同一项目中具有相同 session 的另一个 servlet。但是如果我使用不同的项目,getRequestDispatcher 将不起作用。它给出 404 错误。因为它在网址。如何在同一服务器的不同项目中实现 getRequestDispatcher?

RequestDispatcher rd = request.getRequestDispatcher("/v1/status1/toreply1"); 
rd.forward(request, response);

在同一项目中,getRequestDispatcher 工作正常。但在不同的项目中它不起作用?谁能解释为什么会发生..?

最佳答案

您可以在同一台服务器上的不同项目中实现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"

假设您有两个名为 InterServletComm1InterServletComm2 的 Web 应用程序在每个 Web 应用程序中分别具有 servlet Servlet1Servlet1。然后每个 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 中接收。如果这个答案不清楚,请告诉我。

关于java - getRequestDispatcher 如何在同一服务器的不同项目中为 servlet 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com