Skip to content

Commit

Permalink
filter + interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
冯周 committed Jul 23, 2016
1 parent 921eac8 commit 9a6fee4
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
15 changes: 15 additions & 0 deletions spring-interceptor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,19 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.vonzhou.learning.filter;

import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* Created by vonzhou on 16/7/23.
*/
public class SimpleServletFilter implements Filter{
private static Logger logger = Logger.getLogger(SimpleServletFilter.class);

public void init(FilterConfig filterConfig) throws ServletException {
logger.info("SimpleServletFilter init....");
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String id = request.getParameter("id");
if(id != null && !id.isEmpty() && !id.equals("123")){
chain.doFilter(request,response);
}

// Reply directly here
HttpServletResponse httpResponse = (HttpServletResponse)response;
httpResponse.getWriter().println("+++ From Filter, Another Page...enjoy");
}

public void destroy() {
logger.info("SimpleServletFilter destroy....");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
long startTime = System.currentTimeMillis();
request.setAttribute("startTime", startTime);
logger.info("Will call " + handler.toString());


// response.getWriter().println(" --- From Interceptor, you are rejected.");

return true;
}

Expand Down
1 change: 1 addition & 0 deletions spring-interceptor/src/main/resources/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello TXT
2 changes: 1 addition & 1 deletion spring-interceptor/src/main/resources/service-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:mapping path="/user/**"/>
<mvc:exclude-mapping path="/admin/**"/>
<bean class="com.vonzhou.learning.interceptor.LogInterceptor"/>
</mvc:interceptor>
Expand Down
9 changes: 9 additions & 0 deletions spring-interceptor/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@
classpath:service-context.xml
</param-value>
</context-param>

<filter>
<filter-name>simpleFilter</filter-name>
<filter-class>com.vonzhou.learning.filter.SimpleServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>simpleFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

0 comments on commit 9a6fee4

Please sign in to comment.