I want to access xml files from java class, lying under web/resources folder..
i.e
---Web pages
|
|--Web-INF
|
|-resources
| |-data.xml
|
|-other jsps
---Source Folder
|-databean
I want to access data.xml from a class under databean package. Is it possible? How?
Use ServletContext#getResource() or #getResourceAsStream() to obtain resources which are available in the webcontent.
InputStream input = getServletContext().getResourceAsStream("/WEB-INF/resources/data.xml");
You however normally do that in a Servlet, not a JSP.
Related
I inherited a project at work that includes one java web app nested within another. The higher level app has its own META-INF and WEB-INF directories, and the associated context.xml and web.xml within each.
Also at that higher level, is a directory that contains another set of INF directories and another set of context.xml and web.xml files within those. Basically, something like this:
App1
|--META-INF
|_context.xml
|--WEB-INF
|_web.xml
|_lib
|--Generic Directory
|--App2
|--META-INF
|_context.xml
|--WEB-INF
|_web.xml
|_lib
|--Another Generic Directory
Being relatively new to Java, I'm curious if this is considered bad practice. My initial thought is to restructure the project because I'm concerned users would be able to access the second set of xml files which are normally hidden from view.
What is the purpose of having the App2 directory here ? À webapp (WAR) cannot contain another webapp (WAR) so yes it is a bad practice since it has no interest.
I have a war file that I have deployed which contains an images folder in the WEB-APP directory. I tried accessing the images stored in the folder using
"localhost:8080/testapp/images/image1.jpg" but I am getting a 404 response. Can someone please help me with this?
Thanks in advance.
Your images directory needs to be a sibling of WEB-INF, not a child of it. Try this:
your-war-file.war
|-images/
| |-image1.jpg
| |-image2.jpg
| `-image3.jpg
|-WEB-INF/
| |-classes/
| |-lib/
| `-web.xml
|-index.html
`-404.html
Assuming you have your folder 'images' in the base directory for this web app (on the same level as 'META-INF' and 'WEB-INF' catalogs), the path seems correct. Maybe check that you haven't put 'images' folder inside 'WEB-INF' where it is not accessible? If that doesn't work, try accessing some other file inside your web app to check that it was deployed on server.
Clients may not directly access artifacts under WEB-INF.
Put them in a location directly accessible if you're not streaming them from an app endpoint.
how to read from path ?
use ("."+File.seperator+"some else") path of File for current path into your code or html file
Before migrating to maven, we used Ant and there were no problems with launching Dev Mode from under IntelliJ IDEA. But now the project is under Maven. So I made, I think, all needed configuration, and when I run debugger I see login page, but after logging in I receive the following error:
[ERROR] Resource img/remove_filter_btn.png not found. Is the name specified as Class.getResource() would expect?
Here is how I use resource:
public interface Resources extends ClientBundle {
#Source("img/remove_filter_btn.png")
ImageResource removeBtn();
}
The project structure is the following:
Module
|--Submodule
| |--src
| | |--main
| | | |--java
| | | |--package
| | | |--class
| | |--webapp
| | |--img
| | |--remove_filter_btn.png
| |--target
| |--ROOT
| |--img
| |--remove_filter_btn.png
|--utils
|--tomcat
|--webapps
|--ROOT
|--img
|--remove_filter_btn.png
War file is launched in tomcat that is located in utils folder.
In fact, I don't know where GWT looks for this resource. Maybe there's a solution on how to get the point where GWT starts looking resources for?
At the moment this is the only question because i think that it is the only left issue before debug would start working.
The images consumed by ClientBundle into ImageResources are expected to be in the source tree, not in the external web resources (i.e. in target/ or webapp/).
When you write
package my.project.something.client
//...
public interface Resources extends ClientBundle {
#Source("img/remove_filter_btn.png")
ImageResource removeBtn();
}
you are telling ClientBundle that it can find the image at src/my/project/something/client/img/remove_filter_btn.png.
Keep in mind that the purpose of ClientBundle isn't just to wrap up pathname strings so they can be easily used in code, but to actually compile the image into your app. In the case of small images, it will often actually put the entire image into the app so there is no need for any spriting at all. Even if this isn't possible, it will still rewrite the images so they can be cached forever by the browser, and try to optimize them to not use any more space than actually needed.
This question already has answers here:
Where to place and how to read configuration resource files in servlet based application?
(6 answers)
Closed 3 years ago.
I'm developing a JSF web application, with the current 2.1.7 JSF version.
Actually, I have a login page, associated with a session bean,
which calls to a logic class "PersonCatalog",
which is responsible for reading a text file .txt with the information of the persons, like a database but with files.
The problem is to access the file,
ever I get a NullException because the program doesn't finds the file.
I'm not sure about the path I choose, here is the directory herarchy:
Application
|-- build
|-- src
|--eci
|--pdsw2012
|--shoeople
|--beans
|--loginBean and others ...
|--logic
|--PersonCatalog and others...
|--persistence
|--files
|--persons.txt
|-- WebContent
|--WEB-INF
|--META-INF
|--a lot of xhtml pages ...
I'm using in PersonCatalog for read the person.txt file the follow code:
File readFile = new File(new String("src/eci/pdsw2012/shoeople/persistence/files/persons.txt"));
But I get:
java.lang.NullPointerException
at eci.pdsw2012.shoeople.logic.PersonCatalog.loadFile(PersonCatalog.java:31)
I have used the ExternalContext().getRealPath(), but it only shows inside Web-content folder.
I'm using Eclipse, and the default route is the installation folder of Eclipse, the is so strange:
String ruta = System.getProperty("user.dir");
System.out.println("DirAbs: " + ruta.getAbsolutePath());
and prints
C:/Eclipse/
The root of a deployed web application in this case would be the WebContent folder, which contains WEB-INF.
In the deployed version the src files are compiled into class files and placed in the following folder classes.
The path to persons.txt will be \WEB-INF\classes\eci\pdsw2012\shoeople\persistence\files\persons.txt
I am trying to load a resource that is contained within an embedded JAR file. The project is actually deployed in JBoss using an EAR file with the following structure:
deploy.ear
|
|-> project.sar
|
|-> sub_project.jar
| |
| |-> settings.xml
|
|-> com/path/project/
|
|-> main.class
From main.java I'd like to get a InputStream for settings.xml. What is the correct way to do this?
My current understanding that the following code should work, but it is returning null:
this.getClass().getResourceAsStream("settings.xml");
Update
After some trial and error, the following statements work:
getClass().getResourceAsStream("/settings.xml");
getClass().getResourceAsStream("/sub_project.jar/settings.xml");
getClass().getClassLoader().getResourceAsStream("/settings.xml");
getClass().getClassLoader().getResourceAsStream("settings.xml");
getClass().getClassLoader().getResourceAsStream("sub_project.jar/settings.xml");
getClass().getClassLoader().getResourceAsStream("/sub_project.jar/settings.xml");
This might be a good resource: http://one-jar.sourceforge.net/version-0.95/
The main idea is that the inner JAR is not loaded by the ClassLoader that loaded the outer JAR automatically, you need to do so manually, e.g. by using a StreamClassLoader to load the inner jar
Only then, from your own ClassLoader you can get that resource using getResourceAsStream(...)