dao
public class GoodDao {
QueryRunner queryRunner = QueryRunnerUtils.getQueryRunner();
//查询所有商品
public List allGood() {
String sql = "select * from good";
List list = null;
try {
list = queryRunner.query(sql, new BeanListHandler<>(Good.class));
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public Good goodById(int id) {
String sql = "select * from good where id = ?";
Good good = null;
try {
good = queryRunner.query(sql, new BeanHandler<>(Good.class), id);
} catch (SQLException e) {
e.printStackTrace();
}
return good;
}
}
实体类
package com.entity;
public class Good {
private int id;
private String name;
private double price;
public Good() {
}
public Good(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Good{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
servlet
@WebServlet(name = "LoginCookieServlet", value = "/LoginCookieServlet")
public class LoginCookieServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//防止中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset:utf-8");
//获取账号密码的值
String username = request.getParameter("username");
String password = request.getParameter("password");
//此处没有联动数据库,做个伪登录,假设登录状态为flag
boolean flag = true;
if (flag) {
//登录成功
String[] remembers = request.getParameterValues("remember");
if (remembers != null) {
for (int i = 0; i < remembers.length; i++) {
if ("chose".equals(remembers[i])) {
//点击记住密码
Cookie nameCookie = new Cookie("username", username);
Cookie passwordCookie = new Cookie("password", password);
//设置时效
nameCookie.setMaxAge(60 * 60 * 24);
passwordCookie.setMaxAge(60 * 60 * 24);
//加入响应对象
response.addCookie(nameCookie);
response.addCookie(passwordCookie);
} else {
//取消记住密码
Cookie nameCookie = new Cookie("username", null);
Cookie passwordCookie = new Cookie("password", null);
//设置时效
nameCookie.setMaxAge(0);
passwordCookie.setMaxAge(0);
//加入响应对象
response.addCookie(nameCookie);
response.addCookie(passwordCookie);
}
/* if ("auto".equals(remembers[i])){
//选中自动登录
//将用户名密码存入session
session.setAttribute("name",username);
session.setAttribute("psw",password);
//设置session时效
session.setMaxInactiveInterval(60*60*24);
//将session放入cookie中
Cookie cookieSession = new Cookie("JSESSIONID", session.getId());
//设置cookie时效
cookieSession.setMaxAge(60*60*24);
//加入响应对象
response.addCookie(cookieSession);
}else {
//取消自动登录
}*/
}
}
//获取session
HttpSession session = request.getSession();
//将用户名存入session
session.setAttribute("name", username);
response.sendRedirect("index.jsp");
} else {
//登录失败
response.sendRedirect("login.jsp");
}
}
}
package com.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "SessionServlet", value = "/SessionServlet")
public class SessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取到session对象
HttpSession session = request.getSession();
//使session立即无效
session.invalidate();
//返回登录界面
response.sendRedirect("login.jsp");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
package com.servlet;
import com.dao.GoodDao;
import com.entity.Good;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet(name = "ShoopServlet", value = "/ShoopServlet")
public class ShoopServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
GoodDao goodDao = new GoodDao();
String type = request.getParameter("type");
if ("allGood".equals(type)) {
List goodList = goodDao.allGood();
request.setAttribute("list", goodList);
request.getRequestDispatcher("goodIndex.jsp").forward(request, response);
} else if ("addGood".equals(type)) {
//获取加入购物车的商品ID
int id = Integer.parseInt(request.getParameter("id"));
//获取此商品
Good good = goodDao.goodById(id);
//获取session对象
HttpSession session = request.getSession();
//设置保持时间
session.setMaxInactiveInterval(60 * 60 * 24);
//为了关闭浏览器数据不丢失,存入cookie中
Cookie jsessionid = new Cookie("JSESSIONID",session.getId());
//设置cookie时效
jsessionid.setMaxAge(60 * 60 * 24);
response.addCookie(jsessionid);
//容器存储商品
List goodList = (List) session.getAttribute("list");
if (goodList == null) {
goodList = new ArrayList<>();
}
goodList.add(good);
session.setAttribute("list", goodList);
response.sendRedirect("shoppingCart.jsp");
}
}
}
$END$
<%--测试1
测试2--%>
<%--获取Cookie,必须一次性获取当前网站所有的Cookie--%>
<%
/*Cookie[] cookies = request.getCookies();
if (cookies!=null){
for (Cookie cookie:cookies){
out.println(cookie.getName()+":"+cookie.getValue());
}
}*/
Object sessionAttribute = session.getAttribute("name");
if (sessionAttribute == null) {
response.sendRedirect("login.jsp");
}
%>
欢迎您,${name}
退出登陆
查看所有商品
<%--
Created by IntelliJ IDEA.
User: xx
Date: 2023/8/31
Time: 18:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
商品
<%--
Created by IntelliJ IDEA.
User: xx
Date: 2023/8/31
Time: 18:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
购物车
购物车商品
商品名称
商品价格
${good.name}
${good.price}
<%--
Created by IntelliJ IDEA.
User: xx
Date: 2023/8/31
Time: 11:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录
<%
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
pageContext.setAttribute("username", cookie.getValue());
}
if ("password".equals(cookie.getName())) {
pageContext.setAttribute("password", cookie.getValue());
}
/*if ("JSESSIONID".equals(cookie.getName())){
response.sendRedirect("index.jsp");
}*/
}
}
%>