登陆案例_BeanUtils基本使用自己BeanUtils介绍
BeanUtils工具类,简化数据封装:
导入jar包:commons-beanutils-1.8.0.jar
LoginServlet:
@WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.设置编码 req.setCharacterEncoding("utf-8"); //2.获取请求参数 String username = req.getParameter("username"); String password = req.getParameter("password"); //3.封装user对象 User loginUser = new User(); loginUser.setUsername(username); loginUser.setPassword(password); //2.获取所有请求参数 // Map<String, String[]> map = req.getParameterMap(); // //3.创建user对象 // User loginUser = new User(); // //3.2使用BeanUtils封装 // try { // BeanUtils.populate(loginUser, map); // } catch (IllegalAccessException e) { // throw new RuntimeException(e); // } catch (InvocationTargetException e) { // throw new RuntimeException(e); // } //4.调用UserDao的login方法 UserDao dao = new UserDao(); User user = dao.login(loginUser); //5.判断user if (user == null){ //登陆失败 req.getRequestDispatcher("/failServlet").forward(req, resp); }else { //登陆成功 //存储数据 req.setAttribute("user", user); //转发 req.getRequestDispatcher("/successServlet").forward(req, resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
用于封装JavaBean的
1、JavaBean:标准的Java类
1、要求:
1、类必须被public修饰
2、必须提供空参的构造器
3、成员变量必须使用private修饰
4、提供公共setter和getter方法
2、功能:封装数据
2、概念:
成员变量:
属性:setter和getter方法截取后的产物
例如:getUsername() --> Username --> username
3、方法:
1、setProperty();
2、getProperty();
3、populate(Object obj,Map map):将map集合的键值对信息,封装到对应的JavaBean对象中
public class BeanutilsTest { @Test public void test(){ User user = new User(); try { BeanUtils.setProperty(user,"username","张三"); String username = BeanUtils.getProperty(user, "username"); System.out.println(username); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } System.out.println(user); } }
标签:username,req,User,案例,登陆,loginUser,BeanUtils,user From: https://www.cnblogs.com/qihaokuan/p/16953675.html