Ajax获取action数据

一、Ajax类

public class AjaxSystemAction extends Action {

	private static final Logger logger = Logger.getLogger(AjaxSystemAction.class);
	
	public AjaxSystemAction() {
		
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		
		String method = StringUtils.trim(request.getParameter("method"));
		
		if("getUserIdCookie".equals(method)){
			getUserIdCookie(request, response);
		}
		
		return null;
	}

	private void getUserIdCookie(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		Cookie[] cookies = request.getCookies();
		PrintWriter out = null;
		String result = "";
		if(cookies != null){
			for(int i=0;i


二、jsp中使用ajax

createXMLHttpRequest();
var method = 'getUserIdCookie';
xmlHttp.open("POST", '/trustWeb/AjaxSystemAction.do?method='+method, false);
xmlHttp.onreadystatechange = callbackGetUserIdCookie;
xmlHttp.send(null);


三、返回调用方法

function callbackGetUserIdCookie(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			var result = xmlHttp.responseText;
			if(result){
				document.getElementById("userId").value = result;
				document.getElementById("pwd").focus();
			}
		}
	}
}




你可能感兴趣的:(Java)