『壹』 java web 过滤器跟拦截器的区别和使用
java web 过滤器跟拦截器的区别和使用分别介绍如下:
1、过滤器的使用
Filter主要对客户端的请求和服务器的响应进行过滤,使用场景:
客户端的请求到达服务器,服务器真正开始处理这个请求之前,要经过Filter的过滤
服务器真正的处理完这个请求,生成响应之后,要经过Filter的过滤,才能将响应发送给客户端
作用:可以通过Filter技术,对web服务器管理的所有web资源,例如JSP、Servlet、静态图片文件或静态 html文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。
配置Filter
同开发Servlet一样,写完了类,接下来就是配置了,我们需要在web.xml文件中配置Filter。具体的配置和Servlet配置如出一辙。
<filter>
<filter-name>log</filter-name>
<filter-class>com.jellythink.practise.LogFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
上面配置中比较重要的就是url-pattern和dispatcher了。
过滤类:
public class LogFilter implements Filter
{
private FilterConfig config;
public void init(FilterConfig config)
{
this.config = config;
}
public void destroy()
{
this.config = null;
}
// 这个方法是Filter的核心方法
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
// 对用户的请求进行处理
ServletContext context = this.config.getServletContext();
long begin = System.currentTimeMillis();
// 输出过滤信息
System.out.println("开始过滤...");
HttpServletRequest hRequest = (HttpServletRequest)request;
System.out.println("Filter已经截获到用户请求的地址:" + hRequest.getServletPath());
// 处理完以后,将请求交给下一个Filter或者Servlet处理
chain.doFilter(request, response);
// 对服务器的响应进行处理
long end = System.currentTimeMillis();
System.out.println("过滤结束");
System.out.println("请求被定为到:" + hRequest.getRequestURI() + "; 所花费的时间为:" + (end - begin));
}
}
2、拦截器的使用:
拦截器的主要作用是拦截用户的请求并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306那样子判断当前时间是否是购票时间。
1.在SpringMVC的配置文件中加上支持MVC的schema
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
下面是声明示例:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
这样在SpringMVC的配置文件中就可以使用mvc标签了,mvc标签中有一个mvc:interceptors是用于声明SpringMVC的拦截器的。
『贰』 java 页面怎么传值给过滤器fileder
在web.xml中添加一个过滤器,该过滤器要集成OncePerRequestFilter。
添加过滤器的代码是:
<filter-mapping>
<url-pattern>/*</url-pattern>
</filter-mapping>
『叁』 java过滤器怎么不过滤一个页面里包含的多个请求
abstract public class FilterPerRequest implements Filter {
private static final Boolean FILTERED = true;
private ThreadLocal<Boolean> statusLocal = new ThreadLocal<Boolean>();
@Override
final public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
if (hasNotFiltering()) {
doFilterAndDispatchRequest(req, resp, chain);
return;
}
//dispatch request only
chain.doFilter(req, resp);
}
private boolean hasNotFiltering() {
Boolean status = statusLocal.get();
if (status == null) {
return true;
}
return false;
}
private void doFilterAndDispatchRequest(ServletRequest req,
ServletResponse resp, FilterChain chain) throws IOException,
ServletException {
try {
doFilterPerRequest(req, resp);
setFiltered();
chain.doFilter(req, resp);
} finally {
cleanFilterStatus();
}
}
abstract protected void doFilterPerRequest(ServletRequest req,
ServletResponse resp);
private void setFiltered() {
statusLocal.set(FILTERED);
}
private void cleanFilterStatus() {
statusLocal.set(null);
}
}
那要就情况而定了。。。
如果是request范围内的话,就可以使用上面的过滤器。
如果是session范围内的话,就需要在session内存储一个标志变量,方式和request差不多。
如果是限制用户操作的话,那就必须将用户操作持久化。
由于你的问题不明确,我也不好多说...
『肆』 java html字符过滤器
这是我看到一个不错的,自己看看吧
import java.util.regex.Pattern;
public class Test
{
public static void main(String[] args)
{
String ww="<html>sss<body>ss</body>ssss</html>";
String ff=html2Text(ww);
System.out.println(ff);
}
public static String html2Text(String inputString) {
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script>]*?>[\s\S]*?<\/script>
// }
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style>]*?>[\s\S]*?<\/style>
// }
String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
textStr = htmlStr;
} catch (Exception e) {
System.err.println("Html2Text: " + e.getMessage());
}
return textStr;
}
}
『伍』 在java中,使用过滤器编码过滤乱码时,过滤器的编码怎么写
public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException {
if (null != encoding) {
HttpServletRequest request = (HttpServletRequest)srequest;
HttpServletResponse response = (HttpServletResponse)sresponse;
// 对请求进行编码设置
request.setCharacterEncoding(encoding); //把请求用指定的方法编码
response.setContentType("text/html");
response.setCharacterEncoding(encoding); //把返回响应指定 编码方式
System.out.println("------------过滤编码-------------" + encoding);
}
// 把处理权限交给下一个过滤器
chain.doFilter(srequest, sresponse);
}//这是核心方法
正好项目中有用到
『陆』 javaweb,给HTML文件加上过滤器以后,HTML显示乱码。
那是你的浏览器不识别Content-Type,采用了默认的GBK/GB2312编码显示,这个可以在打开页面“右键-编码”看到;meta最好给一个结束符,即:<meta http-equiv="content-type" content="text/html; charset=utf-8" />
『柒』 关于sevlet、过滤器、jsp、html、xml等这些javaweb的相关知识和用途,请各位大大们给个详细的解释和区别~
说简单点:servlet就是接抄收页面请求然后转发的。
过滤器就是在servlet之前可以做验证、乱码方面的。
jsp就是java语言中的动态页面.
html就是静态页面。
太详细的话这里1W个字都说不完的.
『捌』 在HTML页面写一个过滤器,实现传进SERVLET的CLASS文件里面的编码机制统一
在页面的请求里面传递中文时候,会变成乱码,所以每次请求都需要 request.setCharacterEncoding("utf-8");
周睿羊做很麻烦,在web.xml里面写一个添加一个过滤器
<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>cn.zcq100.util.CharacterFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
然后建立一个类,实现import javax.servlet.Filter;接口
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chian) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chian.doFilter(request, response);
}
这样每次请求的时候都会先通过这个过滤器过滤。
『玖』 filter的使用 java 过滤器的几种使用方法
过滤器来
过滤器是处于客户端自与服务器资源文件之间的一道过滤网,在访问资源文件之前,通过一系列的过滤器对请求进行修改、判断等,把不符合规则的请求在中途拦截或修改。也可以对响应进行过滤,拦截或修改响应。
过滤器一般用于登录权限验证、资源访问权限控制、敏感词汇过滤、字符编码转换等等操作,便于代码重用,不必每个servlet中还要进行相应的操作。
『拾』 jsp\java如何编写过滤器过滤特殊字符
正则表达式来校验:过滤器就网络一大堆,怎么写正则表达式,也可以网络,不知你说的特殊字符是什么字符,所以只能给方法