package com.zkiing.filter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.net.httpserver.Filter.Chain;
@WebFilter("/*")//所有的请求都会被拦
public class LoginFilter implements Filter{
//如果登陆了正常通行
//如果没登陆 让你回到登陆页面
//集合中放上不需要拦截的路径
List<String>list=new ArrayList<String>();
{
list.add("/login.jsp");
list.add("/login.do");
list.add(".css");
list.add(".js");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//有一些页面需要放行
//父转子强转拿到getServletPath()的方法
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse resp=(HttpServletResponse)response;
String path = req.getServletPath();
//判断访问路径是否在list集合中
boolean f=false;
for(String p:list) {
if(path.endsWith(p)) {//是否以p结尾
f=true;
break;
}
}
//如果存在list集合中那么就放行
if(f) {
chain.doFilter(request, response);
return;
}
//判断你是否可以通行【有没有登陆 session中是否有user属性】
Object user = req.getSession().getAttribute("user");
if(user==null) {
resp.sendRedirect("login.jsp");
return;
}else {
chain.doFilter(request,response);//放行
}
}
}
car.jsp补充
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<title>我的购物车</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
* {
outline: none !important;
}
html,
body {
background: #1abe9c;
}
form {
width: 300px;
background: #ebeff2;
box-shadow: 0px 0px 50px rgba(0, 0, 0, .5);
border-radius: 5px;
padding: 20px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.btn-group {
width: 100%;
}
.btn-group button {
width: 50%;
}
</style>
</head>
<c:if test="${list==null}">
<c:redirect url="index.do"></c:redirect>
</c:if>
<body>
<div class="container">
<h1>
${user.name}您好, <small>这是首页</small>
</h1>
<h1>
<a href="index.do" class="btn btn-default">继续购买</a>
<a href="calc.do?" class="btn btn-default">订单结算</a>
<a href="order.do" class="btn btn-default">查看订单</a>
<a hred="clear.do" class="btn btn-default">清空购物车</a>
</h1>
<table class="table table-bordered table-striped">
<tbody><tr>
<th>商品编号</th>
<th>商品名字</th>
<th>商品数量</th>
<th>商品总价</th>
<th>商品操作</th>
</tr>
<c:forEach items="${car}" var="item">
<tr>
<td>${item.goods.id}</td>
<td>${item.goods.name}</td>
<td>${item.quantity}</td>
<td>${item.total}</td>
<td>
<a href="del.do?id=${item.goods.id}" class="btn btn-default">删除</a>
<a href="upd.do?id=${item.goods.id}&type=1" class="btn btn-default">+1</a>
<a href="upd.do?id=${item.goods.id}&type=-1" class="btn btn-default">-1</a>
</td>
</tr>
</c:forEach>
</tbody></table>
<h2>sum:${total}</h2>
</div>
</body></html>