The
Properties class inherits from the Hashtable class and implements the map interface, and also holds the property set in the form of a key-value pair. But properties have a special place, that is, its keys and values are string types. The
code is as follows:
Package cn.zhangheng.filter;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;
Import java.util.Properties;
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 Org.apache.log4j.Logger; /** * My filter * * @author Zhangheng * @date August 16, 2016 Morning 10:14:40 * * */public class Myfilter implements filter {p
ublic static Logger LOG = Logger.getlogger (Myfilter.class);
public static Properties P = new properties (); The public void Destroy () {///only executes log.info when the Web server is destroyed ("End filter ...").
");
SYSTEM.OUT.PRINTLN ("-----Only one---"); public void Dofilter (ServletRequest request, servletresponse response, Filterchain Filterchain) throws
IOException, Servletexception { Log.info ("Perform filtering ...")
");
String Servletcontextpath = Request.getservletcontext (). Getcontextpath ();
System.out.println (Servletcontextpath);
HttpServletRequest Request1 = (httpservletrequest) request;
String URL = Request1.getrequesturi ();
if (!url.contains ("login.do")) {Filterchain.dofilter (request, response); Web.xml (Non-javadoc) * * @see Javax.servlet.filter#init can be obtained when initialization is performed (Javax.servlet.Fi LTERCONFIG) */public void init (Filterconfig filterconfig) throws Servletexception {Log.info ("Initialize filter ...
");
String filtername = Filterconfig.getfiltername ();
String param1 = Filterconfig.getinitparameter ("Firstparamname");
String param2 = Filterconfig.getinitparameter ("Secondparamname");
System.out.println ("filtername =" + FilterName + ";p aram1 =" + param1 + ";p aram2 =" + param2); enumeration<string> s = filterconfig.getinitparameternames();
while (S.hasmoreelements ()) {string string = (String) s.nextelement ();
System.out.println (string);
}//Get XML InputStream in = MyFilter.class.getResourceAsStream ("/para.properties"); try {p.load (in);//This method can be loaded from the file input stream corresponding to the. Properties property file, loading the property list to the Properties class object Log.info (P.getproperty ("NA
Me ") + P.getproperty (" age ");//The two methods are to get and set property information respectively.
Append FileOutputStream out = new FileOutputStream ("/b.properties", true);
P.setproperty ("Phone1", "1232451"); P.store (out, append information);//This method saves the property list of the Properties class object to the output stream. If the comments is not empty, the first row of the Saved property file is #comments, which indicates the annotation information, and if it is empty there is no annotation information.
The comment information is followed by the current save time information for the property file.
catch (IOException e) {e.printstacktrace ();
finally {P.clone ();
}
}
}