Struts filter filters certain characters, struts filter filters

Source: Internet
Author: User

Struts filter filters certain characters, struts filter filters


(1) Some specific characters may need to be filtered out when tianchao's rules are used for project purposes. The following is a simple case of character filtering;


(2) The filter code is as follows:

Package com. lc. 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. httpServlet; public class SafeFilter extends HttpServlet implements Filter {private String keywords []; public void doFilte R (ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {String name = arg0.getParameter ("username"); // obtain the username usernameif (name! = Null) {// first judge whether the user name is null for (String key: keywords) {if (key. equals (name) {arg0.setAttribute ("err", "blocked by the first filter security filter! "); Arg0.getRequestDispatcher ("/WEB-INF/tishi. jsp "). forward (arg0, arg1); return ;}} arg2.doFilter (arg0, arg1);} public void init (FilterConfig arg0) throws ServletException {// read the string to be filtered from the configuration file and remove ";" to obtain the array keywordskeywords = arg0.getInitParameter ("keywords "). split (";");}}


(3) configuration in the web. xml file:

<! -- Manages secure filters and must be configured at the frontend when the first filter is configured --> <filter-name> SafeFilter </filter-name> <filter-class> com. lc. filter. safeFilter </filter-class> <! -- Below are the characters to be filtered --> <init-param> <param-name> keywords </param-name> <param-value> bin Laden; Mao Dou; Feng Jie; insects </param-value> </init-param> </filter> <filter-mapping> <filter-name> SafeFilter </filter-name> <url-pattern>/ * </url-pattern> </filter-mapping>


OK!




What is the difference between the interceptor of the Java framework Struts2 and the filter?

Filter: in java web, the request and response you pass in filter out some information in advance, or set some parameters in advance, and then input the servlet or struts action for business logic, for example, filter out invalid URLs (not login. do address requests are filtered out if the user does not log in), or the character set is set before the servlet or struts action is passed in, or some invalid characters are removed.
The Interceptor is programmed in the aspect-oriented way by calling a method before your service or method, or calling a method after the method. For example, a dynamic proxy is a simple implementation of the interceptor, you can print a string (or perform other operations on the business logic) before calling the method. You can also print a string after calling the method, even when you throw an exception, you can perform business logic operations.

Differences between interceptor and filter:

The Interceptor is based on the java reflection mechanism, and the filter is based on function callback. The interceptor does not depend on the servlet container, and the filter depends on the servlet container. The interceptor can only work on action requests, while the filter can work on almost all requests. The interceptor can access objects in the action context and value stack, but the filter cannot. In the lifecycle of an action, the interceptor can be called multiple times, and the filter can only be called once during container initialization.
Execution sequence: Before filtering-before intercepting-Action processing-after intercepting-after filtering. I personally think that filtering is a horizontal process. First, the content submitted by the client is filtered out (for example, the user who is not logged on cannot access the internal page). After filtering, the interceptor checks the data submitted by the user, performs some preliminary data processing, and then sends the processed data to the corresponding Action. After the Action is processed and returned, the interceptor can also perform other processes (not expected to do anything), and then return to the filter for subsequent operations.


What are the differences between jsp/servlet filters and struts2 interceptors?

Differences between interceptor and filter:

1. the interceptor is based on the java reflection mechanism, and the filter is based on the function callback.

2. The filter depends on the servlet container, while the interceptor does not depend on the servlet container.

3. the interceptor can only work on action requests, while the filter can work on almost all requests.

4. the interceptor can access objects in the action context and value stack, but the filter cannot

5. In the lifecycle of an action, the interceptor can be called multiple times, and the filter can only be called once during container initialization.

Interceptor
: In Aspect-Oriented Programming, you call a method before your service or method, or call a method after the method. For example, a dynamic proxy is a simple implementation of the interceptor, you can print a string (or perform other operations on the business logic) before calling the method. You can also print a string after calling the method, even when you throw an exception, you can perform business logic operations.

The following example shows the differences between filters and interceptors:

Use the Interceptor to filter jsp pages in the/admin directory

[Html] view plaincopy

<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>

The following is my implemented Interceptor class:

[Java] view plaincopy

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... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.