JavaWeb快速入门--Filter&Listener,mybatis源码分析pdf百度云

关于拦截:

  • 拦截路径配置:

1. 具体资源路径: /index.jsp 只有访问index.jsp资源时,过滤器才会被执行

2. 拦截目录: /user/* 访问/user下的所有资源时,过滤器都会被执行

3. 后缀名拦截: *.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行

4. 拦截所有资源:/* 访问所有资源时,过滤器都会被执行

  • 过滤器的四种拦截方式:REQUEST、FORWARD、INCLUDE、ERROR

| 拦截方式 | 功能描述 |

| — | — |

| REQUEST | 直接访问目标资源时执行过滤器。包括:在地址栏中直接访问、表单提交、超链接、重定向,只要在地址栏中可以看到目标资源的路径,就是REQUEST |

| FORWARD | 转发访问执行过滤器。包括RequestDispatcher#forward()方法、<jsp:forward>标签都是转发访问 |

| INCLUDE | 包含访问执行过滤器。包括RequestDispatcher#include()方法、<jsp:include>标签都是包含访问 |

| ERROR | 当目标资源在web.xml中配置为中时,并且真的出现了异常,转发到目标资源时,会执行过滤器 |

注解配置:

  • @WebFilter(value="/index.jsp",dispatcherTypes = DispatcherType.REQUEST)

  • @WebFilter(value="/*",dispatcherTypes ={ DispatcherType.FORWARD,DispatcherType.REQUEST})

web.xml配置:<dispatcher>REQUEST</dispatcher>

案例:字符编码转换

@WebFilter("/*")

public class CharchaterFilter implements Filter {

protected String encoding;

public void destroy() {

// TODO Auto-generated method stub

}

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

//将父接口转换为子接口

HttpServletRequest request=(HttpServletRequest)req;

HttpServletResponse response=(HttpServletResponse)res;

//获取请求方法

String method=request.getMethod();

//解决post请求中文数据乱码问题

if(method.equalsIgnoreCase(“post”)) {

request.setCharacterEncoding(“utf-8”);

}

//处理响应乱码

response.setContentType(“text/html;charset=utf-8”);

chain.doFilter(request,response);

}

public void init(FilterConfig fConfig) throws ServletException {

// TODO Auto-generated method stub

}

}

案例:敏感词过滤

/**

  • 敏感词汇过滤器

*/

@WebFilter("/*")

public class SensitiveWordsFilter implements Filter {

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

//1.创建代理对象,增强getParameter方法

ServletRequest proxy_req = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {

@Override

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

//增强getParameter方法

//判断是否是getParameter方法

if(method.getName().equals(“getParameter”)){

//增强返回值

//获取返回值

String value = (String) method.invoke(req,args);

if(value != null){

for (String str : list) {

if(value.contains(str)){

value = value.replaceAll(str,"***");

}

}

}

return value;

}

//判断方法是否是: getParameterMap

//判断方法是否是: getParameterValue

return method.invoke(req,args);

}

});

//2.放行

chain.doFilter(proxy_req, resp);

}

private List list = new ArrayList();//敏感词汇集合

public void init(FilterConfig config) throws ServletException {

try{

//1.获取文件真实路径

ServletContext servletContext = config.getServ

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

letContext();

String realPath = servletContext.getRealPath("/WEB-INF/classes/敏感词汇.txt");

//2.读取文件

BufferedReader br = new BufferedReader(new FileReader(realPath));

//3.将文件的每一行

String line = null;

while((line = br.readLine())!=null){

list.add(line);

}

br.close();

System.out.println(list);

}catch (Exception e){

e.printStackTrace();

}

}

public void destroy() {

}

}

/**

  • 验证方法

*/

@WebServlet("/testServlet")

public class TestServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String name = request.getParameter(“name”);

String msg = request.getParameter(“msg”);

System.out.println(name+":"+msg);

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

this.doPost(request, response);

}

}

过滤器JavaWeb三大组件之一,当我们请求服务器的资源时,过滤器会在这组资源之前执行,它可以将我们的请求拦截下来,判断是否让我们访问这个资源,并完成一些特殊的功能。过滤器一般用于完成通用的操作。如:登录验证、统一编码处理、敏感字符过滤…

Listener:监听器


监听器JavaWeb的三大组件之一,在我们执行请求之前执行,主要用于监听Web服务器中的某一个执行动作,并根据其要求做出响应的响应。

目前Servlet中包含8个Listener接口,可以将其归纳为3类:

  • ServletContextListener:监听ServletContext对象的创建和销毁

  • 方法:

  • void contextDestroyed(ServletContextEvent sce) :ServletContext对象被销毁之前会调用该方法

  • void contextInitialized(ServletContextEvent sce) :ServletContext对象创建后会调用该方法

  • 步骤:

  1. 定义一个类,实现ServletContextListener接口

  2. 复写方法

  3. 配置

  4. web.xml

cn.itcast.web.listener.ContextLoaderListener

  • 指定初始化参数
  1. 注解:
  • @WebListener

  • 代码实现

@WebListener

public class ContextLoaderListener implements ServletContextListener {

/**

  • 监听ServletContext对象创建的。ServletContext对象服务器启动后自动创建

  • 在服务器启动后自动调用

  • @param servletContextEvent

*/

@Override

public void contextInitialized(ServletContextEvent servletContextEvent) {

//加载资源文件

//1.获取ServletContext对象

ServletContext servletContext = servletContextEvent.getServletContext();

//2.加载资源文件

String contextConfigLocation = servletContext.getInitParameter(“contextConfigLocation”);

//3.获取真实路径

String realPath = servletContext.getRealPath(contextConfigLocation);

//4.加载进内存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值