<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> index.jsp注册成功!欢迎进入首页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here欢迎注册
package com.pojo;import java.util.Date;public class Users { private String userid; private String username; private String address; private String email; private int userage; private Date birthday; public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getUserage() { return userage; } public void setUserage(int userage) { this.userage = userage; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Users() { super(); } public Users(String userid, String username, String address, String email, int userage, Date birthday) { super(); this.userid = userid; this.username = username; this.address = address; this.email = email; this.userage = userage; this.birthday = birthday; } @Override public String toString() { return "Users [userid=" + userid + ", username=" + username + ", address=" + address + ", email=" + email + ", userage=" + userage + ", birthday=" + birthday + "]"; } }
package com.pojo;import java.util.LinkedHashMap;import java.util.Map;import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;/** * 此类为一个过渡 * 以全String类型接收JSP中传过来的数据 * 处理完业务后返回 * @author lin * */public class UserBean { private String userid; private String username; private String address; private String email; private String userage; private String birthday; //用于存储错误的信息 private Map
error = new LinkedHashMap<>(); @Override public String toString() { return "UserBean [userid=" + userid + ", username=" + username + ", address=" + address + ", email=" + email + ", userage=" + userage + ", birthday=" + birthday + ", error=" + error + "]"; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getUserage() { return userage; } public void setUserage(String userage) { this.userage = userage; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public Map getError() { return error; } public void setError(Map error) { this.error = error; } public UserBean(String userid, String username, String address, String email, String userage, String birthday, Map error) { super(); this.userid = userid; this.username = username; this.address = address; this.email = email; this.userage = userage; this.birthday = birthday; this.error = error; } public UserBean() { super(); } /** * 以下进行对数据的校验 */ public boolean validate(){ boolean isOK=true; if(checkUserName(isOK) & checkEmail(isOK) & checkBirthday(isOK)){ isOK=true; }else{ isOK=false; } return isOK; } /** * 对生日的规则 * @param isOK * @return */ private boolean checkBirthday(boolean isOK) { if(birthday!=null && !birthday.trim().equals("")) { DateLocaleConverter dlc=new DateLocaleConverter(); try{ dlc.convert(birthday,"yyyy-MM-dd"); }catch(Exception e){ System.out.println("转换失败"); isOK=false; error.put("birthday","日期格式不对"); } } return isOK; } /** * 对名称的规则 * @param isOK * @return */ private boolean checkUserName(boolean isOK) { if(username==null || username.trim().equals("")){ isOK=false; error.put("username", "用户名不能为空"); }else{ if(!username.matches("[A-Za-z]{3,8}")){ isOK=false; error.put("username","用户名必须是3到8位字母"); } } return isOK; } /**对邮箱的规则 * @param isOK * @return */ private boolean checkEmail(boolean isOK) { if(email==null || email.trim().equals("")){ isOK=false; error.put("email", "邮箱不能为空"); }else{ if(!email.matches("\\w+@\\w+(\\.\\w+)+")){ isOK=false; error.put("email","邮箱格式不对"); } } return isOK; }} package com.servlets;import java.io.IOException;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Enumeration;import java.util.UUID;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.ConvertUtils;import org.apache.commons.beanutils.Converter;import com.pojo.UserBean;import com.pojo.Users;/** * Servlet implementation class Servlet */@WebServlet("/Servlet")public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet() { } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //将数据进行封装 UserBean requestToBean = requestToBean(request, UserBean.class); if (requestToBean.validate()) { //对传入的数据进行校验,成功则将过度类赋给实体类 Users user2 = new Users(); // BeanUtils可以对数据进行复制,自动数据类型转化 try { //将BeanUtils不能转化的日期类型进行转化 copyBean(user2, requestToBean); //加上ID user2.setUserid(UUID.randomUUID().toString()); System.out.println("对象封装成功!"+user2); response.sendRedirect("/JSP-Servlet.3.3/index.jsp"); } catch (Exception e) { e.printStackTrace(); } } else { // 如果校验不符合规则取出error HttpSession session = request.getSession(); session.setAttribute("user", requestToBean); response.sendRedirect("/JSP-Servlet.3.3/reg.jsp"); } } /** * 日期类型的转化 * @param user2 * @param requestToBean */ private void copyBean(Users user2, UserBean requestToBean) { ConvertUtils.register(new Converter() { // 是把时间类型进行转换 @Override //先判断是否为空 public Object convert(Class type, Object value) { if (value == null) { return null; } String str = (String) value; if (str.trim().equals("")) { return null; } //规定格式 SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); try { return s.parse(str.trim()); } catch (Exception e) { throw new RuntimeException(e); } } }, Date.class); try { BeanUtils.copyProperties(user2, requestToBean); } catch (Exception e) { throw new RuntimeException(e); } } /** * 利用泛型,反射得到实体类中的所有属性,方法 * @param request * @param c * @return */ private T requestToBean(HttpServletRequest request, Class c) { try { //泛型接收 T t = c.newInstance(); //获取请求中的数据 Enumeration e = request.getParameterNames(); //对数据操作 while (e.hasMoreElements()) { String name = e.nextElement(); String value = request.getParameter(name); //setProperty提供三个参数,第一是JavaBean对象,第二个是要操作的属性名,第三个为要设置的具体的值 BeanUtils.setProperty(t, name, value); } return t; } catch (Exception e) { throw new RuntimeException(e); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}