file created using getRealPath gets created on .metada/.plugins folder not in WebContent project folder - eclipse

I am trying to create a file using below code in a Servlet:
File outfile= new File(servletContext.getRealPath("/Output/output.xml"));
OutputStream os = new FileOutputStream(outfile);
from a J2EE Application (CallMain) and the file gets created in deployed temp path
.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\CallMain\Output
Actually I want the file to be created in current project folder: WebContent.

Actually when you ran the servlet within eclipse using the embedded tomcat server
getServletContext().getRealPath("WEB-INF")
Gives you this
D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\DynaServletProject\WEB-INF
But when you ran the servlet from standalone Apache Tomcat:
getServletContext().getRealPath("WEB-INF")
This gives you
D:\apache-tomcat-7.0.33\webapps\DynaServletProject\WEB-INF
While in eclipse you can give the absolute path i dont think this is the best approach though

By default, your application stages and runs from a directory buried in the workspace's .metadata directory. That is the "real path" at runtime. Check your Server's configuration for options regarding deploying the app directory from the workspace folders.

Related

web app file spec mismatch between Eclipse project and Eclipse-generated .war

I am developing a web app in Eclipse for deployment to a Tomcat server. Currenly, it works fine when I launch it from within Eclipse, using a Tomcat installation on the localhost. But when I export a .war file and install it on a remote server, it fails because of a mismatch between the file system in Eclipse and the file system in the .war file.
When Eclipse created the Java EE project for me, it created the following directory structure:
app
src
WebContent
I put all of my Java source files---servlets and classes---in the src directory, and all of the Web files---.jsp, .html, and various .xml resources---in WebContent.
So that my app could use the .xml resources, I put them all in a WebContent subdirectory named, not coincidentally, resources.
I then defined a variable pointing to these files...
path_to_resources = "WebContent" + pathMrkr + "resources" + pathMrkr;
// where pathMrkr = System.getProperty("file.separator")
...in each class that needs to access the .xml files.
This works well when I run the app from within Eclipse. But when I export and deploy the app if fails. The .war file Eclipse creates does not include a WebContent folder, so the search for resources fails, and the app crashes, shutting down Tomcat in the process.
So, I guess I'm looking for some advice on how best to structure Eclipse or my app so that a single code base works both in Eclipse on the localhost and from a .war file on a remote server, with a minimum of fuss and fidgeting with build configurations or having to manually edit the .war file.
While I don't think it's pertinent, for the record my system is:
Eclipse Luna, on a Windows 10 localhost, with Tomcat 8 and Java 8. The remote server running in the cloud is a CentOS 5.5 system running Tomcat 7 and Java 8.
Thanks.
Jerry

Problems with relative paths in Eclipse.

I am trying to use relative paths in a Dynamic Web Project in Eclipse. I am NOT using them in the Servlet, but in another class that is called by the Servlet. The file I am trying to access to is a property file located in
MyProject/WebContent/WEB-INF/propertyFile.properties
I have tried almost every relative path...
WebContent/WEB-INF/propertyFile.properties
/WebContent/WEB-INF/propertyFile.properties
./WebContent/WEB-INF/propertyFile.properties
MyProject/WebContent/WEB-INF/propertyFile.properties
/MyProject/WebContent/WEB-INF/propertyFile.properties
WEB-INF/propertyFile.properties
/WEB-INF/propertyFile.properties
./WEB-INF/propertyFile.properties
...and so on...what can I do?
Thanks in advance!
You nee to understand that file IO read files, from the filesystem, and relative to the directory from which the JVM (i.e. your web container) is started.
Remember that, once your app is deployed to production, there won't be any MyProject or WebContent folder. That's what exists on your development machine. The only thing that will exist in production is the war file deployed in the application server.
What you actually want is to load a resource, located in the deployed web application (i.e. which is inside your war file).
To do that, you must use ServletContext.getResourceAsStream():
InputStream in = servletContext.getResourceAsStream("/WEB-INF/propertyFile.properties");

Deploying GWT in localhost and see that webpage

I am a new user in GWT and I want to deploy one of existed samples in my localhost. I could run the example by eclipse and get the result. But I need to deploy that example in my localhost (IIS). How can I do this?
Copy the contents of your project's war directory to your server's document root. You could create a folder there, mproject for example. Then load your project on a web browser: localhost/mproject to view.
Go into your project's war directory For EX:
C:\workspace\HelloWorld\war
Select all the files & folders available inside war directory.
Zip all the selected files & folders in a file called HelloWorld.zip.
Rename HelloWorld.zip to HelloWorld.war.
Deploy it to the server

create temp files in tomcat webapps folder

I have a Google Web Project which works perfect on Development mode. Somewhere inside this project, I create some .xml files which I delete after parsing.
When I deploy the .war file of my project in Tomcat7 (var/lib/tomcat7/webapps) (I use the tomcat manage to deploy it) the project fails to create any file. I've tried all possible paths inside the webapps folder. I even tried context.getRealPath("/")+"/ROOT/tmp/" but nothing happens
You should be using the temp folder instead of attempting to write directly to your webapp's deployment directory:
ServletContext app = (servlet).getServletContext();
File tmpDir = (File)app.getAttribute("javax.servlet.context.tempdir");
File targetFile = new File(tmpDir, "mytempfile.xml");
...
Remember to do everything in a try/catch block and properly clean-up your resources in the 'finally' block or you'll be sorry ;)

Can someone explain what GlassFish does with deployed directories

I have an exploded directory that I am editing in eclipse (created via setting location to directory location when creating New Project, deployed directory already existed). When I deploy this directory from the command line, it does not seem to save it locally (anywhere within glassfish\domain_
If I create a Java EE application in eclipse from scratch and deploy it from Eclipse Run -> on server, it copies it to glassfish\domains\domain1\eclipseapps
Is there a way to have GlassFish save deployed directories locally without using the GlassFish plugin magic?
Short Answer: No.
The whole point of directory deployment is speed and development convenience.
You tell GlassFish to not copy any files at all but to, instead, use the files already laid out on disk.
If you want to run from a copy of your app then just deploy the archive (war/rar/ear/jar) file.