j2ee学习笔记 Filter过滤器

作用:过滤response和request对象的内容

使用:

  Filter是J2EE的一个接口,自定义Filter需要实现该接口,并重写所有的方法.

  Filter提供的方法:

init()

  doFilter()

  destory()

在web.xml中的配置:

* 实现步骤:
* 创建Java类,实现Filter接口,并且重写所有方法.
* 在web.xml文件中进行配置.
<filter>
<filter-name>MyFilter1</filter-name>
<filter-class>filter.MyFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

案例:使用filter解决中文乱码:

Myfilter.java

package 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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //解决全站中文乱码
public class Myfilter1 implements Filter{ @Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
//进行Request和Response对象的类型强转
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
//解决中文乱码问题1.请求的中文乱码Get 和 Post 2.响应中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//3.创建自定义的Request //4.放行
chain.doFilter(request, response);
} @Override
public void init(FilterConfig arg0) throws ServletException {
} }

ServletTest.java

package filter;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ServletTest extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取表单信息
String username = request.getParameter("username");
String password = request.getParameter("password"); System.out.println(username + "--" + password); response.getWriter().println("中文doPost正常"); } }

web.xml配置文件

 <filter>
<filter-name>Myfilter1</filter-name>
<filter-class>filter.Myfilter1</filter-class>
</filter> <filter-mapping>
<filter-name>Myfilter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head> <body>
//表单
<form action="servlet/ServletTest" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

此处action的路径有三种写法:

1.相对路径:"servlet/ServletTest"

2.绝对路径:"/Filter/servlet/ServletTest"

3.获取路径:${pageContext.request.contextPath }

上一篇:自己积累的一些Emgu CV代码(主要有图片格式转换,图片裁剪,图片翻转,图片旋转和图片平移等功能)


下一篇:Java 中的 Filter 过滤器详解