sireesha j sireesha j - 6 months ago 40
Ajax Question

How to call a servlet from a jQuery's $.ajax() function?

I am trying to call a servlet from jQuery's .ajax() function.Ajax call does not go to servlet,when i call this function s.o.p statement is not printed in console, Any ideas?For Any help thanks in advance.

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
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>



Answer

you have to add it in web.xml as below

<?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>
Comments