servlet filters 介绍和使用

Spring Boot如何支持嵌入式Servlet容器【详解】 前言: 这篇文章主要介绍了Spring Boot如何支持嵌入式Servlet容器,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下。如果需要其他学习资料或教程的,可以点击进入,暗号:csyy,绝对免费提供!因为资料太多无法全部展示,这只是其中的一点点,需要学习的朋友,可以进来和其他小伙伴一起探讨学习! Spring Boot支持Tomcat、Jetty和Undertow三种Servlet容器嵌入到Web应用程序中,开发者使用starter即可方便嵌入,默认情况下,嵌入服务器的访问端口为 阅读详情

from : http://servlets.com/soapbox/filters.html

如何利用本文:

1.学习一下用的filter

2.学习写自己的filters



Servlet filters
In case you aren't yet familiar, a filter is an object that can transform arequest or modify a response. Filters are not servlets; they don't actuallycreate a response. They are preprocessors of the request before it reaches aservlet, and/or postprocessors of the response leaving a servlet. As you'llsee later in the examples, a filter can:

  • Intercept a servlet's invocation before the servlet is called
  • Examine a request before a servlet is called
  • Modify the request headers and request data by providing a customizedversion of the request object that wraps the real request
  • Modify the response headers and response data by providing a customizedversion of the response object that wraps the real response
  • Intercept a servlet's invocation after the servlet is called
You can configure a filter to act on a servlet or group of servlets. Zero ormore filters can filter one or more servlets. A filter implementsjavax.servlet.Filter and defines its three methods:
  1. void init(FilterConfig config) throws ServletException:Called before the filter goes into service, and sets the filter'sconfiguration object
  2. void destroy(): Called after the filter has been taken outof service
  3. void doFilter(ServletRequest req, ServletResponse res, FilterChainchain) throws IOException, ServletException: Performs the actualfiltering work
The server calls init(FilterConfig) once to prepare the filterfor service, then calls doFilter() any number of times forrequests specially set up to use the filter. The FilterConfiginterface has methods to retrieve the filter's name, its initparameters, and the active servlet context. The server callsdestroy() to indicate that the filter is being taken out ofservice. The filter lifecycle is now very similar to the servlet lifecycle --a change recently made in the Servlet API 2.3 Public Final Draft #2.Previously the lifecycle involved a setFilterConfig(FilterConfig)method.

In its doFilter() method, each filter receives the currentrequest and response, as well as a FilterChain containing thefilters that still must be processed. In the doFilter() method,a filter may do what it wants with the request and response. (It could gatherdata by calling their methods, or wrap the objects to give them new behavior,as I'll discuss later.) The filter then calls chain.doFilter()to transfer control to the next filter. When that call returns, a filter can,at the end of its own doFilter() method, perform additional workon the response; for instance, it can log information about the response. Ifthe filter wants to halt the request processing and gain full control of theresponse, it can intentionally not call the next filter.


filter lifecycle :

server calls : init(FilterConfig)--> doFilter()-->destroy()

Spot the slowdown :
To really understand filters, you have to see them in action. The firstfilter we'll look at is simple but powerful; it records the duration of allrequests. It's modeled loosely after the non-descriptively namedExampleFilter from the Tomcat 4.0 distribution. Here is thecode:

1) Filter codes :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TimerFilter implements Filter {

  private FilterConfig config = null;

  public void init(FilterConfig config) throws ServletException {
    this.config = config;
  }

  public void destroy() {
    config = null;
  }

  public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException, ServletException {

    long before = System.currentTimeMillis();
    chain.doFilter(request, response);
    long after = System.currentTimeMillis();

    String name = "";
    if (request instanceof HttpServletRequest) {
      name = ((HttpServletRequest)request).getRequestURI();
    }
    config.getServletContext().log(name + ": " + (after - before) + "ms");
  }
}
When the server calls init(), the filter saves a reference to theconfig in its config variable, which is later used in thedoFilter() method to retrieve the ServletContext.When the server calls doFilter(), the filter times how long therequest handling takes and logs the time once processing has completed. Thisfilter nicely demonstrates before- and after-request processing. Notice thatthe parameters to the doFilter() method are not HTTP-awareobjects, so to call the HTTP-specific getRequestURI() methodrequires a cast of the request to an HttpServletRequest type.

2) web.xml  配置:

To use this filter, you must declare it in the web.xml deploymentdescriptor using the <filter> tag, as shown below:

<filter>
    <filter-name>timerFilter</filter-name>
    <filter-class>TimerFilter</filter-class>
</filter>
This notifies the server that a filter named timerFilter isimplemented in the TimerFilter class. You can apply a registeredfilter to certain URL patterns or servlet names using the<filter-mapping> tag:
<filter-mapping>
    <filter-name>timerFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
This configures the filter to operate on all requests to the web application(static or dynamic), just what we want for our timing filter. If you connectto a simple page, the log output might look like this:
2001-05-25 00:14:11 /timer/index.html: 10ms
In Tomcat 4.0 beta5, you'll find the log file underserver_root/logs/.

JSP+servlet+filters+JS+CSS+JBean编写新闻管理系统 JSP+servlet+filters+JS+CSS+JBean编写新闻管理系统,自己学习过程编写,适合学习下载学习使用 带有数据库在src中 sql server 2000的 立即下载

相关推荐

ESP32实战:破解校园网WPA2-Enterprise认证的完整指南

本文详细介绍了如何使用ESP32开发板破解校园网WPA2-Enterprise认证的完整指南。通过硬件准备、环境搭建、核心代码实现及常见问题排查,帮助开发者解决企业级WiFi连接难题,特别适用于校园网环境下的物联网设备开发。

weixin_29194817的博客 462

SpringMVC学习之请求与响应

SpringMVC请求与响应学习

weixin_50943926的博客 811

Agent框架系列:1-低代码智能体平台深度解析Dify、Coze、n8n

Dify、Coze 和 n8n 是三大主流低代码智能体平台,各有侧重。Dify 适合企业级 AI 应用,具备强大的 RAG 和知识库管理能力,技术栈为 Python + React。Coze 由字节推出,主打零代码和快速原型开发,集成 60+ 插件,极适合非技术用户。n8n 以工作流自动化和系统集成为核心,支持 400+ 外部应用,基于 Node.js + Vue,扩展性极强。选择建议:非技术团队选 Coze,AI 应用选 Dify,复杂自动化与集成选 n8n。可根据实际需求混合使用,最大化平台优势。

3099

springmvc - 拦截器

注意特别注意这里面的设定的访问路径(这里面我们设定是"/books"和"/books/*"的访问路径),只有设定了访问的路径时候,客户端访问的路径如果是我们设定的这种路径的话,那么拦截器类才能生效,才能决定放不放行,如果没设定用户如"/users"格式的访问路径,那么拦截器对这种用户的资源访问路径是不生效的。preHandle方法返回值为false时(不放行,不允许用户调用/books路径下的资源)因为拦截器类中的preHandle方法(专门决定让不让用户访问的资源调用的方法)...

lwj_07的博客 546

使用嘉立创绘制STM32F103T8C6最小系统板

使用嘉立创EDA绘制一个属于自己的STM32最小系统板

weixin_67887035的博客 1万+

Kafka 入门篇(一)

初识 Kafka 什么是 Kafka Kafka 的基本术语 Kafka 的特性(设计原则) Kafka 的使用场景 Kafka 的消息队列 点对点模式的消息队列 发布-订阅模式的消息队列 Kafka 系统架构 核心 API Kafka 为何如此之快 broker 端配置 broker.id port zookeeper.connect log.dirs num.recovery.threads.per.data.dir auto.create.topics.enable topic默认配置 num.par

1247

Spring MVC利用getServletFilters注册过滤器

在Spring MVC中,当你使用AbstractAnnotationConfigDispatcherServletInitializer类的getServletFilters方法来注册过滤器时,这些过滤器默认会应用于DispatcherServlet所处理的所有请求。DispatcherServlet通常是Spring MVC应用中的前端控制器,它负责处理进入应用的所有Web请求,并将这些请求分发给适当的控制器。

谷哥的小弟 5865

AD(Altium Designer)器件封装——立创商城导出原理图和PCB完成器件封装操作指南

官网:www.SZLCSC.COM以芯片为例器件类——>芯片类——>对应芯片确定芯片——>数据手册1:原理图2:PCB3:打开操作流程选中原理图——>文件——>导出——>AD1: ✔2:导出AD文件操作流程选中PCB图——>文件——>导出——>AD1: ✔2:导出AD文件1:PCB图2:原理图操作流程文件——>打开操作流程选中文件——>打开操作流程项目文件——>添加新的...到工程——>Schematic Library新原理图库操作流程项目文件——>添加新的...到工程——>PCB Library新的PC

XU157303764的博客 5449

javaWeb---Filter

文章目录Filter配置方式Filter工作流程生命周期Filter 链中 Filter 的执行顺序代码编码方式过滤 Filter 过滤器要实现 javax.servlet.Filter 接口: 在 Web 资源被访问前,检查 request 对象,修改请求头和请求正文,或对请求进行预处理操作。 将请求传递到下一个过滤器或目标资源。 在 Web 资源被访问后,检查 response 对象,修改响应头和响应正文。 配置方式 注解 Web.xml Filter工作流程 1.客户端请求访问容器内的 We

卿本佳人的博客 910

SAP-ABAP:SAP Open SQL 分页技术深度解析:语法、性能陷阱与最佳实践

SAP Open SQL 分页技术深度解析:语法、性能陷阱与最佳实践

baidu_35680696的博客 643

YOLOv12改进策略【独家融合改进】| SPD-Conv+PPA 再次提升模型针对小目标的特征提取能力

SPD-Conv是一种新的 CNN 构建模块,用于替代传统 CNN 架构中使用的步长卷积(strided convolution)和池化(pooling)层,它由空间到深度(Space-to-depth,SPD)层和非步长卷积(non - strided convolution)层组成。

Limiiiing的博客 1032

Servlet filters

Servlet filters In case you aren't yet familiar, a filter is an object that can transform arequest or modify a response. Filters are not servlets; they don't actuallycreate a response. They are prepr

美好的未来在于把握今天 274

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

SpringMVC之五:自定义DispatcherServlet配置及配置额外的 servlets 和 filters

相关文章 《Servlet3.0之四:动态注册和Servlet容器初始化》 《SpringBoot中通过SpringBootServletInitializer如何实现组件加载》 《SpringMVC之五:自定义DispatcherServlet配置及配置额外的 servlets 和 filters》 一、web容器如何初始化第三方组件(servlet、filters) 在web容器启动...

weixin_30548917的博客 143

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

整合Servlets, Filters和listeners

一. Spring Boot中Servlets,Filters和Listeners1. 概述 在Spring Boot中使用内嵌servlet容器时,我们可以通过使用Spring beans或扫描Servlet组件的方式注册Servlets,Filters及特定Servlet相关的所有listeners(比如HttpSessionListener)。 Spring Boot 提供了 Servlet...

qianfeng_dashuju的博客 229

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

javax.servlet.Filter运用

Servlets Filter 是Servlet 2.3 规范中新增加的,它是截取用户从客户端提交的请求,在还没有到达需要访问的资源时运行的一个类。它操纵来自客户端的请求,在资源还没有初发送到客户端前截取响应,并处理这些还没有发送到客户端的响应。Filters 有许多的应用场合。Servlet 2.3 规范建议如下的地方可以应用Filter:authentication filters loggi

JAVAvNET的专栏 3839

Spring Boot 内嵌servlet容器的Servlets +Filters + listeners

一:servlet (1)启动类 @SpringBootApplication @ServletComponentScan public class Application { public static void main(String[] args){ SpringApplication.run(Application.class, args)...

a8865423的博客 207

浅谈servlet版本

说白话,eclipseJ2EE版本新建一个web项目后,在IDE中的项目根目录下会看到2.3,2.4,2.5,3.0,3.1。。。。据说最新的4.0在路上,已经有草案了,很期待ing。 360百科是这么描述的: Servlet(Server Applet),全称Java Servlet,未有中文译文。是用Java编写的服务器端程序。其主要功能在于交互式地浏览和修改数据,生成动态Web...

95

SpringSecurity------Filters Architecture(七)

SpringSecurity------ The Big Picture (六)一、A Review of Filters二、DelegatingFilterProxy三、FilterChainProxy四、SecurityFilterChain五、Security Filters六、Handling Security Exceptions 一、A Review of Filters Spring Security’s Servlet support is based on Servlet Filters,

豢龙先生 440

java 设置运行参数上下文_第四章 Servlet上下文(JavaTM Servlet 规范3.1 )

Servlet Context(Servlet上下文)4.1 Introduction to the ServletContext Interfacen ServletContext接口介绍ServletContext 接口定义了运行在Web应用中的 servlet 的视图。容器提供者有义务提供在此 servlet 容器中 ServletContext 接口的一个实现。利用 ServletCon...

weixin_34274954的博客 685

23.Spring-Boot中Servlet容器的支持

Spring Boot中内嵌了Tomcat、Jetty和Undertow服务器。大多数开发人员将只需使用适当的“启动器”来获得一个完全配置的实例。默认情况下,内嵌的服务器HTTP请求监听8080端口。 注意:如果您选择在CentOS上使用Tomcat,请注意,默认情况下,临时目录是。用于存储已编译的jsp、文件上传等。当应用程序运行导致失败时,该目录可以就被tmpwatch删...

盲目的拾荒者的博客 1459

kaptcha实现验证码(Servlet)

一、下载架包和依赖架包,并导入项目中 链接 https://mvnrepository.com/artifact/com.github.penggle/kaptcha https://mvnrepository.com/artifact/com.jhlabs/filters/2.0.235-1 二、配置web.xml <servlet> <!-- ...

lxp_1101的博客 1006
上一篇: Java applet 在JSP中的使用
下一篇: Eclipse 快捷键
dingxy
博客等级 码龄19年 88粉丝 460原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值