package ljh.flows.web.servlet;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import ljh.flows.entity.ShopCart;
import ljh.flows.entity.User;
import ljh.flows.service.MyException;
import ljh.flows.service.UserService;
import ljh.tools.MD5.DigestMD5;
import ljh.tools.commoms.CommonUtils;
import ljh.tools.servlet.BaseServlet;
import ljh.tools.vcode.VerifyCode;
public class UserServlet extends BaseServlet {
private UserService service = new UserService();
/**
* 注册
* @param request
* @param response
* @return
* @throws ServletException
* @throws IOException
*/
public String regist(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User form = CommonUtils.toBean(request.getParameterMap(),User.class);
form.setU_id(CommonUtils.uuid());
form.setTimes(new Date());
// 校验
String username = form.getU_username();
if (username == null || username.trim().isEmpty()) {
//errors.put("u_username", "用户名不能为空!");
request.setAttribute("name", "用户名不可为空!");
} else if (username.length() < 3 || username.length() > 10) {
//errors.put("u_username", "用户名长度必须在3~10之间!");
}
String password = form.getU_password();
if (password == null || password.trim().isEmpty()) {
//errors.put("u_password", "密码不能为空!");
request.setAttribute("pass", "密码不可为空!");
} else if (password.length() < 3 || password.length() > 10) {
//errors.put("u_password", "密码长度必须在3~10之间!");
}
String email = form.getU_email();
if (email == null || email.trim().isEmpty()) {
//errors.put("u_email", "Email不能为空!");
request.setAttribute("email","邮箱不可为空!");
} else if (!email.matches("\\w+@\\w+\\.\\w+")) {
// errors.put("u_email", "Email格式错误!");
request.setAttribute("email","邮箱格式错误!");
}
String ver = (String) request.getSession().getAttribute("session_ver");
String fver = form.getU_verity();
if (fver == null || fver.trim().isEmpty()) {
//errors.put("u_verify", "验证码不可为空");
request.setAttribute("u_verify","验证码不可为空!");
request.setAttribute("form", form);
return "f:/jsps/user/regist.jsp";
} else if (!ver.equalsIgnoreCase(fver)) {
//errors.put("u_verify", "验证码错误!");
request.setAttribute("u_verify","验证码错误!");
request.setAttribute("form", form);
return "f:/jsps/user/regist.jsp";
}
/*
if (errors.size() > 0) {
request.setAttribute("errors", errors);
request.setAttribute("form", form);
return "f:/jsps/user/regist.jsp";
}*/
try {
service.regist(form);
request.setAttribute("msg", "恭喜,注册成功!");
return "f:/jsps/msg.jsp";
} catch (MyException e) {
request.setAttribute("msg", e.getMessage());
request.setAttribute("form", form);
return "f:/jsps/user/regist.jsp";//转发
}
}
/**
* 登录
*/
public String login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User form = CommonUtils.toBean(request.getParameterMap(), User.class);
try {
User user = service.login(form);
request.getSession().setAttribute("session_us",user);
//创建shopcart
request.getSession().setAttribute("cart",new ShopCart());
return "r:/index.jsp";
} catch (MyException e) {
request.setAttribute("msg", e.getMessage());
request.setAttribute("form", form);
return "f:/jsps/user/login.jsp";
}
}
/**
* 获取验证码
*/
public void verity(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//实体化对象
VerifyCode vc = new VerifyCode();
BufferedImage codeimage = vc.getImage();
request.getSession().setAttribute("session_ver", vc.getText());
VerifyCode.output(codeimage, response.getOutputStream());
}
/**
* 退出功能
*/
public String quit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getSession().invalidate();
return "r:/index.jsp";
}
/**
* 信息修改
*/
public String edit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = CommonUtils.toBean(request.getParameterMap(),User.class);
service.edit(user);
request.setAttribute("change","修改成功!");
return "f:/jsps/user/info.jsp";
}
/**
* 修改密码
* @throws NoSuchAlgorithmException
*/
/* public String psedit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, NoSuchAlgorithmException {
User form = CommonUtils.toBean(request.getParameterMap(),User.class);
System.out.println(form.getU_id());
String pass = DigestMD5.getEncryptedPwd(form.getU_password());
form.setU_password(pass);
form.toString();
service.psedit(form);
request.setAttribute("change","密码修改成功,请牢记!");
return "f:/jsps/user/info.jsp";
}*/
public String psedit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, NoSuchAlgorithmException {
User form = CommonUtils.toBean(request.getParameterMap(),User.class);
User us = service.findById(form.getU_id());
if(!DigestMD5.validPasswd(form.getU_password(),us.getU_password())){
request.setAttribute("msg","原密码输入错误!");
return "f:/jsps/user/passedit.jsp";
}else{
String pass = DigestMD5.getEncryptedPwd(form.getU_repassword());
form.setU_password(pass);
service.psedit(form);
request.setAttribute("change","密码修改成功,请牢记!");
return "f:/jsps/user/info.jsp";
}
}
/**
* 查询密码
* @throws NoSuchAlgorithmException
*/
public String findpass(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, NoSuchAlgorithmException {
User form = CommonUtils.toBean(request.getParameterMap(),User.class);
User us = service.findById(form.getU_id());
if(!DigestMD5.validPasswd(form.getU_password(),us.getU_password() )){
request.setAttribute("pass","密码错误!");
}
return "f:/jsps/user/passedit.jsp";
}
/**
* 设置密保
*/
public String setPropass(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User form = CommonUtils.toBean(request.getParameterMap(),User.class);
service.setPropass(form);
request.setAttribute("form",form);
request.setAttribute("success","密保设置成功,请牢记!");
return "f:/jsps/user/setpass.jsp";
}
/**
* 检测用户名
* @throws NoSuchAlgorithmException
*/
public String loa(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, NoSuchAlgorithmException {
User form = CommonUtils.toBean(request.getParameterMap(),User.class);
System.out.println(form.getU_username());
User us = service.findByName(form.getU_username());
if(form.getKeypass().equals(us.getKeypass())){
String pass = UUID.randomUUID().toString().substring(0,6);
System.out.println(pass);
us.setU_password(DigestMD5.getEncryptedPwd(pass));
service.psedit(us);
request.setAttribute("ps",pass);
request.setAttribute("success","请登陆后修改密码,密码已重置为:");
return "f:/jsps/user/getpass.jsp";
}else{
request.setAttribute("passname",us.getU_username());
request.setA