博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tomcat:为Filter过滤器设置参数
阅读量:6279 次
发布时间:2019-06-22

本文共 3024 字,大约阅读时间需要 10 分钟。

hot3.png

使用netbeans8创建web项目HelloJSP,项目完成时结构如下:

web.xml内容如下:

FirstFilter
letian.filter.FirstFilter
name1
letian1
name2
letian2
FirstFilter
/*

可以看到,过滤器FirstFilter有两个参数name1和name2,值分别是letian1、letian2。

letian/filter/FirstFilter.java内容如下:

package letian.filter;import java.io.IOException;import java.io.PrintWriter;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 FirstFilter implements Filter {        private String filterName;    private String name1;    private String name2;        public void init(FilterConfig filterConfig) throws ServletException {        this.filterName = filterConfig.getFilterName();        this.name1 = filterConfig.getInitParameter("name1");        this.name2 = filterConfig.getInitParameter("name2");    }    public void doFilter(ServletRequest request, ServletResponse response,            FilterChain chain) throws IOException, ServletException {                System.out.println("filterName:"+filterName);        System.out.println("name1:"+name1);        System.out.println("name2:"+name2);                chain.doFilter(request, response);     }        public void destroy() { }}

letian/servlet/Home.java内容如下:

package letian.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet(name = "Home", urlPatterns = {"/"})public class Home extends HttpServlet {            protected void processRequest(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        System.out.println("I am home!"); // 输出到控制台        response.setContentType("text/html;charset=UTF-8");        try (PrintWriter out = response.getWriter()) {            out.println("

Hello

"); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }}

效果:

启动tomcat 8后,访问http://localhost:8080/HelloJSP/,可以看到tomcat输出:
filterName:FirstFiltername1:letian1name2:letian2I am home!

参考:

http://blog.csdn.net/home_zhang/article/details/7754634

转载于:https://my.oschina.net/letiantian/blog/369035

你可能感兴趣的文章
Linux(centos)系统各个目录的作用详解 推荐
查看>>
zabbix监控进程与端口
查看>>
Libvirsh 问题:GLib-WARNING **: gmem.c:483: custom memory allocation vtable not supported
查看>>
COALESCE函数
查看>>
Ext.require callback 不执行
查看>>
面试题:连续子数组的最大和
查看>>
书生教你cocos2d-x-入门篇(一)
查看>>
Linux—yum环境的三种搭建方法
查看>>
Windows Server 2016-命令行批量导出AD用户信息
查看>>
Spring Security 过滤流程
查看>>
Vue transition源码浅析
查看>>
如何提升团队的研发效率?来听听阿里研发专家是怎么说的
查看>>
Django-关于manage.py migrate无效的问题
查看>>
eclipse maven创建web工程2.0转3.0
查看>>
FTP 服务器上传文件 553 Could not create file
查看>>
this的用法
查看>>
windows下安装redis
查看>>
CentOS7 yum 安装git
查看>>
启动日志中频繁出现以下信息
查看>>
httpd – 对Apache的DFOREGROUND感到困惑
查看>>