博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSP的登陆验证
阅读量:5342 次
发布时间:2019-06-15

本文共 10618 字,大约阅读时间需要 35 分钟。

<%@ 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

欢迎注册

姓名 ${user.error.username }
地址 ${user.error.address }
邮箱 ${user.error.email}
年龄
生日 ${user.error.birthday}
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); }}

 

转载于:https://www.cnblogs.com/jiangchanglin/p/9432799.html

你可能感兴趣的文章
mysql统计一张表中条目个数的方法
查看>>
ArcGIS多面体(multipatch)解析——引
查看>>
css3渐变画斜线 demo
查看>>
JS性能DOM优化
查看>>
设计模式 单例模式 使用模板及智能指针
查看>>
HAL层三类函数及其作用
查看>>
Odoo 去掉 恼人的 "上午"和"下午"
查看>>
web@h,c小总结
查看>>
java编程思想笔记(一)——面向对象导论
查看>>
Data Structure 基本概念
查看>>
Ubuntu改坏sudoers后无法使用sudo的解决办法
查看>>
NEYC 2017 游记
查看>>
[搬运] 写给 C# 开发人员的函数式编程
查看>>
Python之旅Day14 JQuery部分
查看>>
core--线程池
查看>>
redux-effect
查看>>
Swift和OC混编
查看>>
Android轻量级的开源缓存框架ASimpleCache
查看>>
他山之石:加载图片的一个小问题
查看>>
shell - 常识
查看>>