Manually adding file to classpath - java

I'm trying to test a war that I deployed in Tomcat 7, however it seems its missing a xml configuration:
web.xml
<servlet>
<servlet-name>StorageEngine</servlet-name>
<servlet-class>com.jpeterson.littles3.StorageEngine</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:StorageEngine-servlet.xml</param-value>
</init-param>
</servlet>
I have downloaded the StorageEngine-servlet.xml separately, to be able to satisfy this init-param where do I need put the
xml file in the \webapps\littleS3-2.3.0\WEB-INF folder, just relative to the web.xml? Or?

No, it belongs in WEB-INF/classes of your WAR file. That is always in the CLASSPATH of a Tomcat app, loaded by the app class loader.

Given the mapping it would need to be placed in the root of your classpath. Try placing it within the root of any source folder

Related

How to expose custom jar in lib folder of java web application?

I have an existing jetty application that i need to modify. Basically i just need one access point that can handle POST requests. I have access to the WEB-INF directory of the project. There i have web.xml file and a lib folder. As i understand from jetty documentation jars under lib will be loaded automatically. Is it possible to add a custom jar under lib folder and the somehow configure web.xml so that my jar will handle HTTP requests from a certain URL? If so how would i start with that?
You can simply create a jar with your Servlet class inside (a class that extends HttpServlet) and add the servlet declaration inside the web.xml
<servlet>
<servlet-name>A_Name</servlet-name>
<servlet-class>your.new.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>A_Name</servlet-name>
<url-pattern>/the/path/you/want</url-pattern>
</servlet-mapping>

How to support variable interpolation in web.xml?

I'm packaging a web application and tomcat into a zip file. This zip should be able to use in any location or path.
This web application require to load an additional folder (response-folder) to be able to startup.
Due to the zip can be unzip anywhere, I need to find out my current path when the server is started and set the path in the startup.bat.
The following is my zip folder structure:
my.zip
- apache-tomcat
- bin
- startup.bat
- webapp
- mywebapp
- WEB-INF
- web.xml
- response-folder
In my startup.bat, I had the following line
set CATALINA_OPTS=-Dapache-tomcat-current-folder="%TOMCAT_CURRENT_FOLDER%"
I would like achieve something like below:
<servlet>
<description>MyService</description>
<display-name>MyService</display-name>
<servlet-name>MyService</servlet-name>
<servlet-class>com.my.package.MyService</servlet-class>
<init-param>
<description>Folder that contains response files</description>
<param-name>RESPONSE_FOLDER</param-name>
<param-value>${apache-tomcat-current-folder}\response-folder</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Note: I'm not able to the change the web application java code, as the web application is not belong to me.
May I know is there anyway to make web.xml to support variable interpolation or achieve behavior such as above?
Any help would be great.
To use environment variables in web.xml, you can add
set "CATALINA_OPTS=-Dapache-tomcat-current-folder=somevalue"
or if you want to reference a System environment variable
set "CATALINA_OPTS=-Dapache-tomcat-current-folder=%apache-tomcat-current-folder%"
in bin/setenv.bat (bin/setenv.sh for *nix).
You need to create this file.

REST API conflicting with Hadoop

This is the error I am getting.
com.sun.jersey.api.container.ContainerException: A root resource, class
org.apache.hadoop.hdfs.server.datanode.web.resources.DatanodeWebHdfsMethods,
has a non-unique URI template /
I am using Hadoop and REST API.
This is how I am initialising REST API in web.xml
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>
com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/RESTMOB/CALL/*</url-pattern>
</servlet-mapping>
I have tried removing the Hadoop JARs and it works, but I need those Hadoop JARs so what's the alternative. Thanks in advance
what is likely happening is some .class file(s) in the jar you removed are also present in the other jars - this is common in java and can create issue restarting your app and it might load different versions on restarts. I would open up the jars and search for duplicate class files and remove them from one jar and test. You can extract the jars to file system and search for all .class files and sort by name. Its always a good idea to eliminate duplicate .class files when possible even though it requires some tedious work

Possible to make path relative in WEB-INF/web.xml?

In my WEB-INF/web.xml file, an upload path is set absolutely.
Can I make this relative to the application root?
Or is there a better "best practices" approach to configuring paths for file uploads?
Below is the relevant config code.
entire config file
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/var/webapp/mdjdreview/upload/temp</param-value>
</init-param>
</filter>
If you make it a relative path, and use new File(path) to access the directory, it will be relative to the directory from which your container is started.
You can do it, but except for making it more difficult to know where the files are actually stored, and for risking to look at the wrong place because the container is started from elsewhere, I don't really see the point.
If waht you plan to do is storing the uploaded files in the directory of the deployed webapp, then you should definitely not do that, as a redeploy of the webapp will remove all the uploaded files. Treat the webapp directory as read-only.

Properties file as init-param in web.xml

I am migrating a Jsp-servlet based Java project that was hosted in websphere to tomcat. Following init-param is in web.xml inside a filter definition. I moved the properties file src folder which is classpath. How to change the following in the web.xml. Can I define properties file as init-param because most of the answers I saw has used context-param to define properties file. I dont think its an option to me as the existing application needs the properties file to be init-param.
<init-param>
<param-name>configPath</param-name>
<param-value>/pws/WebSphere/AppServer/properties/fyp/filterConfig/filter.properties</param-value>
</init-param>
I tried
<init-param>
<param-name>configPath</param-name>
<param-value>classpath:filter.properties</param-value>
</init-param>
It did not work.Thank you in advance,
Check your servlet implementation,you will find something like the following:
get the context root path from ServletContext;
append the property file path get from init-param;
do some file operation
As you asked,you can config the servlet as :
<init-param>
<param-name>configPath</param-name>
<param-value>filter.properties</param-value>
</init-param>
then change your code to
get the file name from init-param
open the stream this.getClass().getClassLoader().getResourceAsStream("fileName");
do some file operation

Categories