wood burning stoves 2.0*
The moose likes Servlets and the fly likes Head First Servlets and JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Head First Servlets and JSP" Watch "Head First Servlets and JSP" New topic
Author

Head First Servlets and JSP

Wendy McAvoy
Greenhorn

Joined: Aug 11, 2007
Posts: 8
I know it is probably something simple but I am trying to go through the tutorial in chapter 3. The book says to use the following to compile the servlet

javac -classpath C:\JavaCode\MyProjects\beerV1>javac -classpath c:/Tomcat5.5/common/lib/servlet-api.jar:classes:. -d classes src/com/example/web/BeerSelect.java

when I use the :classes:. it won't compile the errors I get are that it can't find the servlet classes.

If I remove it the code compiles but when I run it, I get errors saying it "description The requested resource (/Beer-v1/SelectBeer.do) is not available."

Are these two issues connected? Any idea what I am doing wrong? Any suggestions on what to look at?

Thanks

Wendy
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

/Beer-v1/SelectBeer.do


Could you please post your web.xml ? Perhaps the mappings are wrong. Are you using struts ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Wendy McAvoy
Greenhorn

Joined: Aug 11, 2007
Posts: 8
Here is the web.xml. It is just a simple example from the book so no I am not using struts.

Thanks

Wendy

<Web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocaton="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.example.web.BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
</Web-app>
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The deployment descriptor (web.xml) has nothing to do with compiling a servlet.

I'm going to move this to our Java In General (Beginner) forum where javac and classpath questions are discussed.

This FAQ entry may help:
http://faq.javaranch.com/view?CompilingServlets


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Use one '-classpath' switch.
Separate the entries with a semi-colon (on Windows, Unix is different)

Wendy McAvoy
Greenhorn

Joined: Aug 11, 2007
Posts: 8
This is not strictly just a compile question. I can get it to compile with
>javac -classpath c:/Tomcat5.5/common/lib/servlet-api.jar -d classes src/com/example/web/BeerSelect.java

It doesn't compile with what you suggested. The problem is when I compile it with removing the :classes:. then when I run it, it can't find the page. What are the authors of the book trying to accomplish with this syntax and is the fact that I am changing the syntax to get it to compile causing the problem with not being able to find the servlet.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you go it to compile, then this needs to come back to servlets.

Your classpath settings when you compiled the program has nothing to do with the container finding the servlet.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I've asked to have this thread put back in the servlets forum.
In the mean time, what are you using for a URL when trying to access your servlet?
Wendy McAvoy
Greenhorn

Joined: Aug 11, 2007
Posts: 8
I start by putting in
http://localhost:8080/Beer-v1/form.html

I select a color and hit submit and the browser now has

http://localhost:8080/Beer-v1/SelectBeer.do for the url

The error on the screen reads:

message /Beer-v1/SelectBeer.do

description The requested resource (/Beer-v1/SelectBeer.do) is not available.
Wendy McAvoy
Greenhorn

Joined: Aug 11, 2007
Posts: 8
The book seem to have a space in the servlet-name in the DD. I tried removing the space and everything worked.

Thanks for your help.

Wendy
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Wendy, you should check out the errata for the book. It has a lot of these errors. You will save yourself a lot of headaches going forward...
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Michael Ku:
Wendy, you should check out the errata for the book. It has a lot of these errors. You will save yourself a lot of headaches going forward...


I checked the errata and nothing was mentioned about why the authors chose to type their compile command this way.

Wendy: would you mind printing the compile command that eventually ended up working for you? I too have been omitting the ":classes:." part as my workaround. Did you get it to work with the ":classes:."?
[ August 12, 2007: Message edited by: Amanda Albert ]

Sun Certified Programmer for the Java 2 Platform 1.4
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Amanda, it was just a general piece of advice concerning using HFSJ book. It comes from painful experience
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Michael Ku:
Amanda, it was just a general piece of advice concerning using HFSJ book. It comes from painful experience


No problem. The edition I have actually has some of the errors corrected, so I'm confused. I'm stuck again trying to compile version 2 of the BeerSelect servlet, so I'm wondering if additional changes were made that were not included in errata, or if while correcting some things, additional mistakes were made.

This is really frustrating.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The thing to remember is that 'getting it to compile' and getting the container to find it are two totally different issues.

If you have a '.class' file, it compiled.

As long as the directory structure under WEB-INF/classes matches your package, and the package matches what you've put in web.xml, the container should be able to find it.

If it will help, we have some simple web applications in our code barn that have been pre-packaged as war files. All you need to do is drop the war file in your TOMCAT-INSTALL/webapps directory and Tomcat will unpack and deploy them. This will give you a working web application with properly packaged classes, in the correct directory, and a properly configured deployment descriptor (web.xml) to start with or compare to your HeadFirst project.
http://www.javaranch.com/codebarn/codebarn-servlets.jsp
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Amanda, did you download the version 2 from the Head First site
? It is a Struts version and will require more care in setting up.

Or is it the one on page 84 of HFSJ?
[ August 12, 2007: Message edited by: Michael Ku ]
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Michael Ku:
Amanda, did you download the version 2 from the Head First site
? It is a Struts version and will require more care in setting up.

Or is it the one on page 84 of HFSJ?


Yeah I'm trying to compile the version of BeerSelect (CoffeeSelect for me) on page 84. Mine looks exactly like what's typed there except for the obvious exception. When I compile, it can't find the com.example.model package for the CoffeeExpert class. Perhaps I should ask my question in the Java beginner forum.

And by "Head First" site, are you referring to wickedlysmart.com?
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Ben Souther:
The thing to remember is that 'getting it to compile' and getting the container to find it are two totally different issues.

If you have a '.class' file, it compiled.

As long as the directory structure under WEB-INF/classes matches your package, and the package matches what you've put in web.xml, the container should be able to find it.

If it will help, we have some simple web applications in our code barn that have been pre-packaged as war files. All you need to do is drop the war file in your TOMCAT-INSTALL/webapps directory and Tomcat will unpack and deploy them. This will give you a working web application with properly packaged classes, in the correct directory, and a properly configured deployment descriptor (web.xml) to start with or compare to your HeadFirst project.
http://www.javaranch.com/codebarn/codebarn-servlets.jsp


Thanks Ben,

I was fine up until page 84, so I don't know what happened. I'll download your example and see if it helps, but I was able to deploy my form and initial version of the example servlet just fine earlier. I haven't made any changes to my directory structures.

Amanda
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I don't have the book you're using so I have no idea what's on page 84.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Amanda, yes - wickedlysmart.com
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

One of the best pieces of advice I've ever received from a senior when I was about to get into servlet programming was:
Don't try to tackle servlets without first spending some time learning the fundamentals of Java. In particular, get a real solid understanding of javac, classpaths, and packages. Don't just memorize the syntax to get your code to compile. Make sure you understand what the classpath is and how it's used by the JVM and by the compiler.

Because I took this advice (learned the easy way) I never realized how difficult it is to tackle servlet configuration while trying to learn the basics of Java at the same time until I started seeing people trying to do it here. If you try to tackle everything at once, it is very difficult, when things go wrong, to separate the issues and determine which is tripping you up. The slightest error (either in the text of the book itself, or in your interpretation of the books text) will throw you way off course and leave you stuck.

If you're in a hurry and really want to learn serlets quickly, put the servlets book aside and either buy a book on core Java or find some good tutorials on line. Skip right past any book or tutorial that starts you out learning with an IDE. Learn how to set your classpath, compile and run command line programs with nothing but a text editor and a command window.

Once you can sit down on any machine with a JDK, write, compile, and run a command line Java app, AND figure out quickly what's going wrong when you see all the typical classpath errors, THEN go back to your servlets and JSP book. You'll not only learn everything faster, you'll have a much more pleasant (or, at least; much less aggravating time) learning it.
[ August 13, 2007: Message edited by: Ben Souther ]
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Ben Souther:
One of the best pieces of advice I've ever received from a senior when I was about to get into servlet programming was:
Don't try to tackle servlets without first spending some time learning the fundamentals of Java. In particular, get a real solid understanding of javac, classpaths, and packages. Don't just memorize the syntax to get your code to compile. Make sure you understand what the classpath is and how it's used by the JVM and by the compiler.



Thanks for this great piece of advice. Do you have any books in mind that don't teach Java through an IDE?

Amanda
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I like "Core Java" by Horstman Cornell
Published by: Prentice Hall
I have the seventh edition on my desk. It comes in two volumes but the first is all you need to get a firm grip the fundamentals.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I like "Core Java" by Horstman and Cornell
Published by Prentice Hall
I have the seventh edition on my desk. It comes in two volumes but the first is all you need to get a firm grip the fundamentals.
Amanda Albert
Ranch Hand

Joined: Jun 12, 2006
Posts: 41

Originally posted by Ben Souther:
I like "Core Java" by Horstman and Cornell
Published by Prentice Hall
I have the seventh edition on my desk. It comes in two volumes but the first is all you need to get a firm grip the fundamentals.


Thanks for the recommendation!

Amanda
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Servlets and JSP