利用HttpServlet完成MySql数据库my_user表的登录校验
创始人
2024-11-14 11:33:59
0

功能:利用HttpServlet完成MySql数据库my_user表的登录校验

step1:在porm.xml文件中导jdbc的依赖

mysql数据库输入

SELECT VERSION()

查询当前的数据库版本

 maven官网网址;https://mvnrepository.com/

maven官网中搜索:mySQL

并复制依赖粘贴到pom文件中

main函数下java包的class类

package com.iweb.demo03;  import org.apache.commons.codec.digest.DigestUtils;  import javax.servlet.RequestDispatcher; 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 java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.List; import java.util.Map;  @WebServlet(value = "/login") public class Login extends HttpServlet {     @Override     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {      }      @Override     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          req.setCharacterEncoding("utf-8");         resp.setCharacterEncoding("utf-8");         resp.setContentType("text/html");          PrintWriter pw = resp.getWriter();         String username = req.getParameter("username");         String password = req.getParameter("password");         password = DigestUtils.md5Hex(password);         String sql = "select count(*) cou from my_user m " +                 "where m.username=? and m.password=?";         List> list = null;         try {             list = JDBCUtill.jdbcBySelect(sql, username, password);         } catch (SQLException e) {             throw new RuntimeException(e);         } catch (ClassNotFoundException e) {             throw new RuntimeException(e);         }         Map map = list.get(0);         long cou = (long) map.get("cou");         String path="";         if (cou > 0) {             /* pw.print("欢迎您"+username);//页面跳转*/             /* resp.sendRedirect("/demo03/test5.html");*/         path=req.getContextPath() + "/test5.html";              System.out.println("登录成功");         } else {           path=req.getContextPath()+"/login.html?islogin=0";         }         resp.sendRedirect(path);     } } 

html文件 :

            用户登陆        

用户登陆

账号

密码



附录:

JDBC文件: 

package com.iweb.demo03;// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) //  import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;  public class JDBCUtill {     static Connection conn;     static PreparedStatement pstat;     static ResultSet rest;      public JDBCUtill() {     }      private static void getSource() throws SQLException, ClassNotFoundException {         Class.forName("com.mysql.cj.jdbc.Driver");         conn = DriverManager.getConnection("jdbc:mysql://192.168.77.100:3306/mysql?characterEncoding=utf-8", "root", "123456");     }      private static void closeSource() throws SQLException {         if (rest != null) {             rest.close();         }          if (pstat != null) {             pstat.close();         }          if (conn != null) {             conn.close();         }      }      public static List> jdbcBySelect(String sql) throws SQLException, ClassNotFoundException {         List> list = new ArrayList();         getSource();         pstat = conn.prepareStatement(sql);         rest = pstat.executeQuery();         ResultSetMetaData rsmd = rest.getMetaData();         int columnCount = rsmd.getColumnCount();          while(rest.next()) {             Map map = new HashMap();              for(int i = 1; i <= columnCount; ++i) {                 String columnName = rsmd.getColumnName(i);                 Object columnValue = rest.getObject(i);                 map.put(columnName, columnValue);             }              list.add(map);         }          closeSource();         return list;     }      public static List> jdbcBySelect(String sql, String username, String password) throws SQLException, ClassNotFoundException {         List> list = new ArrayList();         getSource();         pstat = conn.prepareStatement(sql);         pstat.setString(1, username);         pstat.setString(2, password);         rest = pstat.executeQuery();         ResultSetMetaData rsmd = rest.getMetaData();         int columnCount = rsmd.getColumnCount();          while(rest.next()) {             Map map = new HashMap();              for(int i = 1; i <= columnCount; ++i) {                 String columnName = rsmd.getColumnName(i);                 Object columnValue = rest.getObject(i);                 map.put(columnName, columnValue);             }              list.add(map);         }          closeSource();         return list;     }      public static  int updateBySql(String sql, E... e) throws SQLException, ClassNotFoundException {         getSource();         int res=0 ;         pstat = conn.prepareStatement(sql);          for(int i = 0; i < e.length; ++i) {             pstat.setObject(i + 1, e[i]);         }          res = pstat.executeUpdate();         closeSource();         return res;     } } 

上一篇:day_30

下一篇:VueRouter 相关信息

相关内容

热门资讯

总算了解!!约局吧作必弊脚本,... 总算了解!!约局吧作必弊脚本,微乐游戏竞技团辅助(透视)确实真的有工具(哔哩哔哩)1、点击下载安装,...
不少玩家反映!智星菠萝辅助,微... 不少玩家反映!智星菠萝辅助,微乐老友广东辅助器(透视)好像是有方法(哔哩哔哩)1)微乐老友广东辅助器...
重大消息!约局吧如何查看是否有... 重大消息!约局吧如何查看是否有挂,微信微乐辅助器使用教程(透视)竟然有有插件(哔哩哔哩)1、点击下载...
推荐攻略!拱趴大菠萝怎么开挂,... 推荐攻略!拱趴大菠萝怎么开挂,微信微乐挂有没有(透视)本来是真的有app(哔哩哔哩)微信微乐挂有没有...
玩家必看科普!xpoker透视... 玩家必看科普!xpoker透视辅助,微乐自建房辅助软件如何下载(透视)切实是真的有技巧(哔哩哔哩)1...
玩家亲测!哈糖大菠萝怎么开挂,... 玩家亲测!哈糖大菠萝怎么开挂,微乐自建房插件详细教程(透视)好像真的有器(哔哩哔哩)1、操作简单,无...
盘点几款!哈糖大菠萝有挂吗5个... 盘点几款!哈糖大菠萝有挂吗5个常用方法,微乐陕西小程序破解器(透视)都是存在有软件(哔哩哔哩)1、金...
有玩家发现!竞技联盟破解版最新... 有玩家发现!竞技联盟破解版最新版,微乐家乡官方app下载(透视)总是存在有技巧(哔哩哔哩)1、实时微...
玩家必看教程!!we poke... 玩家必看教程!!we poker免费辅助器,微乐自建房黑科技辅助工具下载(透视)总是是有软件(哔哩哔...
专业讨论!!哈糖大菠萝有挂吗5... 专业讨论!!哈糖大菠萝有挂吗5个常用方法,微乐小程序免费黑科技下载方法(透视)一直真的是有脚本(哔哩...