This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I've been reading the topics on Servlets the whole day, but I still don't know why I can't get a simple servlet to work.
I am studying Java so I'm trying an example from my book but it just won't work.
Here is what I've done:
1. Set up JAVA_HOME to C:\Program Files\Java\jdk1.5.0_01.
2. Downloaded JBoss Application Server 4 and unzipped (jboss-4.0.5.GA.zip) to my C:\ drive.
3. I've tested that it works fine (http://localhost:8080/).
4. I've installed JAVA EE 5(java_ee_sdk-5_01-windows-nojdk.exe).
5. Set up JAVAEE_HOME to C:\Sun\SDK. and CLASSPATH to %JAVAEE_HOME%\lib\javaee.jar.
6. Created a java file: NameServlet.java and saved it in (C:\Exercises\NameServlet)
7. Created a directory (C:\Exercises\NameServlet\WEB-INF\classes).
8. Comlpield the java file: javac -d WEB-INF\classes NameServlet.java inside C:\Exercises\NameServlet at the command prompt.
9. Created a xml file: web.xml and saved it in (C:\Exercises\NameServlet\WEB-INF).
10. Packaged the web.xml file into a war file: (jar -cvf nameserv.war *.* at the command prompt in C:\Exercises\NameServlet ).
11. Copied the newly created nameserv.war file into the C:\jboss-4.0.5.GA\server\default\deploy directory.
12. Created a html file index.html in C:\Exercises
13. Packaged the index.html file into a war file: (jar -cvf index.war index.html at the command prompt in C:\Exercises ).
14. Copied the newly created index.war file into the C:\jboss-4.0.5.GA\server\default\deploy directory.
15. Started the Jboss Server.
16. In web Browser went to http://localhost:8080/index.(The page displays correctly) 17. Click on NameServlet.jsp link - error message displays:
HTTP Status 404 - /index/localhost:8080/nameserv/NameServlet
ONE more time - ALL classes used in servlets MUST be in a package and stored under the classes directory according to the package. The reason being that when the JVM tries to load a class that is not in a package (default package) it looks in the "current" directory.
In the servlet environment you have no control over the current directory, therefore the class is never loaded and the resource can not be found.
in your servlet add
in web-inf/classes create folder MyPkg and put your modified servert there.
Author and all-around good cowpoke
Joined: Mar 22, 2000
what package it should be in
OK, short story on packages. Packages are used in Java (among other things) to group classes, as part of the access control mechanism and to distinguish between classes with the same name but different origins.
Thus you will find that the java.io package contains classes related to IO - the "java" is reserved for the standard library classes.
In the org.w3c.dom package you find interfaces define by the W3C standards for XML.
Developers may make up a package name based on their web site location, I use com.wbrogden alot for obvious reasons.
Anyway, back to your problem - what is the directory structure of your "nameserv" directory?
Joined: Jun 23, 2009
Hi thank you again people,
Shivendra I tried what you said. It still does not want to work, but I will try it again just to make sure.