网上书城项目总结(一)

  • 注册模块

阻止表单提交用 onsubmit=”return checkForm();” 写在form头里面

JQ的fouces()方法 点击输入框获取焦点 点外面失去焦点

JS 的onblur 事件 失去焦点时发生

JS 的onkeyup 事件 在键盘按键被松开时发生

  • 判断邮箱是否存在

Js代码:

Html:

 

<input type=“text” class=“textinput”  id=“email” name=“email” onkeyup=”checkEmail();” onblur=”findEmail();”/>

 

jQuery:

function findEmail(){

$.post(“${pageContext.request.contextPath}/client/user/findEmail.do?email=”+$(“#email”).val(),

function(data){

if(data==”EXIST”){

alert(“邮箱已注册!”);

$(“#email”).focus();

}

 

 

});

}

Handler方法;

@RequestMapping(“/findEmail.do”)

@ResponseBody

public String findEmail(String email){

User user = userService.findEmail(email);

if(user!=null){

return “EXIST”;

}else{

return “OK”;

}

}

  • 激活邮箱
//激活邮箱

@RequestMapping(“/activeUser.do”)

public String activeUser(String activeCode){

int rows=userService.acticeUser(activeCode);

if(rows>0){

return “/client/activesuccess.jsp”;

}else{

return “/client/register.jsp”;

}

}

  • 自动登录
public  User  autologin(HttpServletRequest request) throws UnsupportedEncodingException

{

//保存cookie找到的用户名和密码

String username = null;

String password =null;

User user = new User();

Cookie[] cookies = request.getCookies();

//从cookie查找保存的用户名和密码

for(Cookie cookie : cookies){

if(“bookstore_password”.equals(cookie.getName()))

{

password = URLDecoder.decode(cookie.getValue(),”utf-8″);

 

}

if(“bookstore_username”.equals(cookie.getName()))

{

username = URLDecoder.decode(cookie.getValue(),”utf-8″);

}

}

if(username != null && password != null){

user.setUsername(username);

user.setPassword(password);

}

//判断cookie保存的用户名密码正确与否

User login_user     = userService.findUserByUSernameAndPassword(user);

return   login_user;

 

}

  • 添加cookie
public void addCookie(String flag,User user,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{

Cookie cookie= new Cookie(“bookstore_username” ,URLEncoder.encode(user.getUsername(),”utf-8″));

cookie.setMaxAge(60*60*24*3);

cookie.setPath(request.getContextPath()+”/”);

response.addCookie(cookie);

if(“1”.equals(flag)){

Cookie cookie2= new Cookie(“bookstore_password” ,URLEncoder.encode(user.getPassword(),”utf-8″));

 

cookie2.setMaxAge(60*60*24*3);

cookie2.setPath(request.getContextPath()+”/”);

response.addCookie(cookie2);

 

}

 

  • 退出
@RequestMapping(“/logout.do”)

public String logout(HttpServletResponse response,HttpSession session,HttpServletRequest request){

session.removeAttribute(“login_user”);

Cookie cookie1= new Cookie(“bookstore_username”,null);

Cookie cookie2= new Cookie(“bookstore_password”,null);

cookie1.setPath(request.getContextPath()+”/”);

cookie2.setPath(request.getContextPath()+”/”);

cookie1.setMaxAge(0);

cookie2.setMaxAge(0);

response.addCookie(cookie1);

response.addCookie(cookie2);

return “/client/login.jsp”;

 

}

 

发表评论