实现步骤
创建maven项目 配置Tomcat 这些都不细说了
jar包
因为要连接数据库所以一定要在maven项目下的xml文件下配置数据库的jar包依赖
mysql mysql-connector-java 8.0.28
这个千万不要忘了 重重之重
数据库的驱动以及用户密码
package com.li.Servlet.blit; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class JDBC { private static String url = null; private static String name = null; private static String password = null; static { try { Class.forName ( "com.mysql.cj.jdbc.Driver" ); } catch (Exception e) { e.printStackTrace ( ); } url = "jdbc:mysql://localhost:3306/zhiqingli?useUnicode=true&characterEncoding=utf8&useSSL=true"; name = "root"; password = "root"; } public static Connection getConn() throws Exception { return DriverManager.getConnection ( url, name, password ); } public static void release(Connection conn, PreparedStatement pst, ResultSet res) { try { if (res != null) { res.close ( ); } if (pst != null) { pst.close ( ); } if (conn != null) { conn.close ( ); } } catch (Exception e) { e.printStackTrace ( ); } } }
这个类可以直接复制使用,但要改成自己的数据库名字
jsp页面
登陆页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
注册页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>注册
登陆成功和失败的页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>成功页面 登陆成功,感谢使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>失败页面 登陆失败,账号密码找不到或者错误
工具类
工具人上线
//登陆使用的方法 Connection conn = null; PreparedStatement pst = null; ResultSet res = null; boolean isQ; public boolean Select(String username,String paw) { try { conn = JDBC.getConn ( ); String sql = "SELECT * FROM `username` where `name`=? and `password`=?"; pst = conn.prepareStatement (sql); pst.setObject ( 1,username ); pst.setObject ( 2,paw ); res = pst.executeQuery ( ); if (res.next ()){ isQ = true; System.out.println (res.getObject ( "name" )+" "+res.getObject ( "password" )); System.out.println ("登陆成功"); }else { isQ = false; System.out.println ("登陆失败"); } }catch (Exception e){ e.printStackTrace (); }finally { JDBC.release ( conn,pst,res ); } return isQ; } //注册用的方法 public void addUser(String name,String paw){ try { conn = JDBC.getConn ( ); String sql = "insert into `username`(`name`,`password`) values (?,?)"; pst = conn.prepareStatement ( sql ); pst.setObject ( 1,name ); pst.setObject ( 2,paw ); int i = pst.executeUpdate ( ); if (i>0){ System.out.println ("添加成功"); } }catch (Exception e){ e.printStackTrace (); }finally { JDBC.release ( conn,pst,res ); } }
登陆的类
package com.li.Servlet.test; import com.li.Servlet.blit.Way; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class index extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String account = req.getParameter ( "account" ); String paw = req.getParameter ( "paw" ); Way way = new Way (); if (way.Select ( account,paw )){ resp.sendRedirect ( "/s4/true.jsp" ); }else { resp.sendRedirect ( "/s4/false.jsp" ); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet ( req, resp ); } }
注册的类
package com.li.Servlet.test; import com.li.Servlet.blit.Way; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class index02 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String number = req.getParameter ( "number" ); String word = req.getParameter ( "word" ); String u = req.getParameter ( "u" ); if (word.equals ( u )){ Way way = new Way ( ); way.addUser ( number,word ); }else { //当两次密码不一致的时候浏览器响应给用户当前注册的页面 resp.sendRedirect ( "/s4/Register.jsp" ); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet ( req, resp ); } }
两个映射路径
test com.li.Servlet.test.index test /test res com.li.Servlet.test.index02 res /res
思路
登陆
这个就是用户输入的账号和密码看看数据库里有没有,有的话就重定向到成功或这失败的页面,相当于浏览器响应给用户的请求结果
注册
相当于就是往数据库里面添加一个账号或者密码然返回到登陆页面
这里我在注册类面没有用到重定向,而是在注册的jsp页面里判断之后跳转到登陆页面
注意:这里为什么没在注册类里面用重定向
因为使用重定向之后就直接返回了登陆类运行的结果
总结
到此这篇关于javaWeb连接数据库实现简单登陆注册功能的文章就介绍到这了,更多相关javaWeb登陆注册功能内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!