Eclipse Java Servlet access to higher level directory - java

I've installed Eclipse with web development and during download have installed Tomcat7 to dir E:\Eclipse\tomcat7.
When in Eclipse and trying to test the program I'm getting a realPath of
C:/Users/user/Documents/eclipseJEEWorkspace/javaWebPages/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/org.example.web/WEB-INF/classes/
as my real path and not what I expect which is E:\Eclipse\tomcat7\webapps\org.example.web/WEB-INF/classes/
1) how do I get the real path of the servlet rather than the temporary path...
or am I missing something with how Eclipse uses the installed server? If this is the way then do I have to continuously create the WAR and fiddle around recreating/deleting all the time?
The reason for this is I'm also trying to get a JAVA Servlet getting access to a file in a path higher than webapps. I'm new to servlets....
My development includes third party software that all reference an individual file our.properties so changing the structure is unfortunately not a option.
My directory structure is:
/tomcat
/mycompany
/properties
our.properties //the file we want to access
/*otherfiles
/html
/*not used in this context but to show usage
/javascript
/*not used in this context but to show usage
/webapps
/org.example01.web
/META-INF
/WEB-INF
/org.example02.web
/META-INF
/WEB-INF
/org.example03.web
/META-INF
/WEB-INF
How do I tell my Servlet to access the our.properties file?
I've tried getPath, getResource but without getting the first bit to work, I doubt I'll be getting anywhere fast.
Thanks

When you run Tomcat under Eclipse, via the Tomcat plugin, the webapps path is changed. Eclipse uses .metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ folder to deploy the project. That's not a temporary path, it's the actual path since your web application is deployed there.
If you run tomcat from command line, or as a service and deploy your web application, you'll see that the path of the servlet will be as you expected.
In order the change the default path to deploy for Eclipse, double click to your Tomcat under Servers view and modify Server locations section.

Related

Cannot see Tomcat homepage( http://localhost:8080) when starting it from eclipse. But it loads when I start it from tomcat bin directory. Any idead?

I have added tomcat 7 to my eclipse.
When I start the server it gets started successfully(shows the message in console) but when I hit the URL http://localhost:8080 it says "404 resource not found".
Strange thing is when I go to Tomcat bin directory and start it through start.bat, the same url http://localhost:8080 shows the tomcat homepage in browser without any issue.
Any idea? Help is appreciated.
You need to be aware of two directory trees with Tomcat : TOMCAT_HOME and TOMCAT_BASE.
TOMCAT_HOME contains binary related data and can be shared by many running Tomcat instances (it contains jar, dll, exe and other non-contextual data).
TOMCAT_BASE specifies configuration files (ie catalina.properties, server.xml, context configurations), webapp directories and further files such as specific librairies if required.
When running Tomcat from his installation directory: TOMCAT_HOME and TOMCAT_BASE are set to same location. Then, Tomcat serves ${TOMCAT_HOME}\webapps directory. The special ROOT context let serve the root path and by default contains a simple webapp (browse ${TOMCAT_HOME}\webapps\ROOT for more information).
When running Tomcat from Eclipse, Eclipse will setup two directories:
with editable configuration (generally goes under ${WORKSPACE}\Servers\${SERVER_NAME}-conf)
with deployed files from last describe directory, served context configuration, deployed context and some other working files such as log and work directories.
This last directory generally goes under your workspace metadata but you can edit it from the server editor. And Eclipse uses this directory as TOMCAT_BASE. To this directory, Eclipse doesn't deploy the default ROOT webapp neither other default ones (docs, examples, host-manager and manager). If necessary you can add them from the server configuration directory (first one described).

Relative Path not working in Vaadin Application

I have a Vaadin application in Eclipse running using Tomcat. I have an external jar file that requires access to a folder "/WordNet-JWI/3.0/dict/".
This works perfectly fine in a normal Java application, where the folder stays in the main project folder, but in a web application, it can't resolve this relative path and gives an error Dictionary directory does not exist: WordNet-JWI/3.0/dict
I am fairly new to Web applications and don't know in which folder to put the Wordnet-folder and how to make it available to the external jar for it to run properly.
PS - I put the external jar in /WEB-INF/lib and is working fine.
Based on your path the file should be located starting from the root of your partition.
For example if you are using windows it would be C:/WordNet-JWI/3.0/dict/ if you are running the webapp on C:
Mind that on unix the "user" running the Tomcat process must have r/w permission on that folder.
Regards.

Getting the Path to WEB-INF on Openshift Tomcat 6 - JBoss EWS 1.0

I previously used the following to get my context path on Tomcat-5.0.28 and earlier:
String context_path = context.getRealPath("/WEB-INF/");
This worked to return the path to that folder.
But on OpenShift (Tomcat 6 - JBoss EWS 1.0) this returns
/var/lib/openshift/53.*context_id_here*..18/jbossews/null
The null should be:
work/Catalina/localhost/_/WEB-INF
How can I get the path to the WEB-INF folder on OpenShift using JBOSS/Tomcat?
A little background information: When I ran this struts webapp on my own Tomcat server, I deployed a appname.war file in the webapps directory and waited for it to expand (since I had set that option in the server.xml file). Then I move a folder to webapps/appname/ folder with xml files I need to read and write to for my app to work. On OpenShift I used jar xvf appname.war to extract the war file by hand (because that's the default and I don't know how to change it), and then moved the files folder (from the same directory as the war file in my folder after a git add and push) to work/Catalina/localhost/_/WEB-INF/
This is causing a NullPointerException for me when trying to use that path as shown above.
I think you should be using something like getServletContext or getRealPath, and reading it from the web root instead of trying to find the file on the physical disk. That way your war file can run anywhere without issue. Try looking up both of those and see if one fits your use case.

Website directory structure different from eclipse

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.

Where does Eclipse store generated servlet files for Tomcat?

I'm using Eclipse Java EE IDE and launch Tomcat from the Server's tab on Eclipse.
Where does Eclipse store generated servlet .java files for JSP files? I've checked the Tomcat installation directory, but nothing there.
Thanks.
Doubleclick the server entry in Servers view and check the path represented by Server path. Explore in there from inside the workspace directory. The generated classes are there.
If you choose the 2nd option Use Tomcat installation, then it will be available Tomcat's /work folder, there where you expected it to be.
go to your application work space directory(not eclipse installation directory) in windows explorer(not in eclipse IDE explorer).
in my laptop it is d:/applicationdata/j2ee
then follow the path as:
work space directory(in my case j2ee)/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/catalina/localhost
here u will find your application
Eclipse doesn't generate servlet files for JSP files. Validation happens directly on JSP syntax. When a JSP is deployed to an app server like Tomcat, the server may choose to generate servlet files to disk, but that is not required. The generation and compilation can happen in memory or the app server may even compile JSP files directly to bytecode.
I don't know specifically about Tomcat, but if it does generate servlet files to disk, the temporary directory containing these files will be somewhere under your Tomcat install.
/home/waheguru/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/Catalina/localhost/jspnotes/org/apache/jsp
localhost or your website name,
project name is jspnotes, whatever your project.
This path is shown in Linux Mint.
Check META-INF\context.xml file from your work space. There will be path metioned like below
workDir="C:\apache-tomcat-7.0.39\work"
In above path you will find the package structure of your project and in it there will be both *_jsp.java and *_jsp.class

Categories

Resources