用servlet校验密码2
登录页面
数据库字段
登录失败页面
登录成功页面
项目结构(这里的小红叉可以忽略,IDE反应问题)
login.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> body{ margin:0; } input{ margin:30px auto; width:200px; height:28px; } .border{ width:400px; height:300px; background:url(./img/bg.jpg) no-repeat; margin:200px auto; } .content{ width:300px; height:58px; border-radius:4px; margin: 30px auto; color:#fff; } .commit{ position:absolute; margin-top:30px; left:720px; background:#3385ff; } .btnSubmit{ line-height:30px; font-size:16px; width:100px; background:#3385ff; border:0; } </style> </head> <body> <div class="border"> <form action="./login" method="POST"> <div class="content"> 用户名:<input type="text" name="username" placeholder="请输入用户名"><br/> </div> <div class="content"> 密 码:<input type="password" name="password" placeholder="请输入密码"><br/> </div> <div class="commit"> <button type="submit" class="btnSubmit" style="">提交</button> </div> </form> </div> </body> </html>
LoginServlet.java
package com.my.deal; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 登录界面 */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private String driverName="com.mysql.cj.jdbc.Driver"; private String url="jdbc:mysql://localhost:3306/login?useSSL=false&serverTimezone=UTC"; private String name="root"; private String word="root"; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); //设置页面编码格式 response.setContentType("text/html;charset=UTF-8"); String sql="select * from user where username='"+username+"' and password='"+password+"'"; Connection connection=null; Statement statement=null; ResultSet resultSet=null; try { Class.forName(driverName);//加载驱动 connection = DriverManager.getConnection(url, name, word);//获取连接 statement = connection.createStatement(); resultSet = statement.executeQuery(sql); if(resultSet.next()) { response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("欢迎你:"+username+"登录成功来到首页"); response.getWriter().write("<br/>"); response.getWriter().write("你的密码为:"+password); }else { response.getWriter().write("用户名或者密码错误"); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); }finally{ try { //关闭连接 resultSet.close(); statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
源码下载地址
链接:https://pan.baidu.com/s/1MjXb7nGpASnYjHStVjyBmA
提取码:dukt