javaWeb连接数据库实现简单登陆注册功能的全过程
实现步骤
创建maven项目 配置Tomcat 这些都不细说了
jar包
因为要连接数据库所以一定要在maven项目下的xml文件下配置数据库的jar包依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency>
这个千万不要忘了 重重之重
数据库的驱动以及用户密码
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" %> <html> <body> <form action="${pageContext.request.contextPath}/test" method="get"> <h3>账号:<input type="text" name="account"></h3> <h3>密码:<input type="password" name="paw"></h3> <h3><input type="submit" value="登陆"><a href="Register.jsp" rel="external nofollow" target="_blank">注册</a></h3> </form> </body> </html>
注册页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>注册</title> </head> <body> <form action="${pageContext.request.contextPath}/res" method="post"> <h3>账号:<input type="text" placeholder="请输入注册的账号" name="number"></h3> <h3>密码:<input type="password" placeholder="请输入注册的密码" name="word" id="one"></h3> <h3>确认密码:<input type="password" placeholder="确认一遍密码" name="u" id="two"></h3> <input type="submit" value="提交" οnclick="show()"> </form> <Script> function show(){ var one = document.getElementById("one").value; var two = document.getElementById("two").value; if (one === two){ alert("注册成功"); window.open("index.jsp") }else { alert("两次输入的密码不一致"); } } </Script> </body> </html>
登陆成功和失败的页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>成功页面</title> </head> <body> <h1>登陆成功,感谢使用</h1> </body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>失败页面</title> </head> <body> <h1>登陆失败,账号密码找不到或者错误</h1> </body> </html>
工具类
工具人上线
//登陆使用的方法 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 ); } }
两个映射路径
<!--登陆--> <servlet> <servlet-name>test</servlet-name> <servlet-class>com.li.Servlet.test.index</servlet-class> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> <!--注册--> <servlet> <servlet-name>res</servlet-name> <servlet-class>com.li.Servlet.test.index02</servlet-class> </servlet> <servlet-mapping> <servlet-name>res</servlet-name> <url-pattern>/res</url-pattern> </servlet-mapping>
思路
登陆
这个就是用户输入的账号和密码看看数据库里有没有,有的话就重定向到成功或这失败的页面,相当于浏览器响应给用户的请求结果
注册
相当于就是往数据库里面添加一个账号或者密码然返回到登陆页面
这里我在注册类面没有用到重定向,而是在注册的jsp页面里判断之后跳转到登陆页面
注意:这里为什么没在注册类里面用重定向
因为使用重定向之后就直接返回了登陆类运行的结果
总结
到此这篇关于javaWeb连接数据库实现简单登陆注册功能的文章就介绍到这了,更多相关javaWeb登陆注册功能内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring-cloud-eureka使用feign调用服务接口
这篇文章主要为大家详细介绍了Spring-cloud-eureka使用feign调用服务接口,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2019-04-04
最新评论