<script type="text/javascript"> $(document).ready(function(){ //如果名为rmbUser的cookie已存在
if ($.cookie("rmbUser") == "true") { //set checked property to true $("#ck_rmbUser").prop("checked", true); // 记住用户名密码checkbox打钩
$("#username").val($.cookie("username")); //将cookie中的用户名值赋予id为“username”的input标签。 $("#password").remove(); //将输入密码的输入框移除,
$("#pass").append("<input type=\'password\' name=\'password\' id=\'password\' class=\'normal\' maxlength=\'30\'/>"); $("#password").val($.cookie("password")); } $("#loginButton").click(function(){ // 点击id为loginButton的标签调用login方法
login(); }); }); //记住用户名密码 function save() { //保存cookie的方法
if ($("#ck_rmbUser").prop("checked")) { //判断记住用户名密码标签是不是被选择! var username = $("#username").val(); var password = $("#password").val(); $.cookie("rmbUser", "true", { expires: 7 }); //存储一个带7天期限的cookie $.cookie("username", username, { expires: 7 }); $.cookie("password", password, { expires: 7 }); }else{ $.cookie("rmbUser", "false", { expire: -1 }); $.cookie("username", "", { expires: -1 }); $.cookie("password", "", { expires: -1 }); } }; function login(){ var dat={username:$("#username").val(),password:$("#password").val()}; $.ajax({ type:"POST", url: "logonAction_execute.action", data:dat, dataType:"text", success:function(data){ //data为action方法返回的值。 if(data =="successed"){ save(); location.href = "/platform/indexAction.action"; } else { var tip=document.createTextNode("账号或密码错误"); var info=document.getElementById("error_info"); $("#error_info").empty(); info.appendChild(tip); } } }); } </script>
以上为实现记住用户名和密码的jquery代码;
action代码:
public String execute() throws Exception{ AjaxUtil ajaxUtil = new AjaxUtil(); if(user == null ) user= new User(); user.setUsername(ServletActionContext.getRequest().getParameter("username")); user.setPassword(ServletActionContext.getRequest().getParameter("password")); if (user.getUsername()!=null && !"".equals(user.getUsername().trim())) {//鐢ㄦ埛鍚嶅瘑鐮佷笉涓虹┖ if(userService.validate(user.getUsername(), user.getPassword())) {//鐢ㄦ埛鍚嶅瘑鐮佹纭� System.out.println("logon succeed!"); //灏嗙敤鎴峰悕淇濆瓨鍒皊ession涓� String username = userService.finduser(user.getUsername()).getUsername(); String realname = userService.finduser(user.getUsername()).getRealName(); User user1= userService.finduser(user.getUsername()); ActionContext.getContext().getSession().put("user",user1 ); ActionContext.getContext().getSession().put("username",username ); ActionContext.getContext().getSession().put("realname",realname ); /*HttpServletResponse response = ServletActionContext.getResponse(); response.getWriter().write("success");*/ System.out.println(user.getRealName()); /**操作日志 */ LogTable log = new LogTable(); HttpServletRequest request = ServletActionContext.getRequest(); log.setCreatedate(new Date()); log.setOper("登录园区数字化平台"); log.setUserip(request.getRemoteAddr()); log.setOperator(user.getUsername()); log.setExplorerinfo(request.getHeader("user-agent")); logTableService.save(log); return ajaxUtil.ajaxText("successed"); } } return ajaxUtil.ajaxText("input"); }
jsp页面代码:
<body> <div class="top"> <div class="header"> <div class="logo"> </div> </div> </div> <div class="content"> <div class="login"> <div class="ad"> </div> <form> <table> <caption>用户登录</caption> <tr> <th> </th> <td><font id="error_info" color="red"> </font></td> </tr> <tr> <th> 用户名: </th> <td> <input type="text" name="username" id="username" class="normal" maxlength="30" dataType="Require" /> </td> </tr> <tr> <th> 密 码: </th> <td id="pass"> <input type="password" id="password"name="password" class="normal" maxlength="30" dataType="Require" msg="请输入密码" /> </td> </tr> <tr> <th></th> <td> <input name="btn_login" type="button" class="btn_login" value="登录" id="loginButton" /> </td> </tr> <tr> <th></th> <td> <label><input type="checkbox" name="ck_rmbUser" id="ck_rmbUser"/>记住用户名和密码</label> </td> </tr> <tr> <th> </th> <td >服务电话:3485233</td> </tr> </table> </form> </div> </div> <div class="clear"></div> <div class="footer"> <div class="friend"> </div> </div> </body>
jquery.cookie中的使用操作:
jquery.cookie.js是一个基于jquery的插件,点击下载!
创建一个会话cookie:
$.cookie(‘cookieName’,\'cookieValue’);
注:当没有指明cookie时间时,所创建的cookie有效期默认到用户浏览器关闭止,故被称为会话cookie。
创建一个持久cookie:
$.cookie(‘cookieName’,\'cookieValue’,{expires:7});
注:当指明时间时,故称为持久cookie,并且有效时间为天。
创建一个持久并带有效路径的cookie:
$.cookie(‘cookieName’,\'cookieValue’,{expires:7,path:’/\'});
注:如果不设置有效路径,在默认情况下,只能在cookie设置当前页面读取该cookie,cookie的路径用于设置能够读取cookie的顶级目录。
创建一个持久并带有效路径和域名的cookie:
$.cookie(‘cookieName’,\'cookieValue’,{expires:7,path:’/\',domain: ‘chuhoo.com’,secure: false,raw:false});
注:domain:创建cookie所在网页所拥有的域名;secure:默认是false,如果为true,cookie的传输协议需为https;raw:默认为false,读取和写入时候自动进行编码和解码(使用encodeURIComponent编码,使用decodeURIComponent解码),关闭这个功能,请设置为true。
获取cookie:
$.cookie(‘cookieName’); //如果存在则返回cookieValue,否则返回null。
删除cookie:
$.cookie(‘cookieName’,null);
注:如果想删除一个带有效路径的cookie,如下:$.cookie(‘cookieName’,null,{path:’/\'});
JQuery中$.ajax()方法参数详解:
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址。
type: 要求为String类型的参数,请求方式(post或get)默认为get。注意其他http请求方法,例如put和
delete也可以使用,但仅部分浏览器支持。
timeout: 要求为Number类型的参数,设置请求超时时间(毫秒)。此设置将覆盖$.ajaxSetup()方法的全局设
置。
async:要求为Boolean类型的参数,默认设置为true,所有请求均为异步请求。
如果需要发送同步请求,请将此选项设置为false。注意,同步请求将锁住浏览器,用户其他操作必须等
待请求完成才可以执行。
cache:要求为Boolean类型的参数,默认为true(当dataType为script时,默认为false)。
设置为false将不会从浏览器缓存中加载请求信息。
data: 要求为Object或String类型的参数,发送到服务器的数据。如果已经不是字符串,将自动转换为字符串格
式。get请求中将附加在url后。防止这种自动转换,可以查看processData选项。对象必须为key/value格
式,例如{foo1:"bar1",foo2:"bar2"}转换为&foo1=bar1&foo2=bar2。如果是数组,JQuery将自动为不同
值对应同一个名称。例如{foo:["bar1","bar2"]}转换为&foo=bar1&foo=bar2。
dataType: 要求为String类型的参数,预期服务器返回的数据类型。如果不指定,JQuery将自动根据http包mime
信息返回responseXML或responseText,并作为回调函数参数传递。
可用的类型如下:
xml:返回XML文档,可用JQuery处理。
html:返回纯文本HTML信息;包含的script标签会在插入DOM时执行。
script:返回纯文本JavaScript代码。不会自动缓存结果。除非设置了cache参数。注意在远程请求
时(不在同一个域下),所有post请求都将转为get请求。
json:返回JSON数据。
jsonp:JSONP格式。使用SONP形式调用函数时,例如myurl?callback=?,JQuery将自动替换后一个
“?”为正确的函数名,以执行回调函数。
text:返回纯文本字符串。
beforeSend:要求为Function类型的参数,发送请求前可以修改XMLHttpRequest对象的函数,例如添加自定义
HTTP头。在beforeSend中如果返回false可以取消本次ajax请求。XMLHttpRequest对象是惟一的参数。
function(XMLHttpRequest){
this; //调用本次ajax请求时传递的options参数
}
complete:要求为Function类型的参数,请求完成后调用的回调函数(请求成功或失败时均调用)。
参数:XMLHttpRequest对象和一个描述成功请求类型的字符串。
function(XMLHttpRequest, textStatus){
this; //调用本次ajax请求时传递的options参数
}
success:要求为Function类型的参数,请求成功后调用的回调函数,有两个参数。
(1)由服务器返回,并根据dataType参数进行处理后的数据。
(2)描述状态的字符串。
function(data, textStatus){
//data可能是xmlDoc、jsonObj、html、text等等
this; //调用本次ajax请求时传递的options参数
error:要求为Function类型的参数,请求失败时被调用的函数。该函数有3个参数,即XMLHttpRequest对象、错误信息、捕获的错误对象(可选)。
ajax事件函数如下:
function(XMLHttpRequest, textStatus, errorThrown){
//通常情况下textStatus和errorThrown只有其中一个包含信息
this; //调用本次ajax请求时传递的options参数
}
contentType:要求为String类型的参数,当发送信息至服务器时,内容编码类型默认为"application/x-www-form-urlencoded"。该默认值适合大多数应用场合。
dataFilter:要求为Function类型的参数,给Ajax返回的原始数据进行预处理的函数。提供data和type两个参数。data是Ajax返回的原始数据,type是调用jQuery.ajax时提供的dataType参数。函数返回的值将由jQuery进一步处理。
function(data, type){
//返回处理后的数据
return data;
}
global:要求为Boolean类型的参数,默认为true。表示是否触发全局ajax事件。设置为false将不会触发全局ajax事件,ajaxStart或ajaxStop可用于控制各种ajax事件。
ifModified:要求为Boolean类型的参数,默认为false。仅在服务器数据改变时获取新数据。服务器数据改变判断的依据是Last-Modified头信息。默认值是false,即忽略头信息。
jsonp:要求为String类型的参数,在一个jsonp请求中重写回调函数的名字。该值用来替代在"callback=?"这种GET或POST请求中URL参数里的"callback"部分,例如
{jsonp:\'onJsonPLoad\'}会导致将"onJsonPLoad=?"传给服务器。
username:要求为String类型的参数,用于响应HTTP访问认证请求的用户名。
password:要求为String类型的参数,用于响应HTTP访问认证请求的密码。
processData:要求为Boolean类型的参数,默认为true。默认情况下,发送的数据将被转换为对象(从技术角度来讲并非字符串)以配合默认内容类型"application/x-www-form-urlencoded"。如果要发送DOM树信息或者其他不希望转换的信息,请设置为false。
scriptCharset:要求为String类型的参数,只有当请求时dataType为"jsonp"或者"script",并且type是GET时才会用于强制修改字符集(charset)。通常在本地和远程的内容编码不同时使用。