How getRequestDispatcher will work for servlet in

2019-08-09 01:57发布

问题:

In servlet we can use requestdispatcher and it will forward from one servlet to another servlet with same session in same project.but if i am using different project getRequestDispatcher is not working.its giving 404 error.because its appending servlet name before the url. how can i achieve getRequestDispatcher in different project in same server?

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

In same project getRequestDispatcher working fine. but in different project its not working ?can anyone explain why its happening ..?

回答1:

You can achieve getRequestDispatcher in different project on same server.

Check out https://stackoverflow.com/a/19273223/1428052

It works on same server for different project by using getServletContext().getContext() method.

You can follow below steps for detail implementation.

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.

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".

Suppose you have two web applications with name InterServletComm1 and InterServletComm2 having servlets Servlet1 and Servlet1 in each web application respectively. Then the code in each servlets goes as follows:

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
    }

}

Above code sends attribute name from InterServletComm1 and it is received in InterServletComm2. Please let me know if this answer is not clear.



回答2:

You have to use the ServletContext of the other web application to get the RequestDispatcher. The other context can be looked up by ServletContext#getContext(String uri):

ServletContext otherContext = request.getServletContext().getContext("/other");
RequestDispatcher rd = otherContext.getRequestDispatcher("/v1/status1/toreply1");