简体   繁体   English

Ajax函数不调用Servlet

[英]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. 我正在从我的jsp调用Ajax javascript函数,这反过来应该加载servlet进行进一步处理。 I am getting the values from jsp to ajax, but the servlet is not being called. 我从jsp到ajax获取值,但是未调用servlet。 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: 这是我在jsp中调用ajax javascript函数的地方:

<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: 在一个单独的ajax.js文件中,我有以下代码:

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. 因此,jsp正确地调用了该函数,并且参数已正常传递。 However, the process stops there, and it never goes to the 'PrintMerge' servlet for further processing. 但是,该过程到此为止,并且永远不会进入“ PrintMerge” servlet进行进一步处理。

Here is the PrintMerge servlet: 这是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: 这是我的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. 但是,我感觉缺少了一些东西,因为ajax函数无法访问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 尝试调用目标servlet的全限定名而不是servlet/PrintMerge ,然后尝试一下,例如http://[::1]/my_ctx/servlet/PrintMerge

For some cases you need to specify the response content-type of the response with servlet, like following. 在某些情况下,您需要使用servlet指定响应的响应内容类型,如下所示。

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);}
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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