Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie problem from chapter1 of Head First Servlets & JSP

 
Carlos Obregon
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64699
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1: Put the servlet in a class other than the default

Step 2: If it works, profit! If not, post back.
 
Carlos Obregon
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry but I don't understand what do you mean by "default"!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64699
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64699
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Carlos Obregon
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah! You are right it's a mistype I needed to type as the url http://localhost:8080/ch1/Serv1

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic