1.创建一个表设计
CREATE TABLE `customer` ( `cid` int(11) NOT NULL AUTO_INCREMENT, `cname` varchar(255) DEFAULT NULL, `pass` varchar(255) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `status` int(11) DEFAULT NULL, `code` varchar(255) DEFAULT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
2.注册页面
<div class="lowin lowin-blue"> <div class="lowin-wrapper"> <div class="lowin-box lowin-login"> <div class="lowin-box-inner"> <form action="costomer?action=register" method="post"> <H1>注册页面</h1> <div class="lowin-group"> <label>用户名:</label> <input type="text" name="cname" class="lowin-input"> </div> <div class="lowin-group password-group"> <label>密码:</label> <input type="password" name="pass" class="lowin-input"> </div> <div class="lowin-group password-group"> <label>邮箱:</label> <input type="email" name="email" class="lowin-input"> </div> <input type="submit" value="注册" class="lowin-btn login-btn"> </form> </div> </div>
3.封装一个javaBean
private int cid;private String cname;private String pass;private String email;private int status; //状态码private String code;//激活码public int getCid() {return cid;}public void setCid(int cid) {this.cid = cid;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public String getPass() {return pass;}public void setPass(String pass) {this.pass = pass;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public int getStatus() {return status;}public void setStatus(int status) {this.status = status;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public Costomer() {super();// TODO Auto-generated constructor stub}public Costomer(int cid, String cname, String pass, String email, int status, String code) {super();this.cid = cid;this.cname = cname;this.pass = pass;this.email = email;this.status = status;this.code = code;}@Overridepublic String toString() {return "Costomer [cid=" + cid + ", cname=" + cname + ", pass=" + pass + ", email=" + email + ", status="+ status + ", code=" + code + "]"; }
4.根据mvc模式的开发
4.1需要让BaserServlet继承于HttpServlet
第三步:根据请求携带的方法参数值,再通过字节码对象获取指定的方法
@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置编码格式request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");//获取action base?action=addString methodName = request.getParameter("action");if (methodName==null) {throw new RuntimeException("方法不存在");}Method method=null;try {method = this.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);} catch (Exception e) {throw new RuntimeException("方法不存在"+methodName);}try {String result=(String) method.invoke(this,request,response);if (result!=null && !result.trim().isEmpty()) {int indexOf = result.indexOf(":");if (indexOf==-1) {request.getRequestDispatcher(result).forward(request, response);}else {String[] split = result.split(":");String prefix = split[0];String path = split[1];if ("f".equals(prefix)) {request.getRequestDispatcher(path).forward(request, response);}else if ("r".equals(prefix)) {response.sendRedirect(request.getContextPath()+"/"+path);}}} }
5.注册用户
//用户注册 public String register(HttpServletRequest req, HttpServletResponse resp){ //获取表单数据便且映射到对象中 Costomer costomer= ConvertBeanUtils.toBean(req.getParameterMap(), Costomer.class); //补全其他信息 costomer.setStatus(0);//表示未激活 costomer.setCode(UUiduitile.Uuid64());//激活码 //调用service实现用户注册 req.setAttribute("cname", costomer.getCname()); boolean b=s.register(costomer); if(b){ return "r:success.jsp"; }else{ return "r:error.jsp"; } }
6.发邮件的封装
public class Emaile_Uitile { static Properties props=new Properties(); static{ try { InputStream resourceAsStream = MyEmaiuitile.class.getClassLoader().getResourceAsStream("Email.properties"); props.load(resourceAsStream); props.setProperty("mail.host",props.getProperty("host")); props.setProperty("mail.smtp.auth", props.getProperty("auth")); props.setProperty("mail.transport", props.getProperty("transport")); } catch (IOException e) { e.printStackTrace(); } } /** * @param to 发给谁 * @param subject 主题内容 * @param content 发送的内容 * @return 是否发送成功 */ public static boolean Emaiuitile(String to,String subject,String content ){ try { Authenticator authenticator=new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(props.getProperty("userName"), props.getProperty("password")); } }; // 获取连接对象 Session s = Session.getInstance(props, authenticator); MimeMessage m = new MimeMessage(s); // 设置发送者 m.setFrom(new InternetAddress(props.getProperty("userName"))); // 设置标题 m.setSubject(subject); // 设置接收者 m.setRecipients(RecipientType.TO, to); // 设置内容 m.setContent(content, "text/html;charset=utf-8"); // 发送 Transport.send(m); return true; } catch (Exception e) { return false; } }
7.发送邮件
public class CostomerserviceImpl implements Costomerservice { CostomerDao dao=new CostomerDaoImpl(); @Override public boolean register(Costomer costomer) { boolean b=dao.register(costomer); if(b){ //发送邮件 boolean emaiuitile = Emaile_Uitile.Emaiuitile(costomer.getEmail(), "注册成功 ", "恭喜"+costomer.getCname()+"注册成功,请点击以下链接进行激活<br><a href='http://"+IpUitile.getIp()+":8080/MyEmai/costomer?action=active&code="+costomer.getCode()+"'>立即激活</a>" ); return emaiuitile; } return false; }
8.dao层的实现层
public class CostomerDaoImpl implements CostomerDao { QueryRunner run=new QueryRunner(MyPoolUtils.getDataSource()); @Override public boolean register(Costomer costomer) { try { String sql="insert into customer values(null,?,?,?,?,?)"; int update = run.update(sql, costomer.getCname(),costomer.getPass(),costomer.getEmail(),costomer.getStatus(),costomer.getCode()); return update>0?true:false; } catch (SQLException e) { throw new RuntimeException(e.getMessage()); } }
9.servlet激活
//用户激活的方法 public String active(HttpServletRequest req, HttpServletResponse resp){ //获取传递过来的激活码 String code = req.getParameter("code"); //调用根据激活码查询用户对象 Costomer costomer = s.active(code); if(costomer!=null){ //修改用户的状态码和清空激活码 costomer.setStatus(1);//1表示已激活 costomer.setCode(null);//清空激活码 //调用修改状态码已及激活码的业务层 boolean b=s.updateCostomer(costomer); //如果激活成功 if(b){ //跳转到登录界面 return "r:login.jsp"; }else{ //修改失败 return "r:error.jsp?msg=fail"; } }else{ //激活成功,切勿重复激活 return "r:error.jsp?msg=repeat"; } }