I used eclipse IDE to develop an application which calls servlet POST method from jQuery using $.ajax(). Now I need to deploy this application on server. Untill I use this application in eclipse IDE it was working fine but when I copied all the files to server the jQuery is giving error in ajax request. So I wanted to know what are all the things we should follow in order to call servlet from ajax without using eclipse IDE. Here are my codes which I am using in eclipse and which working correctly. I need to know what modification should I make in order to make this files work properly in server.
My html code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Demo</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
<script type="text/javascript" src="newJs.js"></script>
</head>
<body>
<button type="button" id="search-button" >button</button>
</body>
</html>
My javascript file which contains jQuery an $.ajax() call
$(function(){
alert("loaded");
page.init();
});
var page = {
addEvent:function(){
alert("here");
page.controls.searchButton.click(page.handlers.onSearchButtonClick);
},
handlers:{
onSearchButtonClick: function(){
alert("hi");
var msg = [1,2,3,4];
$.ajax({
url : "simpleServer",
data : {
msg:msg
},
type:"POST",
success : function(data){
alert("called "+data);
},
error: function(xhr, status){
alert("error : "+status);
},
complete: function(xhr, status){
alert("complete"+status);
}
});
}
},
controls:{
},
init: function(){
page.controls.searchButton = $('#search-button');
page.addEvent();
}
};
My servlet code :
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/simpleServer")
public class simpleServer extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] rcvData = request.getParameterValues("msg[]");
System.out.println("post method "+rcvData.length);
for(int i=0;i<rcvData.length;i++){
System.out.println(rcvData[i]);
}
PrintWriter pw = response.getWriter();
pw.write(" Hi welcome to you \n\n");
}
}
and my web.xml file is
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>simpleServer</servlet-name>
<servlet-class>simpleServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simpleServer</servlet-name>
<url-pattern>/simpleServer</url-pattern>
</servlet-mapping>
</web-app>
Please let me know what I should in order to run servlet through ajax call on server.
Thank you