I have a little Java Web Application project using IntelliJ, Tomcat and jersey.
My project structure looks like this:
src
main
java
package
Main.java
resources
test
web
WEB-INF
web.xml
index.jsp
I want to add a resource xml file. It should be deployed with some initial values and it should be possible to read from the file and also to write to it - all from java code. It should not be accessible directly from, let's say, a browser.
Where should I put that file and how can I access it when I've put it there?
Related
I created one java micro service using spring boot. The application requires some data from static xml files (kept together in a folder, lets name it X) in src/main/resources folder.(I am using STS IDE)
It is working fine when I run it in the IDE itself.
Now when I pack it in a jar and try running it using CLI (java -jar jarFileName.jar) it works and access the folder X from the target/classes folder.
Now my problem is, when I try to deploy this small app on Pivotal Cloud Foundry using either CLI or STS plugin, I have to give a path to the jar file so it uploads only the respective jar file and starts the container with the application. But the application cannot access folder X which contains its resources.
Though I confirmed that the jar file contains the folder X in BOOT-INF/classes but it tries to find the folder X on its own path - not inside itself.
Does anyone have some idea how can a jar file deployed on PCF can access its resources kept inside itself.
I tried using
InputStream is = this.getClass().getClassLoader().getResourceAsStream("X/abc.xml")
but it didn't work. It is unable to find the path during runtime.
why do not try Apache Abdera.
your XML is obtained from an API.
just another way, btw.
In your main class(usually Application.java) add #ImportResource annotation at class level.
Example: #ImportResource("classpath:myfile.xml")
At first, I placed the java class file together with the wsdl files inside the WEB-INF folder, they successfully connected. But when I moved the java class file and the wsdl files inside the scr folder because I need to call the java class inside the jsp file, something went wrong. The java class now is full of errors. Seems like they didn't connected. I already configured the wsdl file in web client service. I'm using eclipse as development tools.
I created a simple Maven web app project, with 1 jsp file index.jsp. I am able to access this file from server as follows:
http://localhost:8080/myProject/
How does it is able to access my index.jsp ? I haven't specified anything on web.xml. Where does all these configuration was exist ?
2.) Can I change the URL to like http://localhost:8080/myProject/webapp/index.jsp ?
If you're using the Maven Standard Directory Layout, then everything located in the Web application sources directory (src/main/webapp) will be in the root of the application and will be public.
If you want to create /myProject/webapp/index.jsp, then create a webapp folder inside the src/main/webapp folder, so you will have folder structure like src/main/webapp/webapp (Which I don't see any reason to use it that way). Then create the index.jsp inside it.
I am coding a website using java servlets and am using eclipse and tomcat. When I test it using localhost, it works fine. But when I am deploying it on my actual website, the directory structure is messed up and the files are not called properly.
My eclipse directory structure on localhost is
Project Name
.src/packageName/java files
.WebContent/HTML files.
When I make a call from the html files, I use the relative location and tomcat automatically knows to look in the src/packageName folder. For example, from the /WebContent/login.html page makes a onClick call as follows,
. This will automatically trigger the java file in /src/packageName/welcome
When I am deploying it in my actual website, the WebContent/login.html is throwing an error WebContent/welcome file is not found. How do I tell my website to search in /src/packageName folder?
Hmm...have you been sure to package the application as a war for deployment.
Problem: i want to do filter for all webapps.
I created a filter for monitoring requests to the apache tomcat server.
for the sake of example, it's called MyFilter. i created it in netbeans, which created 2 separated directories:
webpages - contains:
META-INF
WEB-INF
index.jsp (a file which i created for viewing monitored data)
source packages
my.package/MyFilter
in the same when i execute it from netbeans, it deploys it and sends me to http://localhost:8080/MyFilter
whenever i view a page under /MyFilter/somepath - it runs the filter.
i want the filter to run on all paths, and not only for the webapp
Where do i need to locate the project files? which project files do i need? should i edit conf/web.xml and add the filter? or should i only be in the specific webapp's web.xml?
EDIT:
i copied the generated .war file into tomcat/webapps
when i start tomcat, it creates a directory (tomcat/webapps/MyFilter/)
i added the filter into tomcat/conf/web.xml. when i start it, it fails to load ://localhost:8080/ but successfully loads (and filters) ://localhost:8080/MyFilter
A filter will by definition only ever run within the web application it is defined in.
You could define it in the web.xml in the conf/ directory of Tomcat to have it included in all web applications deployed to that Tomcat by default (note that you'd also need to make the filter class available to all web applications, probably by putting it in the lib/ directory of Tomcat).
Alternatively, you can implement a Tomcat Valve, which is conceptually similar to a filter, but can be installed on an Engine, Host or Context in Tomcat.