JavaWeb简单⽤户登录注册实例代码(有验证码)
需求
编写login登录界⾯(⽤户名,密码,验证码,登陆按钮,注册按钮)
设计关系数据库(编号,⽤户名,密码)
编写注册功能,将数据存储在数据库中。(姓名不能重复,设为主键,重复会注册失败)
编写登录功能。⾸先获取验证码,先判断验证码是否正确,不正确则显⽰验证码错误。验证码正确后再获取⽤户名和密码,进⾏数据库的搜索⽐对,若正确则重定向到成功的界⾯,并且将⽤户名显⽰。
jar包
技术选型
Servlet + JSP + Mysql + JDBCTemplate + Druid + BeanUtils + Tomcat
效果图
验证码或者⽤户名和密码错误
Navicat Premium 显⽰Mysql内容
登录成功
源码
登录主界⾯ login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
<script>
<%-- 增加时间戳来更换验证码图⽚ --%>
this.src = "/daydayup/checkCodeServlet?time" + new Date().getTime();
}
}
<%-- ⽤于打开注册的窗⼝--%>
function reg() {
window.open("register.jsp");
}
</script>
<style>
div{
color: red;
}
</style>
</head>
<body>
<form action="/daydayup/loginServlet" method="post">
<table>
<tr>
<td>⽤户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="checkcode"></td>
</tr>
<tr>
<td colspan="2"><img id="img" src="/daydayup/checkCodeServlet" alt=""></td> </tr>
<tr>
<td><input type="submit" value="登录"></td>
<td><input type="button" value="注册" onclick="reg()" ></td>
</table>
</form>
<div>${requestScope.login_error}</div>
<div>${requestScope.checkcode_error}</div>
</body>
</html>
注册界⾯ register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/daydayup/registerServlet" method="post">
<table>
<tr>
<td>⽤户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="确定注册"></td>
</tr>
</table>
</form>
<div>${ister_success}</div>
<div>${ister_error}</div>
</body>
</html>
成功界⾯ success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- <h1>${requestScope.user},欢迎您</h1> --%>
<h1><%=Session().getAttribute("user")%>,欢迎您</h1>
</body>
</html>
先编辑好JDBCUtils⼯具类
de.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* JDBC⼯具类使⽤Durid连接池
*/
public class JDBCUtils {
private static DataSource ds;
static{
try {
//加载配置⽂件
Properties pro = new Properties();
//使⽤ClassLoader加载配置⽂件,获取字节输⼊流
InputStream is = ClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//初始化连接池对象
ds = ateDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接池对象
*/
public static DataSource getDataSource(){
return ds;
}
/**
* 获取连接connection对象
*/
public static Connection getConnection() throws SQLException {
Connection();
}
}
创建我们的⽤户类实体User
de.domain;
/*
⽤户的实体类
*/
public class User {
private int id ;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;web端登录
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
编写Dao来操作数据库,⾥⾯有login和add两个⽅法 Userdao de.dao;
de.domain.User;
de.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.BeanPropertyRowMapper;
import org.JdbcTemplate;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 操作数据库中User表的类
*/
public class UserDao {
//声明JDBCTemplate对象,以便⽅法公⽤
private JdbcTemplate template = new DataSource());
/**
* 登录⽅法
*
* @param loginUser ⽬前只有⽤户名和密码
* @return user包含⽤户全部数据
*/
public User login(User loginUser) {
//编写SQL
String sql = "select * from user where username = ? and password = ?";
//调⽤query⽅法
try {
User user = template.queryForObject(sql,
new BeanPropertyRowMapper<User>(User.class),
return user;
} catch (DataAccessException e) {
e.printStackTrace();
return null;
}
}
/**
* 注册⽅法
*
* @param user ⽬前只包含⽤户名和密码
* @return 是否注册成功
*/
public boolean add(User user){
//编写SQL
String sql = "insert into user (username,password) VALUES('" + Username() + "','" + Password() + "')"; boolean flag = false;
int num = 0;
try {
//创建数据库连接
Connection conn = Connection();
Statement state = null;
flag = false;
num = 0;
state = ateStatement();
num = uteUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
if(num >0) flag = true;
return flag;
}
}
登录的Servlet
de.servlet;
de.dao.UserDao;
de.domain.User;
import javax.servlet.ServletException;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论