前言:
模拟我们日常使用网页使用账号密码进行登录网页,验证用户名和密码
用户数据表:
drop table if exists t_user; create table t_user( id int(10) primary key auto_increment, username varchar(32) not null unique, password varchar(32) not null, realname varchar(128) ); insert into t_user(username,password,realname) values('zhangzhixi','zhixi158','root'); insert into t_user(username,password,realname) values('zhangsan','123','普通用户'); select * from t_user;
Maven的依赖:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--servlet依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <!--jsp依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> </dependency> <!--mysql-jdbc依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> </dependencies>
JSP页面:登录页面-成功页面-失败页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/login" method="post"> 账号 <input type="text" name="username"><br> 密码 <input type="password" name="password"><br> <input type="submit" value="登录"> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 欢迎您:
欢迎您:${pageContext.request.getAttribute("user")}用户
用户 </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 登录失败,请检查用户名密码是否正确 </body> </html>
Servlet验证:
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.sql.*; /** * @author zhangzhixi */ public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解决乱码问题 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); //获取用户在登录页面输入的值 String username = request.getParameter("username"); String password = request.getParameter("password"); PrintWriter out = response.getWriter(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { //1、注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2、获取连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/donglijiedian", "root", "zhixi158"); //3、获取预编译的数据库操作对象 String sql = "select * from t_user where username=? and password=?"; ps = conn.prepareStatement(sql); ps.setString(1, username); ps.setString(2, password); //4、执行sql rs = ps.executeQuery(); //5、处理查询结果 if (rs.next()) { //有数据代表有这个用户 String realname = rs.getString("realname"); request.setAttribute("user", realname); request.getRequestDispatcher("welcome.jsp").forward(request, response); } else { //没有这个用户 System.out.println("没有这个用户"); request.getRequestDispatcher("error.jsp").forward(request, response); } } catch (Exception e) { e.printStackTrace(); } finally { //6、释放资源 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }