Eclipse IDE を使用して、$.ajax() を使用して jQuery からサーブレット POST メソッドを呼び出すアプリケーションを開発しました。次に、このアプリケーションをサーバーにデプロイする必要があります。このアプリケーションをEclipse IDEで使用するまでは正常に動作していましたが、すべてのファイルをサーバーにコピーすると、jQueryがajaxリクエストでエラーを出しています。そこで、Eclipse IDE を使用せずに ajax からサーブレットを呼び出すために従うべきすべてのことを知りたかったのです。これは、私がEclipseで使用していて、正しく機能している私のコードです。このファイルをサーバーで適切に動作させるには、どのような変更を行う必要があるかを知る必要があります。
私のhtmlコード:
<!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>
jQuery と $.ajax() 呼び出しを含む私の JavaScript ファイル
$(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();
}
};
私のサーブレットコード:
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");
}
}
私の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_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>
サーバー上で ajax 呼び出しを介してサーブレットを実行するために必要なことを教えてください。
ありがとうございました