Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java ee Servlet does not work - error 404

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think the link on your index page me be incorrect.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put your servlet class in web-inf/classes folder
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be a typo, but should be
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic