Ajax簡單例子(JSP)
編輯:AJAX詳解  
首先是JSP頁面和腳本,為了方便寫在一個裡面
這是一個很常見的檢測用戶名是否存在的功能
這裡用的是struts
<%@ page contentType="text/Html; charset=GBK" %>
<Html>
<head>
<title>
AJax
</title>
</head>
<body bgcolor="#ffffff">
<h1>
<input name="username" type="text" maxlength="20" />
<input id="chk-name-btn" type="button" value="檢測帳號" />
<div id="vIEw_name"></div>
</h1>
</body>
</Html>
<script language="Javascript">
if (window.ActiveXObject && !window.XMLHttpRequest) {
window.XMLHttpRequest=function() {
return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf(’msIE 5’) != -1) ? ’Microsoft.XMLHTTP’ : ’Msxml2.XMLHTTP’);
};
}//取得XMLHttpRequest對象
function testName(path){
//path是取得系統路徑
var view_name=document.getElementById("vIEw_name");
var req=new XMLHttpRequest();
if (req) {
req.onreadystatechange=function() {
if (req.readyState==4 && req.status==200) {//判斷狀態,4是已發送,200已完成
if(req.responseText==0){
vIEw_name.style.color=’green’;
vIEw_name.innerHtml=’該用戶名可以正常使用’;
}else if(req.responseText==1){
vIEw_name.style.color=’red’;
vIEw_name.innerHtml=’該用戶名已經被使用’;
}else{
vIEw_name.style.color=’red’;
vIEw_name.innerHtml=’該用戶名含有非法字符!’;
}
}
}
req.open(’POST’, path+’/AJax.do’);//struts
//req.open(’POST’, path+’/AJax.servlet’);//servlet
//req.open(’POST’, path+’/AJax.action’);//webwork
req.setRequestHeader(’Content-Type’, ’application/x-www-form-urlencoded’);
req.send("");//發送參數如果有參數req.send("username="+user_name);用request取得
}
}
</script>
這個JSP頁面並沒有取得用戶名,就是演示一下。還有<div>可以換span,具體作用問美工吧。
接受AJax請求的action。
import org.apache.struts.action.*;
import Javax.servlet.http.*;
import Javax.servlet.http.HttpServlet;
import Javax.servlet.http.HttpServletRequest;
import Javax.servlet.http.HttpServletResponse;
import Java.io.PrintWriter;
/**
* <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.getWriter();
out.print(1);//AJax取得都是字符的輸出。如果數據量大的話,還可以用XML來發送和接受
return null;
}
}
struts-config.XML
<action type="test.whw.upload.AjaxAction" validate="false" scope="request" path="/AJax"/>
----------------------------
如果是servlet
web.XML
<servlet>
<servlet-name>AJaxServlet </servlet-name>
<servlet-class>servlet.AJaxServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AJaxServlet </servlet-name>
<url-pattern>/AJaxServlet .servlet</url-pattern>
</servlet-mapping>
AJaxServlet.Java
import Java.io.IOException;
import Java.io.PrintWriter;
import Javax.servlet.ServletException;
import Javax.servlet.http.HttpServlet;
import Javax.servlet.http.HttpServletRequest;
import Javax.servlet.http.HttpServletResponse;
public class AJaxServlet extends HttpServlet {
Logger log = Logger.getLogger(this.getClass());
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
response.setContentType("text/XML; charset=GBK");
PrintWriter out = response.getWriter();
out.print(2);
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
//Process the HTTP Put request
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//Process the HTTP Delete request
public void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
//Clean up resources
public void destroy() {
}
}
-------------------------------------
如果是webwork
xwork.XML
<action name="ajax" class="com.whw.upload.action.webwork.AjaxAction" method="AJax"/>
AJaxAction.Java
import Java.io.PrintWriter;
public class AjexAlbuMaction extends ActionSupport implements Action{
public void AJax()throws IOException {
PrintWriter pw = ServletActionContext.getResponse().getWriter();
ServletActionContext.getResponse().setContentType("text/Html;charset=GBK");
pw.print(1);
pw.close();
}
}
本例子在winXPsp2、JB9、Eclipse3,j2sdk1.4.1 、Tomcat5、Tomcat4.1都能運行