Where to put jsp project folder in tomcat for hosting? - java

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.

Related

Access to web-inf from deployed jar

I'm currently facing a problem any time i try to access to WEB-INF/classes folder.
Basically if I run the whole project on a local server it works and find the given file, but when i try to access to the same file after all the jar files have been created and deployed it gives my an error - the file doesn't exists
this is the current path on the server :
file:/product/WebSphere85/AppServer/profiles/opntship_node_1/installedApps/opntship/xxxService.ear/lib/xxxServiceService.jar!/xxx.pdf (A file or directory in the path name does not exist.)
this is the path on my local server:
/C:/Users/foo42/IBM/rationalsdp/workspace/TpdPrintServiceService/target/classes/xxx.pdf
this is how I get the file :
getClass().getResource("/"+fullFileName);
fileToBytes(new File(filePath.getFile()));
I tried in many ways to get access to the folder but still, it works locally but not on server :(
any idea how to fix this problem and access to the web-inf folder ?
Thanks in advance!
We cannot read the entries within an archive (xxxService.ear and xxxServiceService.jar) like it was a plain old File. You may want to try Thread.currentThread().getContextClassLoader().getResourceAsStream(path) instead.

unable to find servlet(.java) files converted from jsp

I am using eclipse luna and tomcat version 7. I have written few jsp files and executing them on tomcat easily. I have read that jsps are converted into servlets at run time and you can locate them in tomcat/work/catalina/localhost/project name and further. My project name is quizilla and there exist a folder named quizilla-1.0-SNAPSHOT, but this folder is empty. What is the reason and where can i find those .java files. I have attached the screen shot of the folder as wellAs I am in right directory in search of my java files, but the folder is empty. So what should i do
You are using Tomcat from Eclipse, so the work directory is:
projectworkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
or something like that (if you haven't changed the Server configuration via Eclipse).
Add
<%=getClass().getResource(getClass().getSimpleName() + ".class")%>
to one of your JSP pages to detect where Tomcat has generated the servlet.

web.xml configuration for JSP

This is the structure of the JSP app on eclipse.Once I run it on eclipse using tomcat server(localhost:8090/index4.html) it works.On the index page i have to add details,this details are uploaded via the servlet as you can see above Java Resources->src->FileUPloadDBServlet(Also I am not sure if it uses this or it uses WEB-INF->src->FileUploadServlet below.)
In my index4.html the action is action=uploadServlet(no address given for it).This everything works on using eclipse.
But once I put the QMS folder(not WAR) from eclipse worskspace to tomcat ROOT,the index4.html works but the following action,i.e uploadServlet doesnt work(here I use the address localhost:8090/WebContent/index4.html)I dont have a web.xml.
Is that creating a problem?
Please provide me help.
You need to put your project folder or .war file in the
webapps folder of Tomcat directly not in ROOT folder under webapps.
Try to follow directory structure as follows
Photo Courtesy http://www.studytonight.com/servlet/steps-to-create-servlet-using-tomcat-server.php (Here you can also find more details on Servlet and JSP, as well as project structure)
Where you have your web.xml put into the WEB-INF folder under your application folder.
For java files you don't need the source files, the classes folder will have all the source folder (src) files compiled and ready to execute.
The lib will contain all the .jar files you need for your application to run.
Make sure you put web.xml file at proper place, because without it, application will not be able to run. Because as they say web.xml is Heart of the application.
Let me explain you the problem.
When we configure a dynamic java project to run on eclipse the server takes the just the stuff from webcontent folder and deploy it the wbcontent folder contains web.xml file which defines the url descriptor for servlet.
now when you copy the whole folder the server can not find the web.xml file which is a descriptor where it expects the file to be.
hope its clear comment for clarification

To get the root path of file in java

I am using eclipse Indigo IDE ..I am developing a jsp application (myProject) using eclipse in which i have a property file (myProject\webcontent\db.properties) for configuring database credentials.
I am trying to access this file from a class (myProject\src\samplePackage\sampleDBConnect.java). I have exported samplePackage.jar into *myProject\webContent\WEB-INF\lib*.
I have a Test.jsp page which calls a method in sampleDBConnect.java.
When i am trying to run this Test.jsp page, the current directory path shows C:\Documents and Settings\username. I have my project in some other drive(E:).
Can someone tell me how to access the properties file....
getClass().getResourceAsStream("db.properties");
This assumes your db.properties is in the same directory "samplePackage".
If you want to keep it in src directory or say resource directory then use
getClass().getClassLoader().getResourceAsStream("db.properties");
Since your java code belongs to the same web project you don't have to create a jar of the same project and place that in the WEB-INF/lib folder
move db.properties to myProject\src dir,
java code:
InputStream input = sampleDBConnect.class.getClassLoader().getResourceAsStream("db.properties");

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.

Categories