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