Ajax很簡單(jsp例子)
首先是jsp頁面和腳本
這是一個很常見的檢測用戶名是否存在的功能
這裡用的是struts
<%@ page contentType=
<html>
<head>
<title>
ajax
</title>
</head>
<body bgcolor=
<h
<input name=
<input id=
<div id=
</h
</body>
</html>
<script language=
if (window
window
return new ActiveXObject((navigator
};
}//取得XMLHttpRequest對象
function testName(path){
//path是取得系統路徑
var view_name=document
var req=new XMLHttpRequest();
if (req) {
req
if (req
if(req
lor=
view_name
}else if(req
lor=
view_name
}else{
lor=
view_name
}
}
}
req
//req
//req
req
req
}
}
</script>
這個jsp頁面並沒有取得用戶名
接受ajax請求的action
import org
import javax
import javax
import javax
import javax
import java
/**
* <p>Title:AjaxAction </p>
*/
public class AjaxAction extends Action {
public ActionForward execute(ActionMapping mapping
ActionForm form
HttpServletRequest request
HttpServletResponse response)
throws Exception {
PrintWriter out = response
out
return null;
}
}
struts
<action type=
----------------------------
如果是servlet
web
<servlet>
<servlet
<servlet
</servlet>
<servlet
<servlet
<url
</servlet
AjaxServlet
import java
import java
import javax
import javax
import javax
import javax
public class AjaxServlet extends HttpServlet {
Logger log = Logger
public void doGet(HttpServletRequest request
throws ServletException
response
PrintWriter out = response
out
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request
throws ServletException
doGet(request
}
//Process the HTTP Put request
public void doPut(HttpServletRequest request
throws ServletException
}
//Process the HTTP Delete request
public void doDelete(HttpServletRequest request
HttpServletResponse response) throws ServletException
}
//Clean up resources
public void destroy() {
}
}
-------------------------------------
如果是webwork
xwork
<action name=
AjaxAction
import java
public class AjexAlbumAction extends ActionSupport implements Action{
public void ajax()throws IOException {
PrintWriter pw = ServletActionContext
ServletActionContext
pw
pw
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26032.html