简体   繁体   中英

Ajax Function Not Calling Servlet

I am calling an Ajax javascript function from my jsp, which in turn should load a servlet for further processing. I am getting the values from jsp to ajax, but the servlet is not being called. Upon searching up & down the internet, I could not figure out the missing link.

Here is my jsp where I am calling the ajax javascript function:

<display:column title="Merge Print"><a href="#" onClick="printMerge('arg1', 'arg2')">Click Here</a></display:column>

On a separate ajax.js file, I have the following code:

function printMerge(arg1, arg2) {
alert('In printMerge '+arg1, arg2);
new Ajax.Request('servlet/PrintMerge', {
    method: 'post',
    parameters: { arg1: arg1.value, arg2: arg2.value },
onSuccess: function(transport) {
    var response = transport.responseText || "no response text";
    if(response =='success') {
          alert('RESPONSE: SUCCESS');
          reloadPage();
    } else {
          alert('RESPONSE: ERROR');
    },
onFailure: function() { alert('FAILURE'); }
});
}

On the first alert, the two arguments are displayed properly. Hence, the jsp is properly calling the function, and the parameters are passed normally. However, the process stops there, and it never goes to the 'PrintMerge' servlet for further processing.

Here is the PrintMerge servlet:

public class PrintMerge extends HttpServlet {
    private static final long serialVersionUID = 1L

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String resp = serviceCall;
      response.getWriter().write(resp);
    }
}

Here is my web.xml:

<servlet>
     <servlet-name>PrintMerge</servlet-name>
     <servlet-class>com.servlet.PrintMerge</servlet-class>
</servlet>
<servlet-mapping>
     <servlet-name>PrintMerge</servlet-name>
     <url-pattern>/servlet/PrintMerge</url-pattern>
</servlet-mapping>

Similar configuration is working for other scenarios. However, I feel I am missing something because the ajax function is not accessing the servlet.

Let me know if I can provide something else to visualize the problem better. Thanks in advance.

Try out calling the full qualified name of target servlet instead of servlet/PrintMerge , and have another try, such as http://[::1]/my_ctx/servlet/PrintMerge

For some cases you need to specify the response content-type of the response with servlet, like following.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/plain;charset=UTF-8");
      //String resp = serviceCall;
      try(PrintWriter out=response.getWriter()){out.write(serviceCall);}
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM