Load Properties or XML in glassfish web service - java

I've got a basic glassfish web service and I'm trying to load some properties from WEB-INF folder.
However,
Thread.currentThread().getContextClassLoader().getResource("test.properties")
this.getClass().getResource("test.properties");
don't seem to be searching in the right directories. Replacing test.properties with ".", i get the directory
file:/C:/Program%20Files%20(x86)/glassfish-3.1.2/glassfish/domains/domain1/lib/classes/
which is completely empty.
How do i load property files from the java layer in a web service like this?

Put it in WEB-INF/classes and ask for "/test.properties"

Related

Trying to access xml file in tomcat via URL, getting 404 error

I have a webapp deployed in Tomee+ 1.6.0 (tomcat 7.0). The app runs fine and I can access all pages. Within one of my app's folders (myapp/intructions/) I have placed a xml file that I will access via a client application.
The application will access the file for its configuration, and will be called via a batch file with the following command:
"C:\Java\jre7\bin\java" -cp MyApp.jar com.me.MyApplicationStart "argument1" "argument2"
Within MyApplicationStart the constructor tries to access the configuration from the URL
http://webserver:8080/myapp/instruction/myconfig.xml
As I have no control over the above methodology, this cannot be changed.
I would expect the xml to be downloaded (or fetched) and my application to work fine, but instead I get a 404 error. If I change the extension of my config file to .properties, everything works fine, but someone else other than me in the team insists that we use xml for the config file, since it will allow more automation features.
What gets me is why can't I access this xml (or any other) file from tomcat. It seems that in previous versions of tomcat this was not a problem, or at least not with files outside the WEB-INF folder. Now it is and it seems to be exclusive to xml files, which suggests that there's a configuration in place that prevents access to this extension from anywhere in the webapp.
Would anybody know what this configuration is, or what is the mechanics behind this error?
Thanks in advance.
for security reason, WEB-INF folder is not accessible from client side.
put your xml file in other location.
and try again, It will work

Viewing JSPs with Weblogic JSPServlet

We are currently migrating from OC4J to Weblogic. We have an ANT script which builds the EAR file. The ANT also compiles and packs all the .jsp files into a jar. But when I tried to run the application on Weblogic it always returned 404 when trying to access .jsps. So I tried to add a mapping in web.xml for weblogic.servlet.JSPServlet for the URL pattern *.jsp, but I still get 404 when I try to access .jsps.
What could be the problem?
Thanks.
Your EAR file structure needs to be correct.
If you are deplying to WLS, you can deploy either an EAR or WAR seperately, however if you choose EAR file, you must have the the JSP's within the WAR file. You should only need to specify servlet and resource mapping within the web and context xml files.
You must also make sure that you have your .jar files in the library. Otherwise you can run into problems.
Let me know if you need more info.

Where to put jsp project folder in tomcat for hosting?

I am right now working with JSP and using tomcat Apache for that.what my problem is when i am creating one single JSP page and putting in root directory of tomcat then its working fine.but now i have one project that contains some useful jars and other java classes so how to put that whole project directory in tomcat.
I had put that in web app directory but its giving me error as follows
The requested resource (/dailymotion-cloudkey-java-73f6f35/examples/upload.jsp) is not available.
I am Giving snapshot of my web app folder where i had put this folder name as dailymotion-cloudkey-java-73f6f35
i know that i am doing mistake while putting dailymotion-cloudkey-java-73f6f35 directory in tomcat.
but i am totally new in this so i couldn't find out so can anyone tell me
tree Structure of my project
I think dailymotion-cloudkey-java-73f6f35, does not have WEB-INF folder with web.xml file. Due to which it is not able to locate the resources.
or
You can copy your dailymotion-cloudkey-java-73f6f35 project directory into ROOT folder and then try the same URL.
Put it anywhere, but put a context file for your webapp to TOMCAT_HOME/conf/Catalina/localhost which points to for webapps's directory with the docbase attribute. Read details here.

How to get file system path of the context root of any application

I am working on web application.I invoke on my jsp request.getContextPath(), but strangely I got address /streetshop.
Then I am appending some path as request.getContextPath() + "abc" and create folder.
Then its creating folder in D:// instead of my webapplication folder.
Please, tell me, I want to upload an image in put it in my web-application root/images/images.gif.
You mix things up here. HttpServletRequest.getContextPath() returns your web application root path. In your example this is /streetshop, so your URL may look similar to www.myapp.com/streetshop. If you want to access the internal file system path, you must obtain it from the ServletContext using request.getServletContext().getRealPath("/"). This should return the location of your WAR files' WebContent folder.
Keep in mind that if you modify contents of this path during runtime, you're going to loose everything when redeploying your application.

Java: Accessing properties file inside a war

I already searched StackOverflow for "properties inside war", but none of the results worked for my case.
I am using Eclipse Galileo and GlassFish v3 to develop a set of web services. I am using a "dynamic web project" with the following structure
Src
-java_code_pkg_1
-java_code_pkg_2
-com.company.config
--configfile.properties WebContent
-META-INF
-WEB-INF
--log4jProperties
--web.xml
--applicationContext.xml
--app-servlet.xml
I want to access the "configfile.properties" inside one of the source files in "java_code_pkg1". I am using the Spring Framework and this file will be instantiated once the application starts on the server.
I have tried the following with no luck
getResourceAsStream("/com.company.config/configfile.properties");
getResourceAsStream("/com/company/config/configfile.properties");
getResourceAsStream("com/company/config/configfile.properties");
getResourceAsStream("/configfile.properties");
getResourceAsStream("configfile.properties");
getResourceBundle(..) didn't work either.
Is it possible to access a file when it's not under the WEB-INF/classes path? if so then how?
Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/com/company/config/file.properties"));
works when I'm in debug mode. I can see the values in the debugger, but I get a NullPointerException right after executing the "props.load" line and before going into the light below it.
That's a different issue. At least now I know this is the way to access the config file.
Thank you for your help.
If you are in a war, your classpath "current directory" is "WEB-INF/classes". Simply go up two levels.
getResourceAsStream("../../com/company/config/configfile.properties");
It is horrible but it works. At least, it works under tomcat, jboss and geronimo and It works today.
P.S. Your directory structure is not very clear. Perhaps it is:
getResourceAsStream("../../com.company.config/configfile.properties");
Check the location of the properties file in WAR file.
If it is in WEB-INF/classes directory under com/company/config directory
getResourceAsStream("com/company/config/configfile.properties") should work
or getResourceAsStream(" This should work if the config file is not under WEB-INF/classes directoy
Also try using getClass().getClassLoader().getResourceAsStream.
Are you sure the file is being included in your war file? A lot of times, the war build process will filter out non .class files.
What is the path once it is deployed to the server? It's possible to use Scanner to manually read in the resource. From a java file within a package, creating a new File("../applications/") will get you a file pointed at {glassfish install}\domains\{domain name}\applications. Maybe you could alter that file path to direct you to where you need to go?
Since you are using Spring, then use the Resource support in Spring to inject the properties files directly.
see http://static.springsource.org/spring/docs/3.0.x/reference/resources.html
Even if the class that requires the properties file is not Spring managed, you can still get access to the ApplicationContext and use it to load the resource
resource would be something like, classpath:settings.properties, presuming that your properties file got picked up by your build and dropped in the war file.
You can also inject directly, from the docs:
<property name="template" value="classpath:some/resource/path/myTemplate.txt">

Categories