Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / October 2004

Tip: Looking for answers? Try searching our database.

compiled servlet -- class won't run...

Thread view: 
Frances Del Rio - 27 Oct 2004 14:35 GMT
this is on my machine locally on tomcat..

HTML form and servlet are here:

C:\tomcat\webapps\my-servlet\cform.html
C:\tomcat\webapps\my-servlet\cc.java

I'm following a tomcat book ("Tomcat" by Martin Bond), he says after
compiling put the class here:

C:\tomcat\webapps\my-servlet\WEB-INF\classes\cc.class

the weirdest thing is that in the book he has the form tag thus:

    <FORM method="GET" action="servlet/cc">

where does he get this 'servlet' dir from??

I tried this,

    <FORM method="GET" action="WEB-INF/classes/cc">

(the true path to the class..) but neither works; after hitting 'submit'
on form I get a 404, even though I can see address of class on location
bar in browser.. I'm a bit baffled here... would appreciate any
suggestions..  thank you..  Frances
Bryce - 27 Oct 2004 14:42 GMT
>this is on my machine locally on tomcat..
>
[quoted text clipped - 5 lines]
>I'm following a tomcat book ("Tomcat" by Martin Bond), he says after
>compiling put the class here:

I've never read this book, I wonder how old it is.

You need to have your servlet (afaik) defined in your web.xml file.
Like this:

<web-app>
 <servlet>
   <servlet-name>ccServlet</servlet-name>
   <servlet-class>cc</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>ccServlet</servlet-name>
   <url-pattern>/ccurl</url-pattern>
 </servlet-mapping>
</web-app>

(assuming you have a class in your WEB-INF/classes/cc)

Now, with this your form tag would look like this

<FORM method="GET" action="ccurl">
...

>C:\tomcat\webapps\my-servlet\WEB-INF\classes\cc.class
>
[quoted text clipped - 12 lines]
>bar in browser.. I'm a bit baffled here... would appreciate any
>suggestions..  thank you..  Frances

--
now with more cowbell
Frances Del Rio - 27 Oct 2004 17:12 GMT
>>this is on my machine locally on tomcat..
>>
[quoted text clipped - 28 lines]
> <FORM method="GET" action="ccurl">
> ...

thank you for yr help, Bryce.. ok now I have:
form tag:

     <FORM method="GET" action="ccurl">

I created a web.xml file in C:\tomcat\webapps\basic-servlet\WEB-INF
(it says in http://faq.javaranch.com/view?InvokerServlet, pointed out to
me by another responder to my post, to create a web.xml file in 'your
web-application's WEB-INF/ directory..')
here's entire text of web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <servlet>
    <servlet-name>ccServlet</servlet-name>
    <servlet-class>cc</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ccServlet</servlet-name>
    <url-pattern>/ccurl</url-pattern>
  </servlet-mapping>
</web-app>

but when I try to run I still get 404, and see this address in browser
locaton bar:

      C:\tomcat\webapps\basic-servlet\ccurl

class is now here, just like you say:
C:\tomcat\webapps\basic-servlet\WEB-INF\classes\cc.class

HTML form is here:
C:\tomcat\webapps\basic-servlet\cform.html

web.xml is here:
C:\tomcat\webapps\basic-servlet\WEB-INF\web.xml

( do I need to put full name of servlet where you put 'ccServlet'?
well, I tried that, it also wouldn't work..  why do we need to refer to
servlet (.java) at all once we've compiled and are just running the
class?)  again, thank you very much for your help..  Frances

 tried
Oscar kind - 27 Oct 2004 18:25 GMT
<cut: structure and configuration of a web application>

> but when I try to run I still get 404, and see this address in browser
> locaton bar:
>
>       C:\tomcat\webapps\basic-servlet\ccurl

Yes: that location doesn't exist on the filesystem. A web application is
meant to be accessed from the web. That means a server, not the
filesystem.

So assuming tomcat is running on port 8080, use this URL in your browser:

    http://localhost:80880/basic-servlet/ccurl

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2

Frances Del Rio - 27 Oct 2004 19:27 GMT
> <cut: structure and configuration of a web application>
>
[quoted text clipped - 10 lines]
>
>     http://localhost:80880/basic-servlet/ccurl

thank you Oscar...  how do I acess it like this from HTML from tag?  at
any rate I just typed what you sent into browser and got following
errors (better than 404 I was getting earlier):

- - - - - - - -

The server encountered an internal error () that prevented it from
fulfilling this request.

exception

java.lang.NullPointerException
    java.text.DecimalFormat.parse(DecimalFormat.java:1045)
    java.text.NumberFormat.parse(NumberFormat.java:312)
    cc.doGet(cc.java:28)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache
Tomcat/5.0.27 logs.

- - - - - - - - -

what errors are these?  this compiled fine..  full text of .java servlet:
http://www.francesdelrio.com/cc.html

I looked at tomcat logs, it doesn't really tell me much..  again, thank
you very much..  Frances
Bryce - 27 Oct 2004 19:35 GMT
>thank you Oscar...  how do I acess it like this from HTML from tag?  at
>any rate I just typed what you sent into browser and got following
[quoted text clipped - 16 lines]
>note The full stack trace of the root cause is available in the Apache
>Tomcat/5.0.27 logs.

If you got this, then you are making progress. That means that your
servlet class is being called correctly.

>- - - - - - - - -
>
>what errors are these?  this compiled fine..  

Because those are run time exceptions. Exceptions that can only be
discovered when the program is running, In the case above, it appears
that some variable on line 28 of your source code is accessing an
object that is null. Couldn't tell what line from the code below, as
line 28 didn't match anything, but I guess it is the line:

double poundValue = nf.parse(amount).doubleValue();

Probably, amount is null. Especially if you were just calling it
without the parameter. Try:

http://localhost:8080/basic-servlet/ccurl?amount=55

> full text of .java servlet:
>http://www.francesdelrio.com/cc.html
>
>I looked at tomcat logs, it doesn't really tell me much..  again, thank
>you very much..  Frances

--
now with more cowbell
Frances Del Rio - 27 Oct 2004 22:12 GMT
>>thank you Oscar...  how do I acess it like this from HTML from tag?  at
>>any rate I just typed what you sent into browser and got following
[quoted text clipped - 36 lines]
>
> http://localhost:8080/basic-servlet/ccurl?amount=55

ok, Bryce, I think I'm getting somewhere here..  this is what shows up
on browser when I hit url you posted:

             $55 <pound sign>33.71

so this means what?  I need to deal w/a variable here not being properly
init'd or something right??  will do, will do......  :)

regarding the concept of CLASSPATH, which you mention in your other
post, yes, of course I need to learn more about this (about a lot of
things..), but well, what can I say, I'm so eager to get going here  ;)
(and you know, after I figure all this out I have to figure it out for
my webhosing, on unix/tomcat..
(CLASSPATH:.:/usr/local/jsdk-1.3.1/lib/j2ee.jar) they only have 1.3, can
you believe that?  I can't run this particular servlet on there b/c it
contains a class (currency) which is new since 1.4.. I mean I love it..)
 I will to modify this servlet so it uses a diff. class, but that's a
pain b/c entire book follows this particular servelt.. oh well..  I'll
cross that bridge when I get to it..  what are the '.:' at the beginning
of that path?)

basically CLASSPATH is the path to the classes for the servlet right??
the question I have right now about that is:

when I compile/run my little .java apps in my own env (no tomcat, just
compiling/running on DOS shell...) path for classes for that (system
API, right?) is set in env var's under 'system' in C'panel (I'm on
Windows 2000) so: why can't I add path to c:\tomcat\...\servlet-api.jar
(this is what it's called in my version of tomcat) in there??  this is
main question I have right now...
I have to type entire path every time to compile servlets (but that's
ok, b/c I figured out how to do it w/batch files..  I think batch files
are cool, I don't know them well yet, but I have a feeling you can do a
lot w/them..  but still would like to know answer to my question..)

ok, have a lot to study...  man......  the day I manage to successfully
compile and run a servlet both at home and at my webhosting I'll.......
 hmmm.. better not say publicly.........  :)  Bryce, many thanks for
your help..  Frances
Bryce - 27 Oct 2004 23:17 GMT
>> Because those are run time exceptions. Exceptions that can only be
>> discovered when the program is running, In the case above, it appears
[quoted text clipped - 16 lines]
>so this means what?  I need to deal w/a variable here not being properly
>init'd or something right??  will do, will do......  :)

Because, you have this in your code:
String amount = req.getParameter("amount");

What this basically says is, give me the parameter named "amount" that
was passed in the query string when I was called.

Lets dissect the url above:

http://localhost:8080/basic-servlet/ccurl?amount=55

Everything after the ? is the query string. It is broken into
name=value pairs. In this instance, we have only one:
amount = 55.

So, when you call: req.getParameter("amount") you get "55" returned.

Now, if you had called the servlet without the query string, or a
query string that didn't have a parameter "amount", then the function:
req.getParameter("amount");

would return null, i.e., nothing. Does this make sense?

Now, the reason you were getting the null pointer exception is that
you were trying to use this on the line:

double poundValue = nf.parse(amount).doubleValue();

since amount is null, this method bombs. You might want to do some
value checking, such as seeing if its null, etc. Maybe sent amount to
0 if the parameter is null.

>regarding the concept of CLASSPATH, which you mention in your other
>post, yes, of course I need to learn more about this (about a lot of
>things..), but well, what can I say, I'm so eager to get going here  ;)

I know how that feels. But sometimes you need to learn to crawl before
you walk. You wouldn't go skydiving without first knowing how to
deploy your chute would you?

>(and you know, after I figure all this out I have to figure it out for
>my webhosing, on unix/tomcat..
>(CLASSPATH:.:/usr/local/jsdk-1.3.1/lib/j2ee.jar) they only have 1.3, can
>you believe that?  I can't run this particular servlet on there b/c it
>contains a class (currency) which is new since 1.4.. I mean I love it..)

Ok.. Tomcat is its own environment, and this is a concept you really
need to understand. As we mentioned before in a previous thread,
Tomcat sets its own classpath. That's why you need to include the
servlet libraries when you are compiling your servlet, but don't need
to worry about it when you deploy it to Tomcat. Because Tomcat already
has it set in its CLASSPATH.

There are some other special cases for Tomcat. In each webapp, the
folders:
WEB-INF/classes
WEB-INF/lib

are special.

the classes folder above is included in your apps classpath, and all
jars in the lib folder as well.

>  I will to modify this servlet so it uses a diff. class, but that's a
>pain b/c entire book follows this particular servelt.. oh well..  I'll
>cross that bridge when I get to it..  what are the '.:' at the beginning
>of that path?)

Don't know what you mean here.

>basically CLASSPATH is the path to the classes for the servlet right??
>the question I have right now about that is:

Yea, but see above. When deploying to Tomcat, you only have to worry
about putting your stuff in the folders I described above (there are
some special cases, but for simplicity sake, we won't discuss them
here).

>when I compile/run my little .java apps in my own env (no tomcat, just
>compiling/running on DOS shell...) path for classes for that (system
>API, right?) is set in env var's under 'system' in C'panel (I'm on
>Windows 2000) so: why can't I add path to c:\tomcat\...\servlet-api.jar
>(this is what it's called in my version of tomcat) in there??  this is
>main question I have right now...

Because... Path is not the same as classpath.
Repeat after me:

PATH is an OS concept, allowing it to find executables.
CLASSPATH is a Java concept. Classpath is the path Java uses to find
libraries.

The two server entirely different purposes, even though they are
similar in concept.

Just remember, in Tomcat, you should never have to set a classpath as
long as you put your classes in the folders described above.

>I have to type entire path every time to compile servlets (but that's
>ok, b/c I figured out how to do it w/batch files..  I think batch files
>are cool, I don't know them well yet, but I have a feeling you can do a
>lot w/them..  but still would like to know answer to my question..)

Yes. But I prefer using Ant, as its geared more toward Java and its
concepts.

>ok, have a lot to study...  man......  the day I manage to successfully
>compile and run a servlet both at home and at my webhosting I'll.......
>  hmmm.. better not say publicly.........  :)  Bryce, many thanks for
>your help..  Frances

--
now with more cowbell
Frances Del Rio - 28 Oct 2004 04:15 GMT
Bryce, you won't believe this..  oh my gosh.......  do you know why it
wasn't working? you WILL kill me..  instead of accessing the HTML for
this servlet like this, http://localhost/basic-servlet/cform.html I was
typing file path in the browser.. I was typing this in browser..

C:\tomcat\webapps\basic-servlet\cform.html
    (I mean I was going into this dir and opening file right from there
by clicking on it..)   all of a sudden I went wait, why does it work if
I put value in query string and not pass it thru the form???  so from
that url w/the localhost all the way to values passed I went back to
form then hit same value (55) in form and pressed submit and voila --
there it was...  so I realize now what you say about those errors I got:
I got them b/c no values were passed to servlet (if I just hit submit in
form w/o passing a value I get just this phrase in browser: 'wrong
number format'...)

I feel like a total jerk...  this should have occurred to me a lot
sooner... if I type file path in browser it doesn't go thru the server,
right??  or something like that...  well, what happens is that I still
don't understand what localhost is...  how do you get to where you get
just by typing localhost when there's no dir or anything called
localhost?  (in my case it's just localhost, no no. after it..)
(what are ports?  only ports I know are external ports on yr machine
where you plug in external devices like printers, etc...)

PATH and CLASSPATH..  yes, everybody tells me they are two different
things, yet........  for APIs for just local SDK/JRE you use PATH and
for APIs for servlets you have to use a different kind of path, they are
both paths to java classes..  (yes Path is an OS concept, but still we
use it for java classes, right??)

the .: that I mentioned is in CLASSPATH config instr at my webhosting..
http://www.panix.com/web/faq/tomcat-user.html/

they say to set CLASSPATH to the following:

    .:/usr/local/jsdk-1.3.1/lib/j2ee.jar

I tried it w/o the .: and it also didn't work.. (over there I get
can't-resolve-symbol errors for HttpServletRequest, etc..   i.e.,
servlet APIs are not being read.. then I read on and realized I need to
do permissions on unix, and well, lots of things...)

thanks for explaining about

WEB-INF/classes
WEB-INF/lib

they also talk about this, say where to put stuff...
so I gather what you said is:  WEB-INF/lib  is where servlet APIs are
and WEB-INF/classes is where you put the classes that are created when
you compile...  so then the tricky thing is figuring out path from form
to servlet (do my HTML forms that call servlets have to be inside tomcat
dir?)   Bryce, thank you very very much for your help, you really have
helped a lot.....   this post got too long, sorry..    Frances
Bryce - 28 Oct 2004 15:31 GMT
>Bryce, you won't believe this..  oh my gosh.......  do you know why it
>wasn't working? you WILL kill me..  instead of accessing the HTML for
>this servlet like this, http://localhost/basic-servlet/cform.html I was
>typing file path in the browser.. I was typing this in browser..
>
>C:\tomcat\webapps\basic-servlet\cform.html

hehe.. Yea. that would do it.

>PATH and CLASSPATH..  yes, everybody tells me they are two different
>things, yet........  for APIs for just local SDK/JRE you use PATH and
>for APIs for servlets you have to use a different kind of path, they are
>both paths to java classes..  (yes Path is an OS concept, but still we
>use it for java classes, right??)

Nope. PATH is not used for java classes at all. Java doesn't use the
PATH variable at

>the .: that I mentioned is in CLASSPATH config instr at my webhosting..
>http://www.panix.com/web/faq/tomcat-user.html/
[quoted text clipped - 7 lines]
>servlet APIs are not being read.. then I read on and realized I need to
>do permissions on unix, and well, lots of things...)

Yes. Permissions on Unix is a whole seperate discussion.

>thanks for explaining about
>
[quoted text clipped - 8 lines]
>dir?)   Bryce, thank you very very much for your help, you really have
>helped a lot.....   this post got too long, sorry..    Frances

no problemo

--
now with more cowbell
Frances Del Rio - 28 Oct 2004 17:30 GMT
>>PATH and CLASSPATH..  yes, everybody tells me they are two different
>>things, yet........  for APIs for just local SDK/JRE you use PATH and
[quoted text clipped - 4 lines]
> Nope. PATH is not used for java classes at all. Java doesn't use the
> PATH variable at

well then how come for java apps that I run on DOS shell I have to set
path thus:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;
C:\java\bin;C:\tomcat\bin
^^^^^^^^^^^
this is for the java classes, right??  there's also a tomcat path
there, so........  ok, still confused about this.. what can I say....

again, many many thanks for all your help...  Frances
Juha Laiho - 28 Oct 2004 19:11 GMT
Frances Del Rio <fdr58@yahoo.com> said:
>>>PATH and CLASSPATH..  yes, everybody tells me they are two different
>>>things, yet........  for APIs for just local SDK/JRE you use PATH and
[quoted text clipped - 13 lines]
>this is for the java classes, right??  there's also a tomcat path
>there, so........  ok, still confused about this.. what can I say....

PATH is to get your command interpreter to find the parts it needs to
perform your commands. So, f.ex. when you type 'java', there needs to
be JAVA.EXE (ok, or JAVA.COM or JAVA.BAT or ...) available somewhere.
Windows will search the directories listed in PATH to find these.
Similarly for Tomcat; when you look at the tomcat\bin directory, you'll
find batch files used to start the actual Tomcat server.

So everything to do with your operating environment and nothing to do
specifically with Java. It's just that all too many Windows apps
tend to stuff their executables into %SystemRoot%\system32, thus making
it hard to distinguish which files are OS components and which belong
to applications, that makes it so that you seldom need to consider the
PATH issues. Be thankful that Java and Tomcat install rather courteously
into their own, designated directories.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Garbled time

Bryce - 28 Oct 2004 19:35 GMT
>>>PATH and CLASSPATH..  yes, everybody tells me they are two different
>>>things, yet........  for APIs for just local SDK/JRE you use PATH and
[quoted text clipped - 11 lines]
>C:\java\bin;C:\tomcat\bin
>^^^^^^^^^^^

NO NO NO NO NO.

That entry is not used at ALL for Java. If you took that entry out,
you could just as easily type at your command line:

c:\java\bin\javac ....

What having that entry in your PATH environment variable is it allows
you to type at your command line:

javac ...

and WINDOWS will search along your path to find a file called
javac.exe, given the path above, it will find it at C:\java\bin and
execute that one.

Same thing with the tomcat entry.

Java uses something entirely different to find the java classes. That
being the CLASSPATH.

>this is for the java classes, right??  there's also a tomcat path
>there, so........  ok, still confused about this.. what can I say....
>
>again, many many thanks for all your help...  Frances

--
now with more cowbell
Frances Del Rio - 29 Oct 2004 14:19 GMT
>>>>PATH and CLASSPATH..  yes, everybody tells me they are two different
>>>>things, yet........  for APIs for just local SDK/JRE you use PATH and
[quoted text clipped - 32 lines]
> Java uses something entirely different to find the java classes. That
> being the CLASSPATH.

Bryce, thank you for yr response...  NOW finally I'm understanding this
better...  so what these paths do is simply search for the executable
that runs, in java's case, the compiler (javac.exe) and, in tomcat's
case (I just looked in tomcat\bin), tomcat5.exe (there's also a
tomcat5w.exe, what's that??)  what are all the other .exe's in java\bin?
 it's full of them.. java.exe, javah.exe, jdb.exe, keytool.exe,
klist.exe, and many others.. only one I recognize is appletviewer.exe,
there's a packager.exe with a cup-of-coffee icon..)    Bryce, thank you
again, yr help is very much appreciated...  Frances
Bryce - 29 Oct 2004 16:22 GMT
>Bryce, thank you for yr response...  NOW finally I'm understanding this
>better...  so what these paths do is simply search for the executable
[quoted text clipped - 5 lines]
>there's a packager.exe with a cup-of-coffee icon..)    Bryce, thank you
>again, yr help is very much appreciated...  Frances

No problemo.

Lots of exe's in java/bin.
java.exe and javac.exe are, obviously, the compiler and the one to run
your java programs. I only know what some of the others are:

jdb.exe - Java command line debugger
keytool.exe - manages public and private keys (cryptography)

Not sure what most of them are. java.exe and javac.exe are the most
used.

--
now with more cowbell
Juha Laiho - 31 Oct 2004 21:49 GMT
Frances Del Rio <fdr58@yahoo.com> said:
>Bryce, thank you for yr response...  NOW finally I'm understanding this
>better...  so what these paths do is simply search for the executable
[quoted text clipped - 5 lines]
>there's a packager.exe with a cup-of-coffee icon..)    Bryce, thank you
>again, yr help is very much appreciated...  Frances

How about reading the docs (for what the different exeutables do; for
significance of PATH, try a book on Windows, perhaps one with bias
on administering a Windows system)?

http://java.sun.com/j2se/1.4.2/docs/tooldocs/tools.html

Quote from the above page:
:    The  sections  below  provide a brief introduction to the tools in the
:    Java 2 SDK, with links to their reference pages:
[quoted text clipped - 8 lines]
:      * Java IDL and RMI-IIOP Tools (tnameserv, idlj, orbd, servertool)
:      * Java Plug-inTM Tools

... seems to cover pretty much of what is in the java/bin directory.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

Juha Laiho - 28 Oct 2004 19:04 GMT
Frances Del Rio <fdr58@yahoo.com> said:
>WEB-INF/classes
>WEB-INF/lib
[quoted text clipped - 3 lines]
>and WEB-INF/classes is where you put the classes that are created when
>you compile...

If you deploy your application as a bunch of *.class files, you put the
files in WEB-INF/classes/.

If you deploy your application (or parts of it) as *.jar files (created
with 'jar' command), you put the files in WEB-INF/lib/ .

>(do my HTML forms that call servlets have to be inside tomcat dir?

Yes, in the same directory where you have the WEB-INF directory
(your application base directory)

>so then the tricky thing is figuring out path from form to servlet

Well, not so tricky. You know the path for the form (based on where in
the hierarchy below your application base directory you placed the html
form). You also know the path for your servlet (beause you wrote the
path into your web.xml).
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

Frances Del Rio - 29 Oct 2004 16:52 GMT
> Frances Del Rio <fdr58@yahoo.com> said:
>
[quoted text clipped - 9 lines]
> form). You also know the path for your servlet (beause you wrote the
> path into your web.xml).

well, for me it IS tricky.. consider pls: my form is at
C:\tomcat\webapps\basic-servlet\cform.html

class is here:
C:\tomcat\webapps\basic-servlet\WEB-INF\classes\cc.class

so why is path from form to class
'servlet/cc' ?????

I did the web.xml for path from form to class, that Bryce showed me, but
then after I had solved problems w/this servlet I realized I don't need
that web.xml file, that it works fine if I put this in form tag:

 <FORM method="GET" action="servlet/cc">

I just need to know how you get from 'servlet/cc' to

WEB-INF\classes\cc.class

thank you.. and thank you very much to everyone who has helped me out
here.. you guys on this ng rock!!

Frances   :)
Bryce - 29 Oct 2004 21:11 GMT
>I just need to know how you get from 'servlet/cc' to
>
>WEB-INF\classes\cc.class

See the earlier posts about the Invoker Servlet. It depends on the
Invoker Servlet being enabled.

Enabling it by:
   <servlet-mapping>
    <servlet-name>invoker</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
   </servlet-mapping>

This says that any request that asks for "servlet/*" (or in your case,
servlet/cc will match), route the request to the invoker servlet.

Where's the invoker servlet you ask? Check your Tomcat's conf/web.xml
file. You'll find the following:

    <servlet>
       <servlet-name>invoker</servlet-name>
       <servlet-class>
         org.apache.catalina.servlets.InvokerServlet
       </servlet-class>
       <init-param>
           <param-name>debug</param-name>
           <param-value>0</param-value>
       </init-param>
       <load-on-startup>2</load-on-startup>
   </servlet>

Read the comments there for a decent description of the invoker
servlet. Basically, if you are calling "servlet/cc" it will look for
"cc.class" in WEB-INF/classes or in a jar file in WEB-INF/lib.

But, after saying all that, I say you need to get a different Tomcat
book. Using the Invoker servlet is generally not a good idea.

According to Jakarta RELEASE NOTES:

Using the invoker servlet in a production environment is not
recommended and is unsupported.

http://jakarta.apache.org/tomcat/faq/misc.html#invoker

Forget everything you've heard about it. Here's how you map a servlet
class to an incoming URL:

Given your class cc.class, and located in WEB-INF/classes:

In your WEB-INF/web.xml, you define  your servlet:

<web-app>
 <servlet>
   <servlet-name>MyServlet</servlet-name> [1]
   <servlet-class>cc</servlet-class>  [2]
 </servlet>
 <servlet-mapping>
   <servlet-name>MyServlet</servlet-name> [3]
   <url-pattern>/myServlet</url-pattern>  [4]
 </servlet-mapping>
</web-app>

[1] Here we assign a name to our servlet. It can be anything you want.
Its this name that we will use later to refer to this servlet.

[2] Here we bind our actual servlet class, cc.class to the servlet
name.

[3] Here we are defining a servlet mapping. note that the servlet name
matches the servlet name we defined in [1] above.

[4] url pattern. Tomcat will use this to call the appropriate servlet
given the url-pattern. In this case, we've specified /myServlet. This
means, that any URL with /myServlet at the end will call cc.

http://localhost:8080/myapp/myServlet

The URL above will call our servlet, assuming its contained in a
webapp with a context name of "myapp".

--
now with more cowbell
Bryce - 29 Oct 2004 21:16 GMT
>I just need to know how you get from 'servlet/cc' to
>
>WEB-INF\classes\cc.class

oh, and this is a good URL that explains the whole invoker servlet
stuff:

http://faq.javaranch.com/view?InvokerServlet

--
now with more cowbell
Frances Del Rio - 29 Oct 2004 21:54 GMT
>>I just need to know how you get from 'servlet/cc' to
>>
[quoted text clipped - 4 lines]
>
> http://faq.javaranch.com/view?InvokerServlet

thank you for yr response, Bryce...  I see that this is specified in
web.xml here: C:\tomcat\conf...  I hadn't realized that..  I'm saving
all yr posts and will definitely be reading material you suggest..

I have just tried deplying my first jsp, and it won't run! I don't
believe it, jsp's is a much simpler thing than servlets...  I didn't
think I would have a hard time here..  but oh well, always something..
(get a 404..  but DID type right url this time..) it's a simple jsp that
displays the date..  he says to put it here:

C:\tomcat\webapps\mydate\WEB-INF

I try to access it in browser by typing:

http://localhost/mydate/date.jsp

and get a 404..

code of date.jsp:

<HTML>
<HEAD>
<TITLE> Date JSP </TITLE>
</head>
<body>
Today's date is <%= new java.util.Date() %>
</BODY>
</HTML>

(I did shut down and restart Tomcat after creating this file and putting
it in right location..) can't understand what could possibly be wrong
here (jsp examples provided w/tomcat work fine..)

again, many thanks for all yr help...  Frances
Bryce - 29 Oct 2004 22:17 GMT
>>>I just need to know how you get from 'servlet/cc' to
>>>
[quoted text clipped - 16 lines]
>
>C:\tomcat\webapps\mydate\WEB-INF

No. Don't put it there. Put it in the root (anything in WEB-INF is
protected, and you can't access it by just typing.

Put it in your webapp's root:

c:\tomcat\webapps\mydate

>I try to access it in browser by typing:
>
>http://localhost/mydate/date.jsp

Is tomcat running on port 80? Or 8080? If you didn't change the
default, its 8080, so you'd need to use:

http://localhost:8080/mydate/date.jsp

>and get a 404..
>
[quoted text clipped - 14 lines]
>
>again, many thanks for all yr help...  Frances

--
now with more cowbell
Frances Del Rio - 29 Oct 2004 23:35 GMT
>>>>I just need to know how you get from 'servlet/cc' to
>>>>
[quoted text clipped - 23 lines]
>
> c:\tomcat\webapps\mydate

you're right Bryce, that worked...  I was going to say the guy messed
up, but he didn't, he just wasn't very clear...  don't ask me why, but
in my case it's just locahost, no no. after it...  YOU tell me why...
;)... url for this jsp is http://localhost/mydate/date.jsp  and it works...

I'm home now.. I copied entire folder where I have servlet to same place
in tomcat at home and it's not running.. oh well, have to figure that
one out..  btw, the guy says every time you deploy a jsp in tomcat you
have to shut down tomcat and turn it on again so it recognizes the jsp,
is there a better way?  (I wonder how that works up on my webhosting, I
doubt you can turn tomcat off and on all the time up on their server..)
 again, Bryce, your help has been priceless.. thank you so much..  Frances
Bryce Fischer - 30 Oct 2004 02:52 GMT
>> Put it in your webapp's root:
>>
[quoted text clipped - 4 lines]
>in my case it's just locahost, no no. after it...  YOU tell me why...
>;)... url for this jsp is http://localhost/mydate/date.jsp  and it works...

Don't know why. Have to check in your conf/server.xml

>I'm home now.. I copied entire folder where I have servlet to same place
>in tomcat at home and it's not running.. oh well, have to figure that
[quoted text clipped - 3 lines]
>doubt you can turn tomcat off and on all the time up on their server..)
>  again, Bryce, your help has been priceless.. thank you so much..  Frances

Servlets, usually. JSPs, no. You don't need to restart Tomcat for
JSPs.
Andrew Thompson - 27 Oct 2004 14:50 GMT
> this is on my machine locally on tomcat..
>
> HTML form and servlet are here:
>
> C:\tomcat\webapps\my-servlet\cform.html
> C:\tomcat\webapps\my-servlet\cc.java

Should be..
C:\tomcat\webapps\WEB-INF\classes\servlet\cc.java

The first line of cc.java (are you sure this class
is called 'cc'?) would be..

package servlet;

> I'm following a tomcat book ("Tomcat" by Martin Bond), he says after
> compiling put the class here:
>
> C:\tomcat\webapps\my-servlet\WEB-INF\classes\cc.class

I'd guess he meant..
C:\tomcat\webapps\my-servlet\WEB-INF\classes\servlet\cc.class

> the weirdest thing is that in the book he has the form tag thus:
>
>      <FORM method="GET" action="servlet/cc">

Which would indicate a class in the 'servlet' package
of any of the jar's or ZIP's in WEB-INF\lib\ or a
class in WEB-INF\classes\servlet\

> where does he get this 'servlet' dir from??
>
[quoted text clipped - 6 lines]
> bar in browser.. I'm a bit baffled here... would appreciate any
> suggestions..  

Try to get an understanding of some of the basics before
you try tackling web based projects.  You apparently have a
poor understanding of packages and the directory structures
you need to use when dealing with them.  This is even more
complicated when you are developing and debugging a web-app.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Frances Del Rio - 27 Oct 2004 16:26 GMT
>>this is on my machine locally on tomcat..
>>
[quoted text clipped - 5 lines]
> Should be..
> C:\tomcat\webapps\WEB-INF\classes\servlet\cc.java

really?  well, that's not what he said, maybe this book is no good..
(does it matter where .java is??  I mean after you compile all you run
is the class, right?  and need proper path from HTML form to class (and
put class in proper spot, of course..)  right??...  or wrong???)

> The first line of cc.java (are you sure this class
> is called 'cc'?) would be.
yes, class is called 'cc' -- app is called cc.java (is a currency
converter..)

> package servlet;

top lines:

import java.io.*;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;

(again, verbatim from book..)
entire code for servlet:
http://www.francesdelrio.com/cc.html

>>I'm following a tomcat book ("Tomcat" by Martin Bond), he says after
>>compiling put the class here:
[quoted text clipped - 3 lines]
> I'd guess he meant..
> C:\tomcat\webapps\my-servlet\WEB-INF\classes\servlet\cc.class

well, again, if that's what he meant it's not what he said.. (do I need
to ditch this book?  ;)...)
again, here are exact paths where he says to put everthing:

C:\tomcat\webapps\my-servlet\cform.html
C:\tomcat\webapps\my-servlet\cc.java
C:\tomcat\webapps\my-servlet\WEB-INF\classes\cc.class

(nothing about a dir called 'servlet' anywhere in there...)

actually 'my-servlet' he said to call it 'basic-servlet'.. I had changed
it to 'my-servlet' just for the heck of it...  does it make a diff? I
don't know..  so changed it back, so now these are the paths as he
specified them, EXACTLY:

C:\tomcat\webapps\basic-servlet\cform.html
C:\tomcat\webapps\basic-servlet\cc.java
C:\tomcat\webapps\basic-servlet\WEB-INF\classes\cc.class

I modified everything to your specifications..  but class still won't
run...  I get a 404, and this is address that appears on browser
location bar... C:\tomcat\webapps\basic-servlet\servlet\cc
(form tag: <FORM method="GET" action="servlet/cc"> )

if I put full path to class in form tag, same thing.. I see this url in
browser loc bar, but get a 404...
C:\tomcat\webapps\basic-servlet\WEB-INF\classes\servlet\cc
(form tag: <FORM method="GET" action="WEB-INF/classes/servlet/cc">)

Andrew, PLEASE explain this to me...  WHY doesn't it work if I put exact
full path to class in form tag??

> Try to get an understanding of some of the basics before
> you try tackling web based projects.  

I'm trying Andrew, I'm trying, I'm in the process of learning all this
stuff as we speak..  (and you know, after I succeed in running this
stuff locally I need to be able to run it up on my webhosting.. I guess
I need to brace myself for another battle..)

You apparently have a
> poor understanding of packages and the directory structures
> you need to use when dealing with them.  

well yes, as I said, I'm learning...  however much you can explain to me
about all this would be mostly appreciated, Andrew...  many thanks for
your help..  Frances
Andrew Thompson - 28 Oct 2004 04:40 GMT
...
>> I'd guess he meant..
>> C:\tomcat\webapps\my-servlet\WEB-INF\classes\servlet\cc.class
>
> well, again, if that's what he meant it's not what he said..

My bad.  Stick with Bryce/William on this one.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

William Brogden - 27 Oct 2004 14:58 GMT
> this is on my machine locally on tomcat..
>
[quoted text clipped - 7 lines]
>
> C:\tomcat\webapps\my-servlet\WEB-INF\classes\cc.class

You need a more up to date servlet book.

> the weirdest thing is that in the book he has the form tag thus:
>
>      <FORM method="GET" action="servlet/cc">
>
> where does he get this 'servlet' dir from??

That /servlet/ in the URL requires the "invoker" servlet - it
used to be on by default. Since mid Tomcat 4.1 it has been OFF by
default - the invoker was supposed to make it easy to experiment
with servlets but it has just caused confusion. See this FAQ at
javaranch

http://faq.javaranch.com/view?InvokerServlet

Be sure to put all classes used in servlets in a package - you will
avoid all sorts of confusing errors.

> I tried this,
>
[quoted text clipped - 4 lines]
> bar in browser.. I'm a bit baffled here... would appreciate any  
> suggestions..  thank you..  Frances

Signature

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

Frances Del Rio - 27 Oct 2004 16:38 GMT
>> this is on my machine locally on tomcat..
>>
[quoted text clipped - 9 lines]
>
> You need a more up to date servlet book.

ok, Andrew and William, I see ONE thing that's happening here.. book
covers Tomcat 4.0 and 4.1 -- I have Tomcat 5.0.27..  is this entire
source of this problem??  (man, this is crazy, still can't understand
why it won't work even if I put FULL path to class in form tag...)

> That /servlet/ in the URL requires the "invoker" servlet - it
> used to be on by default.

Please, what's "invoker" servlet??  (yes, will read FAQ you posted..)

many thanks for your help...  shall continue to struggle here....  :)

Frances
Bryce - 27 Oct 2004 19:42 GMT
>>> this is on my machine locally on tomcat..
>>>
[quoted text clipped - 14 lines]
>source of this problem??  (man, this is crazy, still can't understand
>why it won't work even if I put FULL path to class in form tag...)

Shouldn't matter between 4.0 and 4.1. Its the servlet API version
that's important, and 4.0 - 5.0 are similar in this respect.

It really sounds like you do not understand the concept of Classpath
yet. I highly suggest you understand that concept entirely before
getting more complex. Pickup a RECENT java book, I especially like
Java Head First for beginners:
http://www.oreilly.com/catalog/hfjava/

Next, once you understand the basic concepts you can move on to more
advanced topics like Servlets.

>> That /servlet/ in the URL requires the "invoker" servlet - it
>> used to be on by default.
>
>Please, what's "invoker" servlet??  (yes, will read FAQ you posted..)

Read the FAQ. It talks about it.

>many thanks for your help...  shall continue to struggle here....  :)
>
>Frances

--
now with more cowbell
William Brogden - 27 Oct 2004 21:35 GMT
>>> this is on my machine locally on tomcat..
>>>
[quoted text clipped - 13 lines]
> source of this problem??  (man, this is crazy, still can't understand  
> why it won't work even if I put FULL path to class in form tag...)

Tomcat 5 has the "invoker" turned off by default.
The interpretation of a URL into a call to a servlet goes through a
complex lookup using declarations in the web.xml file, so your use of
the FULL path does not help a thing.

>> That /servlet/ in the URL requires the "invoker" servlet - it
>> used to be on by default.
>
> Please, what's "invoker" servlet??  (yes, will read FAQ you posted..)

The "invoker" was a shortcut used in early servlet work to get around  
having
to define each servlet in web.xml - it took all requests that had  
"/servlet"
in the URL and tried to figure out the rest of the URL in terms of a  
package
and class - then passed the request on to that class.

The existance of the "invoker" and its use in early books (including mine,  
sigh)
has turned out to be a real disaster in terms of confusing beginners and
impeding true understanding of the elegant servlet API.

Bill

Signature

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

Juha Laiho - 28 Oct 2004 19:20 GMT
Frances Del Rio <fdr58@yahoo.com> said:
>Please, what's "invoker" servlet??  (yes, will read FAQ you posted..)

As William said, invoker is a servlet provided by the servlet container,
with specific behaviour. It was seen as a nice shortcut, until it was
realized to be (among other things) a security problem. At that time
it was disabled (so, starting with 4.1.10 or so), and I very much hope
to see it completely removed in some future version.

The security problem with the invoker servlet is that it allows an
external user to call servlets that were meant to be for application
internal use only, thus exposing application interfaces in very
unexpected ways. In addition to invoking servlets by a browser (or
other WWW client), servlets can also be invoked in a more direct way
by other servlets within the same application -- so there are very
real cases of servlets that do exist, but are not meant to be used by
WWW clients directly.

The current way (and the only one documented in the servlet
specification) is to expose servlets to HTTP world explicitly via
web.xml mappings; this avoids the unexpected exposures.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.