i am developing an application in java and i am new to this java ee platform.I have a dataset file called "51Degrees-EnterpriseV3_2.dat" in my resource folder. how do i specify the path of this in my web.xml file and how do i retrieve the file in code?
In web.xml , you can add
<context-param>
<param-name>you can mention here localtion variable name</param-name>
<param-value>location here </param-value>
</context-param>
The other answer shows the correct way to add the parameter to your web.xml. Which for the 51Degrees V3.2 data file would look like this:
<context-param>
<description>The name of the device database.</description>
<param-name>BINARY_FILE_PATH</param-name>
<param-value>51Degrees-EnterpriseV3_2.dat</param-value>
</context-param>
As far as I know, there is not a way to get a resource which is located in the resources path (it's an XML file so the .getResource(name) method cannot be used). However, as you have a web.xml file, I assume this is a web project (i.e. .war rather than .jar)? If this is the case, then the WEB-INF directory is what you need. Files in here are also packaged up like resources, but can be more easily consumed by a .war package.
If you put your data file in src/main/webapp/WEB-INF/, which is used as the root by the web project. So the above XML example will work if the path to your data file is:
src/main/webapp/WEB-INF/51Degrees-EnterpriseV3_2.dat
As a sidenote, there is more documentation on configuring 51Degrees in a Java web app here
Related
I'm working in a java webapp that uses Picketlink package. This package needs a config file ( picketlink.xml ) that is located in WEB-INF directory by default. This app will be hosted in 3 different linux jboss servers/environments (development, testing and production).
This configuration file is different for each env.
It's a project requirement that I have only one single build (single artifact) for all envs, so env-specific stuff must be in the server (can't use profiles). So, I need to store this file in the server itself and reference it from inside the app.
The documentation recommends that I use a context-param inside my web.xml to point to this external file:
<context-param>
<param-name>CONFIG_FILE</param-name>
<param-value>/path/to/picketlink.xml</param-value>
</context-param>
I just can't make it work as the app does not find the file.
What would be the right way to write this path? I wanted something like this:
<context-param>
<param-name>CONFIG_FILE</param-name>
<param-value>home/user/picketlink_config/picketlink.xml</param-value>
</context-param>
I am developing a java web-app (v3.0) for a TomCat 7.0 server and I am having troubles loading the log4j2.xml file.
I have defined the log4j2.xml file outside my project and defined the path for the file in my web.xml file.
If i hardcode the path my log4j2.xml file loads as it should.
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///C:/my/path/log4j2.xml</param-value>
</context-param>
On the other hand I want to use an enviroment variable to define the path.
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///${ENVIROMENT_VARIABLE}/log4j2.xml</param-value>
</context-param>
When I start TomCat I have this error:
ERROR SatusLogger Unable to access file:///$%7BENVIROMENT_VARIABLE%7D/log4j2.xml
It looks like it isn't 'translating' the variable.
Any help will be very aprecciated.
PD: Sorry for my english.
Log4j interpolates the value it finds for log4jConfiguration in web.xml. However, you have to use standard Log4j Lookup syntax. To get an environment variable you would specify:
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///${env:ENVIROMENT_VARIABLE}/log4j2.xml</param-value>
</context-param>
I had configured virtual directories in glassfish3.x over which I could write filters.
For an example I could access files at c:/web from http://localhost/TestApp/web over which I could also place a filter at my web app's web/xml file with
<filter-mapping>
<filter-name>dir_filter</filter-name>
<url-pattern>/web/*</url-pattern>
</filter-mapping>
Unfortunately Tomcat 8.0 is not allowing me to write a filter above that. It simply ignores the filters and shows the content in the web directory.
The problem is anybody can access all of the files in the "web" folder.
Any how can we place filter over the virtual directories.
FYI - i have made the web app named "TestApp" and the virtual config is located at "$tomcat_dir/conf/Catalina/localhost" directory with the file name "TestApp#web.xml" file and having the content
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="C:/web" debug="0" privileged="true"></Context>
Regards
The context XML file TestApp#web.xml is mapping requests to /TestApp/web/... to the webapp. In the webapp, paths are relative to that, so /TestApp/web/x.txt is path /x.txt to the webapp, and will serve file C:/web/x.txt.
Change the filter to /*, so all requests are filtered, incl. the request for /x.txt.
however tomcat is throwing the error :
IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml];
Which suggests that tomcat expects the applicationContext.xml to be one directory above where it is copied to. It is being copied to the /WEB-INF/classes directory, not just the plain old /WEB-INF
I am using maven.
If you have put it under src/main/resources, you should refer to the resource as classpath:/applicationContext.xml
In your web.xml you can state where your applicationContext.xml is located. If it is in /WEB-INF/classes then you must state /WEB-INF/classes/applicationContext.xml (and not just /WEB-INF/applicationContext.xml).
Try this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
To include more than one context files you can use the import function. To do so, write into your applicationContext.xml one line per file like this:
<import resource="myOtherSpringContext.xml"/>
Use the following syntax in your web.xml ( http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/context/ContextLoader.html ):
<!-- list of the configuration files to load -->
<context-param>
<!-- Spring -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
It will load it from the classes/* directory.
BTW, if you want to copy the applicationContext.xml directly in your WEB-INF directory, move it in /src/main/webapp
BTW, here is the way recommanded by the spring documentation: http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#context-create
HIH
This is standard Maven behaviour, and is what a lot of people use with no objection. Why do you prefer it in WEB-INF?
Anyway, if you want it there, you can just put it in src/main/webapp/WEB-INF.
the standard way is to create new source dir src/main/webapp. All files from this dir will be places in the same folder as WEB-INF in resulting war. So, inside this new source dir you can create WEB-INF and place you applicationcontext.xml in it.
so you structure would look much like
+src
+---main
+---+---java
+---+---webapp
+---+------WEB-INF
check maven-war-plugin documentation
At the moment, the default I think, it copies to
target/myproject/WEB-INF/classes
so when deploying it does not pick up the context.
Also, i want to reference a server specific config file database.properties, I want to put it in tomcat/conf and then reference it in applicationContext.xml, how can I do this ?
Also(2), I am under the impression that this is a fairly standard and decent way to set things up - please correct me if I am wrong.
edit
for the server specific config file I user this
<context:property-placeholder
location="file:${catalina.home}/conf/database.properties"
ignore-unresolvable="true"
/>
If you need to keep applicationContext.xml as a classpath resource, you can configure ContextLoaderListener to pick it from the classpath by adding the following lines to web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
It's much easier than configuring Maven to copy it to WEB-INF.
Regarding the second question, you can configure PropertyPlaceholderConfigurer or <context:property-placeholder> to load .properties file from a file system.
For your title question: Often in a .war maven module, you'll put web related resources under src/main/webapp instead of src/main/resources. Then the maven plugin will pick them up automatically because it matches convention. So, move your applicationContext.xml to src/main/webapp/WEB-INF
Another option is to configure the webResources as described in the documentation
For the second question you can look at a PropertyPlaceHolderConfigurer. You'll just have to get the path correct.