Ajax获取action数据

前端之家收集整理的这篇文章主要介绍了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<cookies.length;i++){
				Cookie cookie = cookies[i];
				if("username".equals(cookie.getName())){
					result = cookie.getValue();
					try {
						out = response.getWriter();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					out.print(result);
				}
			}
		}
		if(out != null)
			out.close();
	}
	
}


二、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();
			}
		}
	}
}
原文链接:https://www.f2er.com/ajax/161934.html

猜你在找的Ajax相关文章