JavaWeb_(Struts2框架)使用Servlet实现用户的登陆
JavaWeb_(Struts2框架)使用Struts框架实现用户的登陆 传送门
JavaWeb_(Struts2框架)Servlet与Struts区别 传送门
MySQL数据库中存在Gary用户,密码为123;第一次登陆时输入错误的密码1234后页面重定向,并输出错误的提示信息,第二次登陆时输入正确的密码,页面跳转到index.jsp中
c3p0-config.xml链接本地数据库
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///strutstest</property> <property name="user">root</property> <property name="password">123456</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <named-config name="oracel"> <property name="driverClass">oracle.jdbc.driver.OracleDriver</property> <property name="jdbcUrl">jdbc:oracle:thin:@//192.168.40.128/orcl</property> <property name="user">scott</property> <property name="password">scott</property> </named-config> </c3p0-config>
package com.Gary.web; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; 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 org.apache.commons.beanutils.BeanUtils; import com.Gary.domain.User; import com.Gary.service.UserService; @WebServlet("/login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User user = new User(); //封装user对象 try { BeanUtils.populate(user, request.getParameterMap()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } UserService userService = new UserService(); //传递数据,判断数据库是否有user boolean success = false; try { success = userService.findUser(user); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(success) { //存在用户,登陆成功重定向到index.html response.sendRedirect(request.getContextPath()+"/index.html"); }else { request.setAttribute("error", "用户名或密码错误!!"); //不存在,转发到login.jsp request.getRequestDispatcher("/login.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.Gary.web; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; 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 org.apache.commons.beanutils.BeanUtils; import com.Gary.domain.User; import com.Gary.service.UserService; @WebServlet("/login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User user = new User(); //封装user对象 try { BeanUtils.populate(user, request.getParameterMap()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } UserService userService = new UserService(); //传递数据,判断数据库是否有user boolean success = false; try { success = userService.findUser(user); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(success) { //存在用户,登陆成功重定向到index.html response.sendRedirect(request.getContextPath()+"/index.html"); }else { request.setAttribute("error", "用户名或密码错误!!"); //不存在,转发到login.jsp request.getRequestDispatcher("/login.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.Gary.domain; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.Gary.dao; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import com.Gary.domain.User; import com.yl.lain.utils.C3p0DataSourceUtils; public class UserDao { public User findUser(User user) throws SQLException { QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource()); String sql = "select * from user where username = ? and password = ?"; return runner.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword()); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="css/head.css" /> <link rel="stylesheet" type="text/css" href="css/login.css" /> </head> <body> <div class="dvhead"> <div class="dvlogo"> <a href="index.html">你问我答</a> </div> <div class="dvsearch">10秒钟注册账号,找到你的同学</div> <div class="dvreg"> 已有账号,立即 <a href="login.html">登录</a> </div> </div> <section class="sec"> <form action="${pageContext.request.contextPath }/login" method="post"> <div class="register-box"> <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="username" type="text" placeholder="您的用户名和登录名" /> </label> <div class="tips"></div> </div> <div class="register-box"> <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="password" placeholder="建议至少使用两种字符组合" /> </label> <div class="tips"></div> </div> <div class="arguement"> <input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《你问我答用户注册协议》</a> <a href="register.html">没有账号,立即注册</a> <div class="tips" style="color: red">${error }</div> </div> <div class="submit_btn"> <button type="submit" id="submit_btn">立 即 登录</button> </div> </form> </section> <script src="js/index.js" type="text/javascript" charset="utf-8"></script> </body>
项目结构
数据库中用户数据
实现过程
通过c3p0-config.xml链接本地数据库
<property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///strutstest</property> <property name="user">root</property> <property name="password">123456</property>
web层接受由login.jsp页面的表单/login请求
<form action="${pageContext.request.contextPath }/login" method="post"> <div class="register-box"> <label for="username" class="username_label"> 用 户 名 <input maxlength="20" name="username" type="text" placeholder="您的用户名和登录名" /> </label> <div class="tips"></div> </div> <div class="register-box"> <label for="username" class="other_label"> 设 置 密 码 <input maxlength="20" type="password" name="password" placeholder="建议至少使用两种字符组合" /> </label> <div class="tips"></div> </div> <div class="arguement"> <input type="checkbox" id="xieyi" /> 阅读并同意 <a href="javascript:void(0)">《你问我答用户注册协议》</a> <a href="register.html">没有账号,立即注册</a> <div class="tips" style="color: red">${error }</div> </div> <div class="submit_btn"> <button type="submit" id="submit_btn">立 即 登录</button> </div> </form>
在domain层中创建用户User实体
private String username; private String password;
package com.Gary.domain; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
web层LoginServlet.java处理用户登陆逻辑,由servlet层去调用dao层返回数据库中查询用户信息
public User findUser(User user) throws SQLException { QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource()); String sql = "select * from user where username = ? and password = ?"; return runner.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword()); }
dao层得到用户信息后传递给servlet层,servlet层获得user对象后传递给web层
web层中判断是否有用户信息,如果有,那么用户登陆成功后将页面重定向到index.html,否则跳转到login.jsp并显示错误的提示信息(将error封装到request域中在login.jsp中使用${error}标签将信息提示出来)
web层接受到login的dopost请求后调用doGet请求的逻辑处理
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User user = new User(); //封装user对象 try { BeanUtils.populate(user, request.getParameterMap()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } UserService userService = new UserService(); //传递数据,判断数据库是否有user boolean success = false; try { success = userService.findUser(user); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(success) { //存在用户,登陆成功重定向到index.html response.sendRedirect(request.getContextPath()+"/index.html"); }else { request.setAttribute("error", "用户名或密码错误!!"); //不存在,转发到login.jsp request.getRequestDispatcher("/login.jsp").forward(request, response); } }
(如需转载学习,请标明出处)