Cookie对象的创建--Cookie cookie=new Cookie("key","value");
发送Cookie:resp.addCookie();
获取Cookie数据:req.getCookie("","");
Cookie不能直接存储中文
需要进行转码:URL编码
String value=URLEncoding.encode(value,"utf-8");
解码:
String value=URLDecoding.decode(value,"utf-8");
登录注册案例实现
//用户登录
//登录界面的实现
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录界面</title>
</head>
<body>
<center>
<form action="loginServlet" method="post">
<h1 id="loginMsg">LOGIN</h1>
<div id="errorMsg">${login_msg}</div>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" placeholder="请输入用户名"></td>
</tr>
<p>
<tr>
<td>密码:</td>
<td><input type="text" name="password" placeholder="请输入密码"></td>
</tr>
<p>
<tr>
<input type="checkbox" name="remember">记住信息
</tr>
<p>
<tr>
<button type="submit" value="登录">登录</button>
</tr>
</form>
</center>
</body>
</html>
//显示界面的实现
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: 12140
Date: 2022/10/27
Time: 14:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>浏览界面</title>
</head>
<body>
<center>
<h1>${user.username},欢迎你!</h1>
<table colspan="3">
<tr>
<td align="center">用户名</td>
<td align="center">密码</td>
<td align="center">学号</td>
</tr>
<c:forEach items="${list}" var="user">
<tr>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.id}</td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>
//后端界面的实现
//获取到前端输入的数据,并且进行判断该用户是否存在在数据库中
package org.example.serlvet;
import org.example.Bean.User;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
private UserService us=new UserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String username=req.getParameter("username");
String password=req.getParameter("password");
User user=us.login(username,password);
//判断
if(user!=null){
HttpSession hs=req.getSession();
hs.setAttribute("user",user);
String contentPath=req.getContextPath();
resp.sendRedirect(contentPath+"/allServlet");
}
else{
req.setAttribute("login_msg","用户名或者密码错误!");
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
//查询到数据库的表中的所有数据
package org.example.serlvet;
import org.example.Bean.User;
import org.example.service.UserService;
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.List;
@WebServlet("/allServlet")
public class allServlet extends HttpServlet {
private UserService us=new UserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
List<User> list=us.selectAll();
req.setAttribute("list",list);
req.getRequestDispatcher("look.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
登录界面的实现:
显示界面的实现:
用户登录时需要注意到的问题
在进行用户的登录操作时,发现若是想要实现在利用c:forEach标签之前,将文本框中获取到的数据先展示在jsp页面,那么,我们需要利用
session实现存储数据的状态,到时候方便拿出来用;
因为这次的用户登录案例,在两个jsp页面之间,有两个servlet.java文件进行跳转,数据跳转就没了,需要session将数据存储起来,
到时候直接在第二个jsp页面利用一下就好!
记住用户案例的实现(复选框------记住我)
再返回到登录界面,就会显示出数据保存到这里的结果:
用户注册案例的实现(用户名不可重复)
//UserService.java(里面存放各种操作的具体实现方法)
//用户注册(添加)
public void add(User user){
SqlSession sqlSession=sf.openSession();
UserMapper us=sqlSession.getMapper(UserMapper.class);
us.add(user);
sqlSession.commit();
}
//用户判断注册
public boolean register(User user){
SqlSession sqlSession=sf.openSession();
UserMapper us=sqlSession.getMapper(UserMapper.class);
User u=us.selectByUsername(user.getUsername());
if(u==null){
//可以注册
us.add(user);
sqlSession.commit();
}
sqlSession.close();
return u==null;
}
//register.java(注册功能的具体实现方法)
package org.example.serlvet;
import org.example.Bean.User;
import org.example.service.UserService;
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;
@WebServlet("/registerServlet")
public class registerServlet extends HttpServlet {
private UserService us=new UserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String username=req.getParameter("username");
String password=req.getParameter("password");
User user=new User();
user.setUsername(username);
user.setPassword(password);
boolean flag=us.register(user);
if(flag){
req.setAttribute("register_msg","注册成功!请登录!");
//req.setAttribute("user",user);
req.getRequestDispatcher("index.jsp").forward(req,resp);
}else{
req.setAttribute("register_msg","注册失败!");
req.getRequestDispatcher("register.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
//注册界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册界面</title>
</head>
<body>
<center>
<tr>
已有账号?<a href="index.jsp">登录</a>
</tr>
<form action="registerServlet" method="post">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" placeholder="请输入用户名"></td>
<br>
<div id="errorUsername">${register_msg}</div>
</tr>
<p>
<tr>
<td>密码:</td>
<td><input type="text" name="password" placeholder="请输入密码"></td>
</tr>
<p>
<tr>
<td>验证码:</td>
<td><input type="text" name="yan" placeholder="请输入验证码"></td>
</tr>
<p>
<tr>
<button type="submit" value="提交">提交</button>
</tr>
</form>
</center>
</body>
</html>
若是数据库里面有与注册界面的那个username文本框里面的相同的数据,那么会显示注册不成功,需要重新注册!
验证码案例的实现
由于验证码用时较长,决定单独出一期,给足验证码“牌面”。
移步下一篇嗷!