Java Create filter parsing XML file

Source: Internet
Author: User

Today I wrote a filter demo, now parsing the Actions.xml file, getting the business rules in action; no need to import any jar packages

Actionfilter Filter Class:

 Packageaccp.com.xh.utils;Importjava.io.IOException;ImportJava.io.InputStream;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.xml.parsers.DocumentBuilderFactory;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element;Importorg.w3c.dom.NodeList;/*** Create filter * The front controller receives all parameters, formats the encoding, controls the login to intercept; distribute requests *@authorXiaohua **/ Public classActionfilterImplementsfilter{/*** Tomcat executes at startup and executes only once; * Parsing the configured rules file: Actions.xml*/@Override Public voidInit (filterconfig config)throwsservletexception {Try {            //Get Action.xml fileInputStream is =Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Actions.xml"); //Parsing XML FilesDocument document =documentbuilderfactory.newinstance (). Newdocumentbuilder (). Parse (IS); //Get action tag element: <action name= "pro_.*" class= "xxxxxaction" >NodeList NodeList = document.getelementsbytagname ("Action");  for(inti = 0; I<nodelist.getlength (); i++){                //Parse action tag elementElement actionelement =(Element) Nodelist.item (i); String ActionName= Actionelement.getattribute ("name"); String Actionclass= Actionelement.getattribute ("Class"); System.out.println ("ActionName Property:" +actionname); System.out.println ("Actionclass Property:" +Actionclass); //gets the result tag element//<result name= "Reload" type= "redirect" >product</result>NodeList resultlist = actionelement.getelementsbytagname ("Result");  for(intJ=0;j<resultlist.getlength (); j + +) {Element Resultel=(Element) Resultlist.item (j); String Resultname= Resultel.getattribute ("name");//Get the result Name property in Action.xmlString Resulttype = Resultel.getattribute ("type"); System.out.println ("Name attribute:" +resultname); System.out.println ("Type attribute:" +Resulttype); }                            }        } Catch(Exception e) {Throw NewRuntimeException ("Error parsing action.xml file:" +e.getmessage ()); }    }        /*** Each request will be executed by the filter, * According to the requested URL to find the corresponding processing xxxaction, determine which method processing*/@Override Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {httpservletrequest req=(httpservletrequest) request; HttpServletResponse resp=(httpservletresponse) response;//resp.sendredirect ("");    }    /*** Normal stop Tomcat execution, will only be executed once; * Destruction of resource operations,*/@Override Public voiddestroy () {}}

Actions.xml file

<?XML version= "1.0" encoding= "UTF-8"?><!--Defining Business Rules -<Actions>    <Actionname= "pro_.*"class= "Xxxxxaction">        <resultname= "List">/web-inf/view/list.jsp</result>        <resultname= "Edit"type= "Redirect">/web-inf/view/edit.jsp</result>        <resultname= "Reload"type= "Redirect">Product</result>    </Action></Actions>

The values of the elements that are output when the server is started:

Of course, don't forget. Configuration mappings in Web. XML are specified in the Actionfilter class, so output is only available when the service is started.

Java Create filter parsing XML file

Related Article

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.