wood burning stoves 2.0*
The moose likes Servlets and the fly likes Java ee Servlet does not work - error 404 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java ee Servlet does not work - error 404" Watch "Java ee Servlet does not work - error 404" New topic
Author

Java ee Servlet does not work - error 404

Dene Lamprecht
Greenhorn

Joined: Jun 23, 2009
Posts: 14
Hi People
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

--------------------------------------------------------------------------------

type Status report

message /index/localhost:8080/nameserv/NameServlet

description The requested resource (/index/localhost:8080/nameserv/NameServlet) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.20


IT JUST WON'T WORK.
Can someone please tell me what I'm doing wrong?

Thank you
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377



I think the link on your index page me be incorrect.


I love this place!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The url you are accessing appears to be:

/index/localhost:8080/nameserv/NameServlet


So this is asking for a web application called "index", that has sub-directories /localhost:8080/nameserv/NameServlet. Have a look at your hyperlink; you may be missing a couple of forward slashes.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
put your servlet class in web-inf/classes folder


SCJP 1.5(97%) My Blog
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
It might be a typo, but should be
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5


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.

Read about the long and sordid tale of the Invoker servlet here.

Bill
Dene Lamprecht
Greenhorn

Joined: Jun 23, 2009
Posts: 14
Hi people thank you for the replies,
I have done what Eduardo Bueno said and changed my link in the html file to:


I have packaged it again and when I run it now I get the following error:
HTTP Status 404 - /nameserv/NameServlet

type Status report

message /nameserv/NameServlet

description The requested resource (/nameserv/NameServlet) is not available.

Apache Tomcat/5.5.20



And Bill I took your advise and read the the long and sordid tale of the Invoker servlet here.
but still don't know what package it should be in, I'm a beginner to Java, please tell me.

Thank you
Dene
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
modify
<servlet-mapping>
<servlet-name>NameServlet</serlet-name>
<url-pattern>/NameServlet</url-pattern>
</servlet-mapping>

to
<servlet-mapping>
<servlet-name>MyPkg.NameServlet</serlet-name>
<url-pattern>/NameServlet</url-pattern>
</servlet-mapping>

in your servlet add

package MyPkg;

in web-inf/classes create folder MyPkg and put your modified servert there.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
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?

Bill
Dene Lamprecht
Greenhorn

Joined: Jun 23, 2009
Posts: 14
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.

---------------------------------------------------------------------------------------

Bill the directory structure of my "nameserv.war" file is:
C:\Exercises\NameServlet where it was created

and I've copied the file to:
C:\jboss-4.0.5.GA\server\default\deploy

Thank you for trying to help.
Dene
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java ee Servlet does not work - error 404