Read the file in the WAR package and the OutOfMemoryError in Ant use

xiaoxiao2021-03-06  39

First, the reading of the file in the WAR package

When developing J2EE web applications, the deployment of directory is usually used in the development phase, and the web application is usually packaged for a single .war file for easy deployment when official runtime. That is, under your application directory (such as WebLogic DefaultWebApp), execute the following command:

Jar cf0 mywebapp.war **

This way, it is very convenient to deploy to a formal system, just copy this .war file to WebLogic Applications Directory or Tomcat's WebApps directory automatically deployment. Tomcat will automatically monitor the deployed .war application package, unpack, so there is no problem mentioned below. WebLogic does not automatically unpack. War, so if you need to read the configuration file or other resource files in your application, you will find that the procedure running normally when unpacking deployment, When the package deployment in WebLogic, the run is wrong, and the file will not be found. For example, the following applications: [pre] | --defaultwebapp | --index.jsp | --... JSP | --WEB-INF | - Web.xml | - log4j.properties | - Classes. ..... [/ pre] where log4j is used as a log output tool, log4j's profile log4j.propertes is placed in the defaultwebapp / web-inferscript. Log4j is initialized through an automatically loaded servlet, and the initialization code is as follows:

ServletContext context = getServletContext (); org.apache.log4j.propertyConfigurator.configure ("/") "/Web-inf/log4j.properties");

Among them, context.getRealPath ("/") gets the true root directory of the current web application, for example, if your WebLogic is installed under D: / BEA, under Windows CONTEXT.GETREALPATH ("/") is usually returned: D: /bea/wlserver6.1/config/mydomain/applications/defaultwebapp At UNIX Similar to: /bea/wlserver6.1/config/mydomain/applications/defaultwebapp this, and "/ Web-Inf /Log4j.properties) After stitching, Get the true path of the log4j.properties file, and log4j reads this profile via file IO to complete the initialization. Now everything is normal! After the test passes, all the files under DefaultWebApp be called a .war package, when deploy, discovery the system report not to find the "D: /Bea/wlServer6.1/null/web-inf /Log4j.properties" file! If you need to read other files that have been packaged into the WAR package, you can't find a file. Also, the system does not look for the D: /bea/wlserver6.1/config/mydomain/applications/defaultwebapp directory, and will find D: /bea/wlserver6.1/null. This is because context.getRealPath ("/") returns NULL. View ServletContext API documentation, public String getRealPath (String path) ...... The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if The Servlet Container Cannot Translate The Virtual Path To A Real path for any reference is beking master, it is the concept of a package, is the concept of a RealPath, call GetRealPath only simply returns NULL. In fact, it is also very understandable that a file is packaged in. War file, there is no directory structure (although there is still a directory structure in the package, this is not equivalent to the directory structure in the file system). Therefore, resources in the WAR package are unable to get RealPath. This will not be read from the file IO. So how do you read resources in the WAR package? The answer is to use the servletContext.getResourceAsStream (String) method. For org.apache.log4j.propertyConfigurator, there are several configuration methods: static void configure; static void configure (STATIC VOIGURUR); STATIC VOIGURUR (URL Configurl);

Since it, you cannot get the log4j profile in the WAR package, so you can construct a Properties by reading InputStream, constructing a Properties, which can also be completed by configure (Properties Properties). The following sample code: InputStream is = getServletContext () getResourceAsStream ( "/ WEB-INF / log4j.properties"); Properties props = new Properties (); try {props.load (is);} catch (IOException e) {System. .rr.println ("Load Log4j Configuration Faled");} PropertyConfigurator.configure (PrOPS);

Then, now you can run successfully for WAR applications, but if you don't pass the WAR deployment, you will not find the resource error without the application of the directory structure. Come take a look at ServletContext.getResourceAsStream API documentation, Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root. This method allows the servlet ContaR to Make A Resource Available To Servlets from any Source. Resources CAN Be Located on a local or remove file system, in a catabase, or in a .war file. Visible, with the GetResourceAsStream, you can get the local file system, the remote file system, WAR package and other resources. There is no problem that is worried. Conclusion: When developing J2EE web applications, if you need to read files in this application, try to use ServletContext.getResourceAasstream without using file IO.

Second, OutofMemoryError in Ant is resolved

转载请注明原文地址:https://www.9cbs.com/read-75306.html

New Post(0)