本文主要是介绍web + servlet + jdbc mysql 实现简单的登陆注册界面有点长但是耐心很重要(含完整步骤代码,不懂可留言私信),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先我们准备好开发使用的工具以及配置
idea2020
tomcat8.5 创建javaweb参考idea编译Tomcat详细步骤
IDEA通过JDBC连接数据库请参考jdbc连接数据库
首次创建我们的javaweb
接下来我们导包,导包请参考IDEA通过JDBC连接数据库请参考jdbc连接数据库
包的位置如图,记得右键选add library
我们在src包里面新建一个bean
在bean里面写一个Usedata类
建立一个登录界面要有,id,密码,注册界面,id,密码,姓名。后续需要什么自己可以添加的,比如加个性别什么的,做什么管理系统图书之类的,可以加书名,价格,日期等等,所以这里面我们的属性简单一点方便大家学习和交流
package bean;public class Usedata {private String id;//账号private String name;//名字private String pwd;//密码public Usedata(String id, String name, String pwd) {this.id = id;this.name = name;this.pwd = pwd;}public String getName() {return name;}public String getId() {return id;}public String getPwd() {return pwd;}}
//定义好数据表的属性
数据库
我们创建一下数据库
创建表
create table table_name
(id varchar(45) not null,name varchar(45) not null,pwd varchar(45) not null,constraint table_name_pkprimary key (id)
);
在idea使用database连接数据库
再创建一个连接数据库的和使用数据库的类
package db;import bean.Usedata;import java.sql.*;
import java.util.ArrayList;public class Database {Connection conn = null;//注册驱动器public Database() throws ClassNotFoundException, SQLException {Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动this.conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata01", "root", "123456");//System.out.println(conn);测试是否连接成功}//验证是否连接成功public void showConnect() {System.out.println(this.conn);}//在表里添加数据public void insert(String id, String name,String pwd) throws SQLException {PreparedStatement prep = conn.prepareStatement("insert into mydata01.table_name(id, name,pwd) value (?,?,?)");prep.setString(1, id);prep.setString(2, name);prep.setString(3, pwd);prep.execute();//运行程序}//在表里删除数据public void delete(String id) throws SQLException {PreparedStatement prep = conn.prepareStatement("delete from sl.table_name where id=?");prep.setString(1, id);prep.execute();}//在表里查询数据public Usedata getUser(String id) throws SQLException {PreparedStatement prep = conn.prepareStatement("select * from mydata01.table_name where id=?");prep.setString(1, id);prep.execute();ResultSet resultSet = prep.getResultSet();if (resultSet.next()) {String myname = resultSet.getString("name");String myid = resultSet.getString("id");String mypwd = resultSet.getString("pwd");return new Usedata(myid,myname,mypwd);} else {return null;}}//查看整张表public ArrayList<Usedata> getAllUser() throws SQLException {ArrayList<Usedata> uselist = new ArrayList<Usedata>();PreparedStatement prep = conn.prepareStatement("select * from mydata01.table_name");prep.execute();ResultSet resultSet = prep.getResultSet();while (resultSet.next()) {String myid = resultSet.getString("id");String myname = resultSet.getString("name");String mypwd = resultSet.getString("pwd");uselist.add(new Usedata(myid, myname, mypwd));}return uselist;}//关闭连接public void close() throws SQLException {conn.close();}//登录实现public Usedata login(String id, String pwd) throws SQLException {PreparedStatement prep = conn.prepareStatement("select mydata01.table_name.pwd from sl.table_name where id=?");prep.setString(1,id);prep.execute();ResultSet resultSet = prep.getResultSet();if (resultSet.next()){String mypwd =resultSet.getString("pwd");if (pwd.equals(mypwd)){return getUser(id);}elsereturn null;}elsereturn null;}public void insert(String id, String name, String pwd) {}
}
创建servlet
首先创建一个包servlet
创建登陆界面的类
创建注册界面的类
package servlet;import bean.Usedata;
import db.Database;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;@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("GBK");response.setCharacterEncoding("GBK");PrintWriter printWriter = response.getWriter();response.setHeader("content-tupe","test/html;charset=GBK");String id = request.getParameter("id");String pwd = request.getParameter("pwd");response.setContentType("text/html;charset=GBK");PrintWriter out =response.getWriter();try {Database database =new Database();database.showConnect();Usedata userdata = database.login(id,pwd);database.close();if(userdata==null) {out.println("用户名或者密码不正确<br>");out.println("<a href=\"http://localhost:8880/servlet_war_exploded/login.html\">登陆界面</a>");}else {out.println("welecome" + userdata.getName());// response.sendRedirect("/_war_exploded/loginOK.jsp")这个是直接跳转到别的jsp用的out.println("用户名:" + userdata.getName());out.println("<a href=\"http://localhost:63342/%E4%BA%94%E5%AD%90%E6%A3%8B%E5%B0%8F%E6%B8%B8%E6%88%8F/gobang-master/index.html?_ijt=gruh5vqggfpoepnspgdeocftv0\">点击跳转游戏界面</a>");}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);}
}
package servlet;import db.Database;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;@WebServlet( "/RegisterServlet")
public class RegisterServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("GBK");response.setCharacterEncoding("GBK");PrintWriter printWriter = response.getWriter();response.setHeader("content-tupe","test/html;charset=GBK");String name = request.getParameter("name");String id = request.getParameter("id");String pwd = request.getParameter("pwd");String pwd2 = request.getParameter("pwd2");if (pwd.equals(pwd2)) try {Database database = new Database();
// //一、是否连接数据库database.showConnect();database.insert(id,name,pwd);database.close();printWriter.write("注册成功");response.sendRedirect("/servlet_war_exploded/login.html");} catch (ClassNotFoundException |SQLException e ) {e.printStackTrace();printWriter.write("注册失败");}else {printWriter.write("两次输入密码不一致!");}}protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {doPost(request,response);}
}
我们再web.xml配置一些东西
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><servlet><servlet-name>loginServlet</servlet-name><servlet-class>servlet.LoginServlet</servlet-class></servlet><servlet><servlet-name>registerServlet</servlet-name><servlet-class>servlet.RegisterServlet</servlet-class></servlet><servlet-mapping><servlet-name>loginServlet</servlet-name><url-pattern>/servlet/LoginServlet</url-pattern></servlet-mapping><servlet-mapping><servlet-name>registerServlet</servlet-name><url-pattern>/servlet/RegisterServlet</url-pattern></servlet-mapping></web-app>
接下来我们写登录界面
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<form method="post" action="LoginServlet">账号:<input type="text" name="id">密码:<input type="password" name="pwd"><br><input type="submit" name="登录"><input type="reset" value="重新输入">
</form>
</body>
</html>
注册界面
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<form method="get" action="RegisterServlet" >姓名:<input type="text" name="name"><br>账号:<input type="text" name="id"><br>密码:<input type="password" name="pwd"><br>确认密码:<input type="password" name="pwd2"><br><input type="submit" value="提交"><input type="reset" value="重新输入"></form></body>
</html>
首页我们直接再jsp里面写
<%--Created by IntelliJ IDEA.User: 咋咋Date: 2022/1/15Time: 11:07To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><head><title>$Title$</title></head><body><a href="http://localhost:8880/servlet_war_exploded/login.html">登陆界面</a><a href="http://localhost:8880/servlet_war_exploded/Register.html">注册界面</a></body>
</html>
配置好tomacat后运行
运行效果
测试一下注册
当两次密码不一致的时候
注册成功后会自动跳转到登录界面
数据库方面
以上便是今天全部内容欢迎咨询各种疑难杂症
这篇关于web + servlet + jdbc mysql 实现简单的登陆注册界面有点长但是耐心很重要(含完整步骤代码,不懂可留言私信)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!