转发从一个servlet到另一个warlet中的servlet的请求

时间:2015-01-16 22:16:40

标签: java jsp java-ee servlets websphere

我有一个EAR1。这个EAR1里面有一个WAR1,带有' / war1'的上下文。这个war1有一个servlet,其url-mapping为' / servlet1'在里面。

在WebSphere的同一个实例上,我有一个EAR2。这个EAR2里面有一个WAR2,带有' / war2'的上下文。这个war2有一个servlet url-mapping of' / servlet2'在里面。

EAR1---- WAR 1 --- Servlet1
EAR2---- WAR 2 --- Servlet2

现在,我需要将请求从servlet1转发到servlet 2 ...所以,在servlet1内部,我试图以这种方式转发请求..

getServletContext().getRequestDispatcher("/war2/servlet2").forward(request, response);

但它失败了..

如何将请求转发给位于EAR2中的war2中的servlet2。

提前致谢

3 个答案:

答案 0 :(得分:0)

我怀疑由于RequestDispatcher存在于ServletContext上,因此您无法转发到其他servlet。你得到什么错误?

它在这里说:

http://java.boot.by/wcd-guide/ch03s05.html

  

" ServletContext.getRequestDispatcher方法接受一个String   描述ServletContext范围内路径的参数。"

有关更多信息,请参阅此帖子:

ServletContext.getRequestDispatcher() vs ServletRequest.getRequestDispatcher()

答案 1 :(得分:0)

根据您的说明,您有两个war文件(war1war2),因此这意味着您在同一服务器中拥有two个上下文。

为了在不同的环境中访问资源,您可以在下面进行操作。

ServletContext ctx = request.getServletContext().getContext("/war2");
RequestDispatcher dispatcher = ctx.getRequestDispatcher("/servlet2");
dispatcher.forward(request, response);

答案 2 :(得分:0)

我使用 HttpUrlConnection 进行如下操作:

// variable methodType is GET or POST in method call below

private void sendToAnotherServletInDifferentWar(HttpServletRequest request, HttpServletResponse response, String methodType) throws IOException
    {
        HttpURLConnection conn = null;
       
        // replace/modify the path as per the path you want to hit.

        String pathToAnotherServlet = "http://localhost:8080/<war2-context>/<servlet-path>/<path-Info>";
        final URL url = new URL(pathToAnotherServlet);

        try
        {
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod(methodType);

            final Enumeration<String> headers = request.getHeaderNames();
            while (headers.hasMoreElements())
            {
                final String header = headers.nextElement();
                final Enumeration<String> values = request.getHeaders(header);
                while (values.hasMoreElements())
                {
                    final String value = values.nextElement();
                    System.out.println("header-> " + header + ": value-> " + value);
                    conn.addRequestProperty(header, value);
                }
            }

            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(false);
            conn.connect();

            final byte[] buffer = new byte[16*1024];

            response.setStatus(conn.getResponseCode());
            System.out.println("HTTP Response Code obtained from service " + conn.getResponseCode());
            if(conn.getResponseCode() == HttpServletResponse.SC_OK)
            {
                for (int i = 0; ; ++i)
                {
                    final String header = conn.getHeaderFieldKey(i);
                    if (header == null)
                        break;
                    final String value = conn.getHeaderField(i);
                    response.setHeader(header, value);
                }

                while (true)
                {
                    final int read = conn.getInputStream().read(buffer);
                    if (read <= 0)
                        break;
                    response.getOutputStream().write(buffer, 0, read);
                }
            }
            else {
                response.sendError(conn.getResponseCode(), conn.getResponseMessage());
            }
        }
        catch (IOException e)
        {
            logger.error("Calling service Failed with cause ", e);
            if(conn != null)
                response.sendError(conn.getResponseCode(), e.getMessage());
            else
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }