如何从jQuery的$ .ajax()函数调用servlet?

时间:2016-05-27 05:32:23

标签: jquery ajax

我试图从jQuery的.ajax()函数中调用一个servlet.Ajax调用不会去servlet,当我调用这个函数时,sop语句没有打印在控制台中,有什么想法吗?对于任何帮助都要提前感谢。< / p>

This is my code:
<!--Html code:-->
<form id="ajaxform" action="weeklyMarks" method="post">
    <table>
        <td>
            <label id="stForm" for="Marks">Marks Obtained:</label>
            <input type="text" id="stMarks" name="stMarks" style="margin-left: 33px; width: 150px;" />     
            <label id="stMarksError" style="display: none; color: red;">*Please enter marks</label>
            <br>
            <br>
        </td>
        </tr>
        <tr>
            <td>
                <input type="button" value="Add" id="addButton" class="btn-warning" style="margin-left: 200px;"" onclick="addSubjectMarks() ">
            </td>
        </tr>
    </table>
    </div>
    </div>
</form>
//ajax call from jsp page
function addSubjectMarks(){  
 var postData = $(this).serializeArray();
 var formURL = $(this).attr("action");
 var selectedStudent=document.getElementById("selectedStudent").value;
 var selectedSubject=document.getElementById("selectedSubject").value;
 var marks=document.getElementById("stMarks").value;
  $.ajax({  
    type: "POST",  
    url: formURL,  
    data:postData,
    success: function(data,status){  

      alert(status);

    }                
  });  
}       
//servlet is:this servlet having url weeklyMaks
 protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    System.out.println(request.getParameter("stMarks"));
}

my web.xml code is:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
    <servlet-name>week</servlet-name>
    <servlet-class>com.studentmodels.WeeklyMarks</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>week</servlet-name>
    <url-pattern>/weeklyMarks</url-pattern>
</servlet-mapping>

<display-name>Students</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>Home.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<error-page>
    <exception-type>java.lang.exception</exception-type>
    <location>/login.html</location>
</error-page>

2 个答案:

答案 0 :(得分:0)

你必须在web.xml中添加它,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ajaxtry</display-name>
  <welcome-file-list>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
<servlet-name>ProcessForm</servlet-name> //url given in ajax call
<servlet-class>com.ajaxtry.web.ProcesFormServlet</servlet-class>//servlet class path
  </servlet>
   <servlet-mapping>
<servlet-name>ProcessForm</servlet-name> //url given in ajax call
<url-pattern>/ProcessForm</url-pattern> //url given in ajax call
  </servlet-mapping>
</web-app>

答案 1 :(得分:0)

此错误可能有两个原因: 1.如果您在.java文件和web.xml中编写了WebServlet Path,则可能会导致该问题。从任何一个位置删除它都适合您。

如下所示:

在.java文件中

    @WebServlet("/weeklyMarks")

并在web.xml中

<servlet>
    <servlet-name>week</servlet-name>
    <servlet-class>com.studentmodels.WeeklyMarks</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>week</servlet-name>
    <url-pattern>/weeklyMarks</url-pattern>
</servlet-mapping>

但是从你给定的代码我可以看到你只在web.xml中提到过 导致此问题的第二个原因可能如下:

2.javamail.jar丢失了。将javamail.jar添加到tomcat lib文件夹中。或者,如果您的工作空间中有任何Maven项目打开,则通过向pom.xml文件添加以下依赖项,将commons-logging JAR添加到项目中。

    <dependency>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
     <version>1.2</version>
    </dependency>