RequestDispatcher ending in infinite loop

柔情痞子 提交于 2019-12-02 17:21:44

问题


I have two WAR applications and the mode of communication between them is via servlets.

My application (WAR A) opens a child window with the URL of a servlet in another WAR (lets say WAR B).

The servlet (in WAR B) processes the data and should send the processed data back to original application's servlet (i.e WAR A's servlet).

But this process ends in an infinite loop and also the URL parameters sent from WAR-A are null.

Here is the code snippet :

The below script opens a child window with the URL of servlet in WAR-B also passing some URL parameters.

function invokePlugin(invokeURL, custValJSON, meaCompPartJSON) {
    window.open(invokeURL + '?custValJSON=' + custValJSON,'');
}

Below is servlet code in WAR-B which extracts the URL parameters and process the data and again send the request back to WAR-A's servlet...

private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String custValJSON = request.getParameter("custValJSON");
    System.out.println("custValJSON : " + custValJSON);

    CustomValues custVal = gson.fromJson(custValJSON, CustomValues.class);

    if(custVal != null) {
        System.out.println("Cust val details : " + custVal.getName());
        custVal.setValue("Satya");
    }

    String destination = "/testPlannerPluginResult";

    RequestDispatcher reqDispatch = request.getRequestDispatcher(destination);
    request.setAttribute("custValJSON", gson.toJson(custVal));

    if(reqDispatch != null) {
        reqDispatch.forward(request, response);
    }
}

Does anybody have idea on this?

Regards,

Satya


回答1:


That then just means that the servlet is basically calling itself everytime. I don't immediately see the cause in the information given so far, but apparently the URL which you passed into the getRequestDispatcher() matches the URL of the servlet itself.

I however see a major mistake here:

RequestDispatcher reqDispatch = request.getRequestDispatcher(destination);
request.setAttribute("custValJSON", gson.toJson(custVal));

That can impossibly invoke the servlet which runs in another servlet context (read: another WAR). You need ServletContext#getContext() first to get the other servlet context and then use ServletContext#getRequestDispatcher() to dispatch the request to there.

ServletContext otherServletContext = getServletContext().getContext("/otherContextPath");
RequestDispatcher dispatcher = otherServletContext.getRequestDispatcher(destination);

This only requires that the both WARs are configured to expose the context for sharing. On Tomcat for example, this is to be done by adding crossContext="true" to the <Context>.



来源:https://stackoverflow.com/questions/7981026/requestdispatcher-ending-in-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!