JavaWeb 07_创建web项目连接MySQL实现注册登录功能
阅读原文时间:2023年07月08日阅读:1

一、创建一个web项目,参照JW/01_创建web项目及部署

二、在NAVICat 里建数据库 db_01,建表tb_user ,字段UName 、Pwd

三、在web下创建一个Directory, 设名字为JSPWorks

1. 创建jsp文件如图

代码如下:

1)shouye.jsp

<%@ page language="java" import="java.util.\*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>




<title>My JSP</title>  
<meta http-equiv="pragma" content="no-cache">  
<meta http-equiv="cache-control" content="no-cache">  
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
<meta http-equiv="description" content="This is my page">  
<!--  
<link rel="stylesheet" type="text/css" href="styles.css">  
-->  


欢迎JSP
<%--
--%>



2)login.jsp

<%@ page language="java" import="java.util.\*" pageEncoding="utf-8"%> <%! private Object Finally; %><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>




<title>My JSP</title>

<meta http-equiv="pragma" content="no-cache">  
<meta http-equiv="cache-control" content="no-cache">  
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
<meta http-equiv="description" content="This is my page">  
<!--  
<link rel="stylesheet" type="text/css" href="styles.css">  
-->

登录界面 <% String flag = request.getParameter("errNo"); try{ if(flag!=null) out.println("用户名为空或不存在或密码错误"); }catch(Exception e){ e.printStackTrace(); } %>
账 户:
密 码:



3) loginCh.jsp

<%@ page language="java" import="java.util.\*,java.sql.\*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>




<title>My JSP</title>

<meta http-equiv="pragma" content="no-cache">  
<meta http-equiv="cache-control" content="no-cache">  
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
<meta http-equiv="description" content="This is my page">  
<!--  
<link rel="stylesheet" type="text/css" href="styles.css">  
-->



<%
String user = new String(request.getParameter("user").getBytes("ISO-8859-1"),"UTF-8");
String pwd = request.getParameter("pwd");

String driverClass = "com.mysql.cj.jdbc.Driver";  
String url = "jdbc:mysql://localhost:3306/db\_01";  
String username = "root";  
String password = "root";  
try {  
    java.lang.Class.forName(driverClass);//加载驱动  
    Connection conn = DriverManager.getConnection(url, username, password);//得到连接

   /\* if(!conn.isClosed()) {  
        out.println("数据库连接成功!!");  
    }\*/  
    PreparedStatement pStmt = conn.prepareStatement("select \* from tb\_user where  UName = '" + user + "' and Pwd= '" + pwd + "'");  
    ResultSet rs = pStmt.executeQuery();  
    if(rs.next()) {  
        out.println("<br>用户:"+rs.getString(1)+"密码:"+rs.getString(2));  
        out.println("<script language='javascript'>alert('用户登录成功!将返回首页!');window.location.href='JSPWorks/shouye.jsp';</script>");  
    }else{  
        out.println("<script language='javascript'>alert('用户登录失败!将返回首页!');window.location.href='JSPWorks/shouye.jsp';</script>");

    }  
        rs.close();  
        conn.close();  
        pStmt.close();  
}  
catch(ClassNotFoundException e){  
    System.out.println("数据库加载失败!");  
    e.printStackTrace();  
}  
catch(SQLException e1){  
e1.printStackTrace();  
}  
catch(Exception e2){  
e2.printStackTrace();  
}  
finally{  
    System.out.println("数据库获取成功!");  
}  

%>

4) register.jsp

<%@ page language="java" import="java.util.\*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> My JSP
注册界面
用户名:
输入密码:
确认密码:
5) checkRegister.jsp <%@ page language="java" import="java.util.\*,java.sql.\*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>




<title>检查注册</title>

<meta http-equiv="pragma" content="no-cache">  
<meta http-equiv="cache-control" content="no-cache">  
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
<meta http-equiv="description" content="This is my page">  
<!--  
<link rel="stylesheet" type="text/css" href="styles.css">  
-->



<%
String user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pwd = request.getParameter("password");

String driverClass = "com.mysql.cj.jdbc.Driver";  
String url = "jdbc:mysql://localhost:3306/db\_01";  
String username = "root";  
String password = "root";  
Class.forName(driverClass);//加载驱动  
Connection conn = DriverManager.getConnection(url,username,password);//得到连接  
PreparedStatement pStmt = conn.prepareStatement("select \* from tb\_user where UName = '" + user + "'");  
ResultSet rs = pStmt.executeQuery();  
if(rs.next()){  
    out.println("<script language='javascript'>alert('该用户已存在,请重新注册!');window.location.href='JSPWorks/register.jsp';</script>");  
}else{  
    PreparedStatement tmt = conn.prepareStatement("Insert into tb\_user values('" + user + "','" + pwd + "')");  
    int rst = tmt.executeUpdate();  
    if (rst != 0){  
        out.println("<script language='javascript'>alert('用户注册成功!');window.location.href='JSPWorks/shouye.jsp';</script>");  
    }else{  
        out.println("<script language='javascript'>alert('用户注册失败!');window.location.href='JSPWorks/register.jsp';</script>");  
    }  
}  

%>

2. 配置Tomcat

1)先run

2)

3)

4)