❶ java里面过滤器和拦截器的区别
java里面过滤器和拦截器的区别:
①拦截器是基于java的反射机制的,而过滤器是基于函数回调。
②拦截器不依赖与servlet容器,过滤器依赖与servlet容器。
③拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用。
④拦截器可以访问action上下文、值栈里的对象,而过滤器不能访问。
⑤在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次。
⑥拦截器可以获取IOC容器中的各个bean,而过滤器就不行,这点很重要,在拦截器里注入一个service,可以调用业务逻辑。
拦截器:是在面向切面编程的就是在你的service或者一个方法,前调用一个方法,或者在方法后调用一个方法比如动态代理就是拦截器的简单实现,在你调用方法前打印出字符串(或者做其它业务逻辑的操作),也可以在你调用方法后打印出字符串,甚至在你抛出异常的时候做业务逻辑的操作。
下面通过实例来看一下过滤器和拦截器的区别:
使用拦截器进行/admin 目录下jsp页面的过滤
<package name="newsDemo" extends="struts-default"
namespace="/admin">
<interceptors>
<interceptor name="auth" class="com.test.news.util.AccessInterceptor" />
<interceptor-stack name="authStack">
<interceptor-ref name="auth" />
</interceptor-stack>
</interceptors>
<!-- action -->
<action name="newsAdminView!*" class="newsAction"
method="{1}">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authStack">
</interceptor-ref>
下面是我实现的Interceptor class:
package com.test.news.util;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.test.news.action.AdminLoginAction;
public class AccessInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -4291195782860785705L;
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext actionContext = actionInvocation.getInvocationContext();
Map session = actionContext.getSession();
//except login action
Object action = actionInvocation.getAction();
if (action instanceof AdminLoginAction) {
return actionInvocation.invoke();
}
//check session
if(session.get("user")==null ){
return "logout";
}
return actionInvocation.invoke();//go on
}
}
过滤器:是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts的 action进行业务逻辑,比如过滤掉非法url(不是login.do的地址请求,如果用户没有登陆都过滤掉),或者在传入servlet或者 struts的action前统一设置字符集,或者去除掉一些非法字符.
使用过滤器进行/admin 目录下jsp页面的过滤,首先在web.xml进行过滤器配置:
<filter>
<filter-name>access filter</filter-name>
<filter-class>
com.test.news.util.AccessFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>access filter</filter-name>
<url-pattern>/admin
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
HttpSession session = request.getSession();
if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){
response.sendRedirect("login.jsp");
return ;
}
filterChain.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
❷ javaweb中怎么添加过滤器
web.xml中配置,如filter>
<!-- Filter的名字 -->
<filter-name>log</filter-name>
<!-- Filter的实现类 -->
<filter-class>lee.LogFilter</filter-class>
</filter>
<!-- 定义Filter拦截的版URL地址 -->
<filter-mapping>
<!-- Filter的名字 -->
<filter-name>log</filter-name>
<!-- Filter负责拦截的URL 全部以/的请求,如果<url-pattern>/*.action </>,将会权以拦截*.action的请求-->
<url-pattern>/*</url-pattern>
</filter-mapping>
❸ java web项目中过滤器的使用(过滤器执行多次)
图片中显示的log是执行一次的。
不是本来就打开login.jsp吗,不用再跳转到login.jsp,不是的时候,再跳转。
❹ java web项目里面的加了过滤器,在自己电脑上面测试项目的时候就打不开网页了,怎么回事
这是我自己写过的一个登陆过滤器
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
System.out.println("=============LoginFilter============");
//将父类转成子类
HttpServletRequest request=(HttpServletRequest) req;
//javaweb_T7/LoginServlet
String uri=request.getRequestURI();
//控制登录的
HttpSession session=request.getSession();
String userName=(String) session.getAttribute("userName");
//如果没有登录就跳转
//要么你session中有东西(已经登录) ,要么你是登录的动作
if(userName!=null || uri.indexOf("LoginServlet")!=-1){
//如果登录过,就不阻拦
chain.doFilter(req, res);
}else{
request.setAttribute("msg", "请先登录后操作");
request.getRequestDispatcher("login.jsp")
.forward(request, res);
}
}
❺ Java Web 过滤器
路径配置错误,你的来filter包应该放在WEB-INFO/lib/下面源。所有的class路径都会到lib或classes目录下面找,肯定不会去你的/common/classes/zs/simplefilter/找类。
是打成jar包放到lib下的不? 在lib下的要是.jar的。
❻ 用javaweb怎样实现过滤器
public class FilterImpl implements Filter{
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
/**
*根据自己的需要,编写相应功能内的过滤语容句
*/
chain.doFilter(request, response);
}
}else{
chain.doFilter(request, response);
}
}
public void init(FilterConfig arg0) throws ServletException {
System.out.println("---程序已启动---");
}
}
❼ Java Web 中的过滤器如何使用
你你在配置过来滤器的自 时候
配置web.xml
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
最后的<url-pattern>/*</url-pattern> 就是过滤的路径
比如你在文件夹下吧不过滤的放在根目录,过滤的放在一个filter的文件夹下,那么就配<url-pattern>/filter/*</url-pattern> 这样根目录的所有文件都不过滤,名字我随便起的,你可以根据你自己的写,或者这个*也可以用匹配符,比如<url-pattern>/*.jsp</url-pattern>
再或者<url-pattern>/java*</url-pattern> 这样只有以java开头的才过滤,其他的不过滤
你可以根据实际设置
❽ java 过滤器必须要配置在web.xml中吗
servlet3.0开始就可以用注解代替web.xml里面的好多配置了
❾ 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 WEB中登陆过滤器的问题
你先把过滤器new出来,再调用不就好了
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
String name = request.getParameter("username");
String password = request.getParameter("password");
HttpSession session = request.getSession();
if(name.equals("admain") && password.equals("admain")){
session.setAttribute("usertype", "admain");
Fileter f=new Fileter();
f.doFilter(request,response,chain);//反正你先把过滤器new出来,再调用不就好了,具体的属性看实际情况,我是手写代码,难免有错
}else{
out.print("<a href=Admain.html>用户页面</a>");
}
}