1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > javaWeb连接数据库实现简单的登陆注册功能

javaWeb连接数据库实现简单的登陆注册功能

时间:2024-05-16 08:08:24

相关推荐

javaWeb连接数据库实现简单的登陆注册功能

javaWeb

作者自述实现步骤jar包数据库的驱动以及用户密码jsp页面登陆页面注册页面登陆成功和失败的页面工具类登陆的类注册的类两个映射路径思路登陆注册

作者自述

现在初学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" 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 {@Overrideprotected 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" );}}@Overrideprotected 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 {@Overrideprotected 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" );}}@Overrideprotected 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页面里判断之后跳转到登陆页面

注意:这里为什么没在注册类里面用重定向

因为使用重定向之后就直接返回了登陆类运行的结果

(如有更好的思路,浏览作者 ! 谢谢)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。