一:拦截器 :是在面向切面编程的就是在你的service或者一个方法,前调用一个方法,或者在方法后调用一个方法比如动态代理就是拦截器的简单实现,
在你调用方 法前打印出字符串(或者做其它业务逻辑的操作),也可以在你调用方法后打印出字符串,甚至在你抛出异常的时候做业务逻辑的操作。
1.Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现。package com.lzw.struts.Interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class MyInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = -6410044851077844880L;
/**
* 在struts.xml struts
*/
private String lzw;
public String getLzw() {
return lzw;
}
public void setLzw(String lzw) {
this.lzw = lzw;
}
@Override
public void destroy() {
System.out.println("destroy!");
}
@Override
public void init() {
System.out.println("init!");
}
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("MyInterceptor-start");
System.out.println(lzw);
String result = invocation.invoke();
System.out.println("MyInterceptor-end");
return result;
}
}
package com.lzw.struts.Interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class FirstInterceptor extends MethodFilterInterceptor {
private static final long serialVersionUID = 1L;
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("FirstInterceptor-Start");
String result = invocation.invoke();
System.out.println("FirstInterceptor-End");
return result;
}
}
struts.xml
struts
/result.jsp
/error.jsp
/error.jsp
或者:
struts
/result.jsp
/error.jsp
/error.jsp
web.xml中加入:
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
actionPackages
com.lzw.struts.action
struts2
/*
package com.lzw.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
System.out.println("=====execute=====");
if ("hello".equals(this.getUsername().trim()) && "world".equals(this.getPassword().trim())) {
return "success";
} else {
this.addFieldError("username", "username or password error");
return "failer";
}
}
@Override
public void validate() {
System.out.println("=====validate=====");
if (null == this.getUsername() || "".equals(this.getUsername().trim())) {
this.addFieldError("username", "username required");
}
if (null == this.getPassword() || "".equals(this.getPassword().trim())) {
this.addFieldError("password", "password required");
}
}
public String lzwTest() {
System.out.println("======Test====");
return SUCCESS;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'login.jsp' starting page
init!
2013-10-31 13:51:15 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-apr-8080"]
2013-10-31 13:51:15 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-apr-8009"]
2013-10-31 13:51:15 org.apache.catalina.startup.Catalina start
信息: Server startup in 1699 ms
MyInterceptor-start
struts
FirstInterceptor-Start
=====validate=====
======Test====
FirstInterceptor-End
MyInterceptor-end
二: 过滤器:是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,package com.lzw.filter.demo;
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;
import javax.servlet.http.HttpSession;
public class UserAccessFilter implements Filter{
@Override
public void destroy() {
System.out.println("destroy!");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
HttpSession session = request.getSession();
if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){
response.sendRedirect("login.jsp");
return ;
}
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig config) throws ServletException {
//ApplicationFilterConfig[name=UserFilter, filterClass=com.lzw.filter.demo.UserAccessFilter]
System.out.println(config.toString());
}
}
UserFilter
com.lzw.filter.demo.UserAccessFilter
UserFilter
/jsp/*
package com.lzw.filter.demo;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class InitDataListener implements ServletContextListener {
private static ServletContext servletContext;
public static ServletContext getServletContext() {
return servletContext;
}
@Override
public void contextInitialized(ServletContextEvent contextEvent) {
servletContext = contextEvent.getServletContext();
//final ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
System.out.println("服务器启动完毕!");
System.out.println(servletContext);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {}
}
web.xml
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
com.lzw.filter.demo.InitDataListener
信息: Starting service Catalina
2013-10-31 15:13:55 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.42
服务器启动完毕!
org.apache.catalina.core.ApplicationContextFacade@7966340c
2013-10-31 15:13:56 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-apr-8080"]
2013-10-31 15:13:56 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-apr-8009"]
2013-10-31 15:13:56 org.apache.catalina.startup.Catalina start
信息: Server startup in 402 ms