4.23
程序员文章站
2022-07-12 13:54:42
...
过滤器
内容:
1.过滤器
2.正则表达式
3.log4j
4.Servelt
5.多表联合查询
日期:2020.04.23
作者:爱猫狗的小郝_河南济源
package hsy.cool.filter;
import hsy.cool.util.MybatisUtils;
import org.apache.log4j.Logger;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.regex.Pattern;
@WebFilter(urlPatterns = "/*",filterName = "SqlSessionFilter")
public class SqlSessionFilter implements Filter {
String regex="(/\\w+)*/\\w+\\.(jpg|png|mp4|mp3|rm)";
//日志
Logger log4j=Logger.getLogger(SqlSessionFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
HttpServletRequest hReq=(HttpServletRequest)servletRequest;
//https://127.0.0.1:8080/haosiyuan/a.html|a.js|a.jsp|a.jpg a/b/c/s/sada.jsp
String path=hReq.getServletPath();
boolean mattch=Pattern.matches(regex,path);
log4j.debug(hReq.getRemoteAddr()+"request["+path+"]open SqlSession……");
if(path!=null&&!mattch){
try {
//1.请求
MybatisUtils.open();
log4j.debug(hReq.getRemoteAddr()+"request["+path+"]start SqlSession……");
//2.目标
//放行
filterChain.doFilter(servletRequest,servletResponse);
log4j.debug(hReq.getRemoteAddr()+"request["+path+"]end SqlSession……");
}catch (Exception e){
MybatisUtils.rollback();
log4j.debug(hReq.getRemoteAddr()+"request["+path+"]Exception"+e);
throw e;//为何要显示抛一个异常?
}finally {
//3.释放
MybatisUtils.close();
log4j.debug(hReq.getRemoteAddr()+"request["+path+"]close SqlSession……");
}
}else {
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
log4j.debug(hReq.getRemoteAddr()+"request["+path+"]end!!!!!!!!!!!!!!!!!!!!!!! SqlSession……");
}
@Override
public void destroy() {
}
}
web
UsersServelt
package hsy.cool.web;
import hsy.cool.bean.Users;
import hsy.cool.dao.UsersMapper;
import hsy.cool.util.MybatisUtils;
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(urlPatterns = "/user",name = "UsersServelt")
public class UsersServelt extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String name=req.getParameter("account");
String pwd=req.getParameter("pwd");
Users user=MybatisUtils.getMapper(UsersMapper.class).findByName(name);
if(user!=null&&user.getPwd().equals(pwd)){
resp.sendRedirect("/webAC_war_exploded/index.jsp");
}else {
resp.sendRedirect("/webAC_war_exploded/login.jsp");
}
}
}
NewsServelt
package hsy.cool.web;
import hsy.cool.bean.News;
import hsy.cool.dao.NewsMapper;
import hsy.cool.util.MybatisUtils;
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(urlPatterns = "/list",name = "NewsServelt")
public class NewsServelt extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
NewsMapper newsDao=MybatisUtils.getMapper(NewsMapper.class);
List<News> list=newsDao.findAll();
req.setAttribute("list",list);
req.getRequestDispatcher("/news/list.jsp").forward(req,resp);
}
}
日期工具类
package hsy.cool.util;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
static SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static String parse(Date date){
return df.format(date);
}
}
Bean
package hsy.cool.bean;
import hsy.cool.util.DateUtils;
import java.util.Date;
public class News {
private int id;
private String title;
private String content;
private int users_id;
private int category_id;
private Date pubtime;
private String keywords;
private int state;
private int check_users_id;
private Date check_time;
private int is_elite;
private String usersName;
private String categoryName;
private String checkUsersName;
public String getPubtimeString(){
return DateUtils.parse(pubtime);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getUsers_id() {
return users_id;
}
public void setUsers_id(int users_id) {
this.users_id = users_id;
}
public int getCategory_id() {
return category_id;
}
public void setCategory_id(int category_id) {
this.category_id = category_id;
}
public Date getPubtime() {
return pubtime;
}
public void setPubtime(Date pubtime) {
this.pubtime = pubtime;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public int getCheck_users_id() {
return check_users_id;
}
public void setCheck_users_id(int check_users_id) {
this.check_users_id = check_users_id;
}
public Date getCheck_time() {
return check_time;
}
public void setCheck_time(Date check_time) {
this.check_time = check_time;
}
public int getIs_elite() {
return is_elite;
}
public void setIs_elite(int is_elite) {
this.is_elite = is_elite;
}
public String getUsersName() {
return usersName;
}
public void setUsersName(String usersName) {
this.usersName = usersName;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getCheckUsersName() {
return checkUsersName;
}
public void setCheckUsersName(String checkUsersName) {
this.checkUsersName = checkUsersName;
}
}
Dao
NewsMapper
package hsy.cool.dao;
import hsy.cool.bean.News;
import java.util.List;
public interface NewsMapper {
void delete(int id);
List<News> findAll();
}
NewsMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--!DOCTYPE configuration SYSTEM "mybatis-3-config.dtd" -->
<mapper namespace="hsy.cool.dao.NewsMapper">
<!--删除-->
<delete id="delete" parameterType="int">
delete from n_news where id=#{id}
</delete>
<select id="findAll" resultType="hsy.cool.bean.News">
select
n.id,
title,
content,
users_id,
u.nickname usersName,
category_id,
c.name categoryName,
pubtime,
keywords,
state,
check_users_id,
u1.nickname checkUsersName,
is_elite
from n_news n inner join n_users u on n.users_id=u.id
inner join n_category c on n.category_id=c.id
inner join n_users u1 on n.check_users_id=u1.id
</select>
</mapper>
jsp页面
index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<p>
<h1> 首页</h1>
</p>
<h2>欢迎登录!</h2>
<ul>
<li><a href="/webAC_war_exploded/list">新闻</a></li>
</ul>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<h1>登录页面</h1>
<form action="/webAC_war_exploded/user" method="post">
<p>
账号:<input type="text" name="account" />
</p>
<p>
密码: <input type="password" name="pwd">
</p>
<p>
<input type="submit" value="登录">
</p>
</form>
</body>
</html>
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>新闻列表</title>
</head>
<body>
<p>首页/新闻</p>
<h1>新闻列表</h1>
<table width="1000" border="1">
<tr>
<th>编号</th>
<th>标题</th>
<th>内容</th>
<th>分类</th>
<th>作者</th>
<th>关键字</th>
<th>发布时间</th>
<th>审批</th>
</tr>
<c:forEach items="${list}" var="news">
<tr>
<td>${news.id}</td>
<td>${news.title}</td>
<td>${news.content}</td>
<td>${news.categoryName}</td>
<td>${news.usersName}</td>
<td>${news.keywords}</td>
<td>${news.pubtimeString}</td>
<td>${news.checkUsersName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>