I am trying to learn Java Struts (I am completely new to Java web applications) using a tutorial book, and the first project is a simple, Model 2 Java Web application (non-strut) with 2 classes and 2 JSP pages. However, I cannot even get that to work! I have a Dynamic Web Project in Eclipse and that project has a class called "ControllerServlet" that resides in a package called "app01a"; my web.xml looks like this:
When I run the Tomcat server through Eclipse and try to navigate to "http://localhost:8080/app01a/Controller?action=displayAddProductForm," the server returns an error saying "The requested resource (/app01a/Controller) is not available." I have tried different variations on the URL (without "/app01a" for instance) and I have tried moving my class files (they reside in the project folder under /build/classes/app01a) to the /WebContent/app01a and /WebContent/WEB-INF/app01a, both in and out of a jar file, but no luck. I can navigate to the jsp pages directly.
I wrote this exact same application a few days ago and got it to work, I just cannot remember the combination of class file location, URL and web.xml that I had (when I wrote it the first time I had put the classes in a long, convoluted package, which is why I decided to start over again).
Java web apps live in an application context. When you deploy an app you deploy it to a specific context, like "/" (the root context), "/foo", or whatever. If it's deployed to the root context your URL would just be "/Controller". You might want to check online for some basic Java webapp stuff if the book doesn't cover it.
Your classes should live in /WEB-INF/classes, in a package hierarchy. If you're not sure about how Java packaging/class hierarchies work you should search online for some basic info.
Eclipse should be deploying them properly for you already--you shouldn't have to move anything; Eclipse knows how to deal with web apps.
Joined: Aug 23, 2009
Thank you for the explanation. As far as I can tell, Eclipse is not putting the classes in a /WEB-INF/classes directory. However, I myself built that directory through eclipse and tried both putting the class files into it and adding the class files to a .jar file (using an /app01 context in the .jar file) which I then copied to /WEB-INF/classes. Maybe I have something set up wrongly in Eclipse if it isn't doing this automatically for me?
Joined: Aug 23, 2009
If it helps, here is an image of what my project explorer looks like for this app (the class files are also in the /build directory but Eclipse doesn't show them in the explorer).
Also, to start the server I will go to the displayAddProductFormApp01a.jsp and hit the Run As button. It will display the page and the URL will be "http://localhost:8080/Struts_app01/displayAddProductFormApp01a.jsp," though I expected the URL without the "Struts_app01" in it, I figure this is something Eclipse does to separate the projects? I then try to navigate to the ControllerServlet using both "http://localhost:8080/app01/Controller?action=displayAddProductForm" and "http://localhost:8080/Struts_app01/app01/Controller?action=displayAddProductForm," but neither work.
I don't know what running a JSP as a web app would do; I'm not sure why you wouldn't just pick the server and add a deployment to it. It's going to be hard to help "from scratch" without sitting in front of the machine and setting everything up right. You might want to spend some time with Eclipse tutorials/screencasts/etc. to get some of the basics down.