JSP实现简单的⽤户登录和注册实验环境:Tomcat 9.0,MySQL 8.0,JDBC 8.0;
实验⽅法,利⽤连接池连接MySQL进⾏操作
⾸先创建⼀个Dynamic Web Project
1.连接池的创建
META_INF ⽬录下创建⽂件 l
在 WebContent
WebContent ⽬录下的 META_INF
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="123456"
maxIdle="30"
maxWait="10000"
maxActive="100"
driverClassName="sql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/Duser?serverTimezone=GMT"
logAbandoned="true" />
</Context>
create database Duser;
use Duser;
create table user(
name varchar(30) not null,
password varchar(30) not null,
constraint primary key(name) //添加姓名为主键
);
4.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>⽤户登录页⾯</title>
</head>
<body >
<h2 align="center"><font color=red>⽤户登录页⾯</font></h2>
<form action="success.jsp" method="post">
<table align="center" border="1">
<tr>
<td>⽤户名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密 ;码:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录" name="login"></td>
<td><input type="reset" value="重置" name="reset"></td>
</tr>
</table>
<p align="center"><a href="registered.jsp" color=blue>注册⽤户</a></p> </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body align="center">
<h2>新⽤户注册</h2>
<form action="registeredSucceed.jsp" method="post">
<table align="center">
<tr align="right">
<td>请输⼊⽤户名:</td>
<td><input type="text" name=name autofocus="autofocus"></td> </tr>
<tr align="right">
<td>请输⼊密码:</td>
<td><input type="text" name=password></td>
</tr>
<tr align="right">
<td>请输⼊确认密码:</td>
<td><input type="text" name=refill></td>
</tr>
</table>
<input type="submit" name=register value="注册" >
<input type="reset" name=refill value="重填" >
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册成功页⾯</title>
</head>
<body>
<%
Context ctx = null;
DataSource ds = null;
Statement stmt =null;
ResultSet rs = null;
Connection con = null;
String Parameter("name").trim();//去除⾸尾空格
String Parameter("password").trim();
String Parameter("refill").trim();
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");//mysql是在创建连接池时⾃⼰创建的名字 con = ds.getConnection();
stmt = ateStatement();
if(name!=null ){
uteQuery("select * from user where name='"+name+"'");
()){
out.print("⽤户已经存在 "+"请<a href=\"registered.jsp\">注册</a>");
}else{
if(password.equals(refill)){
%>
注册成功<br>
三秒钟后⾃动转到登录页⾯<br>
如果没有跳转,请点击<a href="login.jsp">这⾥</a>
<span ><meta http-equiv="refresh" content="3;URL=login.jsp"> </span> <%
}else{
out.print("密码输⼊不⼀致<br>"+"重新<a href=\"registered.jsp\">注册</a>");
}
}
}else {
out.print("姓名不能为空");
}
}catch(Exception e){
out.print(e);
}finally{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
%>
</body>
</html>
7.success.jsp
<%request.setCharacterEncoding("utf-8"); %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>判断登录</title>
</head>
<body>
<%
Context ctx = null;
DataSource ds = null;
Statement stmt =null;
ResultSet rs = null;
Connection con = null;
String name = Parameter("name").trim();
String password = Parameter("password").trim();
jsp和html哪个更好try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");//mysql是在创建连接池时⾃⼰创建的名字语句功能到配置的数据库 con = ds.getConnection();//创建数据库连接
stmt = ateStatement();
uteQuery("select * from user where name='"+name+"'");
()){
uteQuery("select * from user where name='"+name+"' and password='"+password+"'");
()){
out.print(name+"登录成功");
}else{
out.print("密码输⼊错误<br>"+"重新<a href=\"login.jsp\">登录</a>");
}
}else{
out.print("<font color=red>"+name+"</font>⽤户不存在<br>"+"请点击<a href=\"registered.jsp\">注册</a>");
}
}catch(Exception e){
out.print(e);
}finally{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
%>
</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论