File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Newbie problem from chapter1 of Head First Servlets & JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Newbie problem from chapter1 of Head First Servlets & JSP" Watch "Newbie problem from chapter1 of Head First Servlets & JSP" New topic
Author

Newbie problem from chapter1 of Head First Servlets & JSP

Carlos Obregon
Greenhorn

Joined: May 26, 2009
Posts: 18
I'm reading Head First Servlets & JSP.

In Chapter1 there's a small example but I canĂ½ get it work.

I wrote this class



Which I compiled without problems.

I wrote this DD:



This is where the files are on my PC:

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ch1\WEB-INF\web.xml
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ch1\WEB-INF\classes\Ch1Servlet.class


I try to access it using:

http://localhost:8080/ch1/Serv1/

But Tomcat gives my an error 404.

What am I doing wrong?

Thanks a lot!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Step 1: Put the servlet in a class other than the default

Step 2: If it works, profit! If not, post back.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Carlos Obregon
Greenhorn

Joined: May 26, 2009
Posts: 18
I'm sorry but I don't understand what do you mean by "default"!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Basic Java: the default package is used when you don't specify a package.

As of Java 1.4, the default package is not automatically included. Bottom line: place all of your classes in an explicit package.

Please see this FAQ entry.
Carlos Obregon
Greenhorn

Joined: May 26, 2009
Posts: 18
Ok. Thanks! I used other package but it still doesn't work:

My Servlet class is:



My web.xml file is:



The files are in:

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ch1\WEB-INF\classes\com\examples\Ch1Servlet.class
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ch1\WEB-INF\web.xml

Thank you a lot!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

OK, that's been eliminated as a possible source of error.

Now, saying "it doesn't work" isn't all that helpful. Please read this for more information.

What is the URL you are using, and what is the exact text of the error message?
Carlos Obregon
Greenhorn

Joined: May 26, 2009
Posts: 18
The URL I'm using is:

http://localhost:8080/ch1/Serv1/

Its a 404 error it says "required resource is not available"

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3829

Have you restarted the ch1 context/tomcat server after placing the servlet class in the right place ? (Or when you put the changed web.xml there context will get restarted automatically).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Carlos Obregon
Greenhorn

Joined: May 26, 2009
Posts: 18
I'm not sure I'm following.

Everytime I tried something I shutdown Tomcat first, the I restarted. Is there anything else to do to restart the context/Tomcat?

I did not understand what you said about the web.xml file

As I come to understand the thing is:

Everything that Tomcat will use must be in the directory "webapps"

Inside that directory there must be a directory per app.

Inside each app root directory there must be a WEB-INF directory. Inside it there must be a "classes" directory and under it there must be directories that matches the packages you are using.

Static context that you intent your client to use must be in the root or a directory structure not under WEB-INF.

The web.xml must be in the app root directory.

Right? Is that so for every Tomcat version or it may have changed?

Thanks a lot.
Denis Wen
Ranch Hand

Joined: Nov 11, 2008
Posts: 33
I assume you have mistyped closing tag in line
<url-pattern>/Serv1</servlet-class>
?

What does your tomcat error log say?



[quote=Carlos Obregon]I'm not sure I'm following.

Everytime I tried something I shutdown Tomcat first, the I restarted. Is there anything else to do to restart the context/Tomcat?

I did not understand what you said about the web.xml file

As I come to understand the thing is:

Everything that Tomcat will use must be in the directory "webapps"

Inside that directory there must be a directory per app.

Inside each app root directory there must be a WEB-INF directory. Inside it there must be a "classes" directory and under it there must be directories that matches the packages you are using.

Static context that you intent your client to use must be in the root or a directory structure not under WEB-INF.

The web.xml must be in the app root directory.

Right? Is that so for every Tomcat version or it may have changed?

Thanks a lot.[/quote]
Carlos Obregon
Greenhorn

Joined: May 26, 2009
Posts: 18
Yeah! You are right it's a mistype I needed to type as the url http://localhost:8080/ch1/Serv1

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Newbie problem from chapter1 of Head First Servlets & JSP