Define a servlet filter when using struts2

Source: Internet
Author: User

I usually use struts2 to match all types. In this case, a problem occurs. when accessing a servlet, the system will access it as an action, the action cannot be found. Therefore, you need to implement a filter to filter the servlet, and the filter should be placed before the filter of struts2. You need to directly redirect it to access the servlet before using it as an action.
 
 
Code:
Java code
Import java. io. IOException;
Import java. util. ArrayList;
Import java. util. Arrays;
Import java. util. List;

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;


Public class ServletFilter implements Filter {

Private List <String> targets = new ArrayList <String> ();

Public void destroy (){
// TODO Auto-generated method stub

}

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String uri = req. getRequestURI ();
String target = uri. indexOf ("? ")> 0? Uri. substring (uri. lastIndexOf ("/") + 1), uri. indexOf ("? "): Uri. substring (uri. lastIndexOf ("/") + 1 );
// The servlet URL uses the servlet/servlet name.
If (uri. contains ("servlet") & targets. contains (target )){
Req. getRequestDispatcher (uri. substring (9). forward (req, resp );
} Else {
Chain. doFilter (req, resp );
}
}

Public void init (FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
String targets = config. getInitParameter ("targets"); // in the web. the parameters in the filter declared in xml specify which servlets need to be filtered. You can also directly filter all servlets without specifying them.
This.tar gets. addAll (Arrays. asList (targets. split (",")));
}

}

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.