加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax获取action数据

发布时间:2020-12-16 01:30:59 所属栏目:百科 来源:网络整理
导读:一、Ajax类 public class AjaxSystemAction extends Action {private static final Logger logger = Logger.getLogger(AjaxSystemAction.class);public AjaxSystemAction() {}@Overridepublic ActionForward execute(ActionMapping mapping,ActionForm form,

一、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();
			}
		}
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读