登录代码
package com.cyf;
import com.im.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.pojo.User;
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.InputStream;
import java.util.Map;
//这里指定的地址,是告诉对应前端将数据传入这里
@WebServlet("/ServletDemo1")
public class ServletDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// mybatis操作数据库
SqlSessionFactory build = ResourseUtil.getSqlSession();
SqlSession sqlSession = build.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
sqlSession.close();
// 获取到页面传来的数据,并且对数据进行操作
// 传输来的数据是键值对形式,所以request.getParameterMap()方法可以将数据存入map集合
Map<String, String[]> map = request.getParameterMap();
User user = new User();
// 遍历集合,获取到所有的key
for (String s : map.keySet()) {
// 通过key获取到所有的value
String[] strings = map.get(s);
// 判断key是否和实体类的属性名称相等
// 如果相等,那就将数据复制给对应的属性
for (String string : strings) {
if (s.equals("username")){
user.setUsername(string);
}else if(s.equals("password")){
user.setPassword(string);
}
}
}
// 将实体类对象当作参数,传给数据库查询
User user1 = mapper.selectUser(user);
System.out.println(user1);
// 判断返回的数据
if (user1!=null){
// 如果查到数据库中有对应的数据,则判定账号密码生效,登陆成功
// 否则登录失败
response.getWriter().write("登录成功");
}else {
response.getWriter().write("登录失败");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
注册代码
package com.cyf;
import com.im.UserMapper;
import com.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
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.util.Map;
@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// mybatis操作数据库
SqlSessionFactory build = ResourseUtil.getSqlSession();
SqlSession sqlSession = build.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 可以通过request中getParameter方法获取数据
// 主要的数据来自于页面收集到的数据
// 如果页面发送的方法是get,那么数据来自于请求行拼接在url后面的字符串
// 如果页面方法是post,那么数据来自于请求体中
String username = request.getParameter("username");
User user = new User();
user.setUsername(username);
User user1 = mapper.selectUser(user);
String password = request.getParameter("password");
if(user1 != null){
// 查询数据库中如果返回值不为空的话,那么说明数据库中存在数据
// 则判定注册不成功
// 执行跳转语句
response.sendRedirect("index/index.html");
}else{
// 如果判断为空的话,则说明数据不存在
// 就将采集的剩下的数据加入数据库
user.setPassword(password);
mapper.addUser(user);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
工具类代码
package com.cyf;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class ResourseUtil {
// 将数据库链接封装成一个工具类
// 因为数据库只需要开启一次链接,所以使用静态代码块
private static SqlSessionFactory build;
static{
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
build = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSession(){
return build;
}
}
登录页面代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
<link href="css/login.css" rel="stylesheet">
</head>
<body>
<div id="loginDiv">
<form action="/ServletDemo1" id="form" method="post">
<h1 id="loginMsg">LOGIN IN</h1>
<p>Username:<input id="username" name="username" type="text"></p>
<p>Password:<input id="password" name="password" type="password"></p>
<div id="subDiv">
<input type="submit" class="button" value="login up">
<input type="reset" class="button" value="reset">
<a href="register.html">没有账号?点击注册</a>
</div>
</form>
</div>
</body>
</html>
注册页面代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>欢迎注册</title>
<link href="css/register.css" rel="stylesheet">
</head>
<body>
<div class="form-div">
<div class="reg-content">
<h1>欢迎注册</h1>
<span>已有帐号?</span> <a href="login.html">登录</a>
</div>
<!--将表中数据传到指定的action地址-->
<form id="reg-form" action="/ServletDemo2" method="get">
<table>
<tr>
<td>用户名</td>
<td class="inputs">
<input name="username" type="text" id="username" onblur="checkName()">
<br>
<span id="username_err" class="err_msg" style=" display: none ">用户名不太受欢迎</span>
</td>
</tr>
<tr>
<td>密码</td>
<td class="inputs">
<input name="password" type="password" id="password">
<br>
<span id="password_err" class="err_msg" style="display: none">密码格式有误</span>
</td>
</tr>
</table>
<div class="buttons">
<input value="注 册" type="submit" id="reg_btn">
</div>
<br class="clear">
</form>
<script>
function checkName() {
let name = document.getElementById("username").value;
let regex = /^\w{6,19}$/;
if (regex.test(name)){
document.getElementById("username_err").style.display = "none";
} else {
document.getElementById("username_err").style.display = "";
}
}
</script>
</div>
</body>
</html>
跳转页面代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
--这是一个弹窗有两个按键
--点击后,程序会收到true或者false
<script>
var resutl = confirm("用户名已存在")
if(resutl){
window.location="../register.html";
}else{
window.location="../login.html";
}
</script>
</body>
</html>
操作数据库接口
package com.im;
import com.pojo.User;
public interface UserMapper {
User selectUser(User user);
void addUser(User user);
}
|