首页 > 其他分享 >servlet、filter 顺序

servlet、filter 顺序

时间:2023-04-26 19:37:03浏览次数:43  
标签:bin 顺序 HelloFilter filter org apache import servlet


java代码:

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("HelloServlet");
	}

}



package com.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class HelloFilter implements Filter {

	public void destroy() {
		System.out.println("HelloFilter.destroy");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("HelloFilter.doFilter before");
		chain.doFilter(request, response);
		System.out.println("HelloFilter.doFilter after");
	}

	public void init(FilterConfig fConfig) throws ServletException {
		System.out.println("HelloFilter.init");
	}

}



web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>servlet-filter</display-name>
	<welcome-file-list>
		<welcome-file>HelloServlet</welcome-file>
	</welcome-file-list>
	<servlet>
		<description></description>
		<display-name>HelloServlet</display-name>
		<servlet-name>HelloServlet</servlet-name>
		<servlet-class>com.servlet.HelloServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HelloServlet</servlet-name>
		<url-pattern>/HelloServlet</url-pattern>
	</servlet-mapping>
	<filter>
		<display-name>HelloFilter</display-name>
		<filter-name>HelloFilter</filter-name>
		<filter-class>com.filter.HelloFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HelloFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>



输出:

2014-5-5 22:38:09 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_05\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk1.6.0_05/bin/../jre/bin/client;C:/Program Files/Java/jdk1.6.0_05/bin/../jre/bin;C:/Program Files/Java/jdk1.6.0_05/bin/../jre/lib/i386;F:\software\apache-maven\apache-maven-3.2.1\bin;C:\Program Files\Java\jdk1.6.0_05\bin;C:\Program Files\Java\jdk1.6.0_05\jre\bin;C:\oracle\product\10.2.0\db_1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Rational\common;F:\software\java\eclipse-jee-juno-SR2-win32\eclipse;
2014-5-5 22:38:09 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:servlet-filter' did not find a matching property.
2014-5-5 22:38:10 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2014-5-5 22:38:10 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 601 ms
2014-5-5 22:38:10 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2014-5-5 22:38:10 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.29
HelloFilter.init
2014-5-5 22:38:10 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2014-5-5 22:38:10 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2014-5-5 22:38:10 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/47  config=null
2014-5-5 22:38:10 org.apache.catalina.startup.Catalina start
信息: Server startup in 494 ms
HelloFilter.doFilter before
HelloServlet
HelloFilter.doFilter after



看到:

HelloFilter.doFilter before
HelloServlet
HelloFilter.doFilter after

标签:bin,顺序,HelloFilter,filter,org,apache,import,servlet
From: https://blog.51cto.com/u_1002776/6228659

相关文章

  • freemarker 与 servlet 之 Helloworld版本
    新建web工程,新建servlet类:packagecom.freemaker.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.HashMap;importjava.util.Map;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.......
  • 后台servlet OrderListSer
    packagecn.service.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.h......
  • 后台servlet LoginOfAdminSer
    packagecn.service.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse......
  • 后台servlet SideMenuSer
    packagecn.service.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse......
  • PYTHON FILTER
    Filter顾名思义,filter过滤列表中的元素,并且返回一个由所有符合要求的元素所构成的列表,符合要求即函数映射到该元素时返回值为True.这里是一个简短的例子:number_list=range(-5,5)less_than_zero=filter(lambdax:x<0,number_list)print(list(less_than_zero))#......
  • How use the RegExp to filter IP address in js All In One
    HowusetheRegExptofilterIPaddressinjsAllInOne如何使用RegExp在js中过滤IP地址192.168.18.1<192.168.18.N<192.168.18.255ignoreIPs:192.168.18.0/192.168.18.1/192.168.18.255errorfunctiontest(n){letreg=/192\.168\.(1?[0-9][0-......
  • Servlet添加自定义的过滤器没有效果?
    在学习HttpServlet的时候有个自定义过滤器的定义类,我们想让特定url走过滤器。publicclassMyFilterimplementsFilter{privateFilterConfigconfig;publicvoidinit(FilterConfigconfig)throwsServletException{this.config=config;}publi......
  • @PostConstruct的顺序
    其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入。所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入是发生在A的构造方法执行完之后的。如果想在生成对象时候完成某些初始化操作,而偏偏这些初始化操作又......
  • Filter概述及快速入门
    Filter概述及快速入门Filter概述:概念:Filter表示过滤器,是JavaWeb三大组件之一JavaWeb三大组件:Servlet,Filter,Listener过滤器可以把对资源的请求拦截下来,从而实现一些特殊的功能过滤器一般完成一些通用的操作:比如:权限控制,统一编码处理,敏感字符处理等......
  • PTS,SLM,Filter三种降低PAPR方法的matlab仿真
    1.算法仿真效果matlab2022a仿真结果如下:    2.算法涉及理论知识概要         正交频分复用(OFDM)是长期演进的4G蜂窝网络标准中采用的调制技术。但是,OFDM具有较强的带外辐射和较高的旁瓣,对频谱的感知精度低下。未来的无线标准需要为下一代移动系统提出具有......