锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
1. ActionServlet璐熻矗Struts鏋舵瀯鐨勬暣涓垵濮嬪寲宸ヤ綔錛屽綋ActionServlet瀹炰緥鎺ュ彈鍒?/span>HTTP璇鋒眰鏃訛紝鍦?/span>doGet()鍜?/span>doPost()鏂規硶涓兘浼氳皟鐢?/span>process()鏂規硶錛?/span>Process()涓張浼氳皟鐢?/span>RequestProcessor鐨?/span>process()鏂規硶銆?/span>
2. RequestProcessor涓殑processActionPerform()璋冪敤action鐨?/span>execute()鏂規硶銆?/span>RequestProcessor鍙互鏍規嵁闇瑕佽繘琛屾墿灞曪紝濡?/span>Spring涓殑org.springframework.web.struts.DelegatingRequestProcessor銆傛墿灞曞悗蹇呴』鍦?/span>Struts鐨?/span>Config涓姞鍏?/span><controller processorClass=
"錛革幾錛革幾錛革幾"/>
<action type="com.ajax.CheckAction"
銆銆scope="request" path="/ajax/check">
銆銆<forward name="success" path="/check.jsp"/>
</action>
璺熸櫘閫氱殑Struts搴旂敤鐨勯厤緗竴鏍鳳紝鍙槸娌℃湁ActionForm鐨勯厤緗?/font>涓嬮潰鏄疉ction綾伙細
package com.ajax;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CheckAction extends Action
{
public final ActionForward execute(ActionMapping mapping, ActionForm form,
銆銆HttpServletRequest request,
銆銆HttpServletResponse response)
throws Exception
{
銆銆System.out.println("haha...............................");
銆銆String username= request.getParameter("username");
銆銆System.out.println(username);
銆銆String retn;
銆銆if("educhina".equals(username)) retn = "Can't use the same name with the old use,pls select a difference...";
銆銆else retn = "congraducation!you can use this name....";
銆銆PrintWriter out=response.getWriter();
銆銆out.write(retn);
銆銆out.close();
銆銆return mapping.findForward("success");
}
public static void main(String[] args)
{
}
}
鎴戜滑鍙互鐪嬪埌閲岄潰鐨勯昏緫璺熶笂渚嬩腑Servlet閲岀殑閫昏緫涓鏍楓傛渶鍚庯紝鎴戜滑鏉ョ湅鐪婮SP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Check.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<script type="text/javascript">
var http_request = false;
function send_request(url) {//鍒濆鍖栥佹寚瀹氬鐞嗗嚱鏁般佸彂閫佽姹傜殑鍑芥暟
http_request = false;
file://寮濮嬪垵濮嬪寲XMLHttpRequest瀵硅薄
if(window.XMLHttpRequest) { file://Mozilla 嫻忚鍣?br>銆銆http_request = new XMLHttpRequest();
銆銆if (http_request.overrideMimeType) {//璁劇疆MiME綾誨埆
銆銆http_request.overrideMimeType('text/xml');
銆銆}
}
else if (window.ActiveXObject) { // IE嫻忚鍣?br>銆銆try {
銆銆http_request = new ActiveXObject("Msxml2.XMLHTTP");
銆銆} catch (e) {
銆銆try {
銆銆http_request = new ActiveXObject("Microsoft.XMLHTTP");
銆銆} catch (e) {}
銆銆}
}
if (!http_request) { // 寮傚父錛屽垱寤哄璞″疄渚嬪け璐?br>銆銆window.alert("涓嶈兘鍒涘緩XMLHttpRequest瀵硅薄瀹炰緥.");
銆銆return false;
}
http_request.onreadystatechange = processRequest;
// 紜畾鍙戦佽姹傜殑鏂瑰紡鍜孶RL浠ュ強鏄惁鍚屾鎵ц涓嬫浠g爜
http_request.open("GET", url, true);
http_request.send(null);
}
// 澶勭悊榪斿洖淇℃伅鐨勫嚱鏁?br> 銆銆function processRequest() {
銆銆if (http_request.readyState == 4) { // 鍒ゆ柇瀵硅薄鐘舵?br> 銆銆if (http_request.status == 200) { // 淇℃伅宸茬粡鎴愬姛榪斿洖錛屽紑濮嬪鐞嗕俊鎭?br> 銆銆alert(http_request.responseText);
銆銆} else { file://欏?/u>闈笉姝e父
銆銆alert("鎮ㄦ墍璇鋒眰鐨勯〉闈㈡湁寮傚父銆?);
銆銆}
銆銆}
銆銆}
function userCheck() {
var f = document.forms[0];
var username = f.username.value;
if(username=="") {
銆銆window.alert("The user name can not be null!");
銆銆f.username.focus();
銆銆return false;
}
else {
銆銆send_request('ajax/check.do?username='+username);
}
}
</script>
<body>
<form name="form1" action="" method="post">
User Name:<input type="text" name="username" value="">
<input type="button" name="check" value="check" onClick="userCheck()">
<input type="submit" name="submit" value="/oblog312/submit">
</form>
</body>
</html>
鎴戜滑鍙互鐪嬪埌錛孞SP鍩烘湰鏄竴鏍風殑錛岄櫎浜嗚鍙戦佺殑url錛歛jax/check.do?username="+username銆?nbsp;