过滤器的使用
Posted eddy-s
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了过滤器的使用相关的知识,希望对你有一定的参考价值。
使用过滤器,代码如下:
package pers.eddy.hello; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String usr = req.getParameter("usr"); String psd = req.getParameter("psd"); System.out.println(usr); resp.getWriter().print("您的用户id:"+usr+", 密码:"+psd); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>pers.eddy.hello.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> <filter> <filter-name>MyFilter</filter-name> <filter-class>pers.eddy.hello.MyFilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
以上是关于过滤器的使用的主要内容,如果未能解决你的问题,请参考以下文章