servelt newbie question

M

maxwell

Linux Slackware 9.1, Tomcat 5.

I'm trying to run a simple servlet:

---------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Hello extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();

out.println("<HTML>");
out.println("HelloHelloHello");
out.println("</HTML>");
}
}
----------------------------

This compiles fine with "javac Hello.java" and gives me
an Hello.class file in $CATALINA_HOME/webapps/ROOT/WEB-INF/classes

My web.xml file is in $CATALINA_HOME/webapps/ROOT/WEB-INF and is:

------------------
<?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>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</web-app>
--------------------

Yet when I load http://127.0.0.1:8080/servlet/Hello I get
"requested resource /servlet/Hello is not available"

Any ideas as to what I'm doing wrong?

Many thanks for any info.

Max
 
W

Wendy S

maxwell said:
<?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>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</web-app>
Yet when I load http://127.0.0.1:8080/servlet/Hello I get
"requested resource /servlet/Hello is not available"

You appear to be missing the <servlet-mapping> tag. (And the closing
</servlet> tag as well, unless you didn't include your entire web.xml file.)

Every <tag> must have a closing </tag> unless it's empty: <tag />

The /servlet/ part of your URL refers (usually) to the Invoker Servlet,
which is disabled by default everywhere except the 'examples' webapp. Leave
it disabled.

Try closing your </servlet> and then adding:

<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>hello</url-pattern>
</servlet-mapping>

Then visiting:
http://127.0.0.1:8080/hello

Should work! Let us know...
 
T

Tony Morris

Try closing your said:
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

Then point your browser to http://<host>:<port>/app/hello
Better still, there are a zillion tutorials out there for this, and probably
one with Tomcat (I generally use different application servers).

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
R

Ryan Stewart

Tony Morris said:
Then point your browser to http://<host>:<port>/app/hello
Better still, there are a zillion tutorials out there for this, and probably
one with Tomcat (I generally use different application servers).
In case the OP missed it, because I sure did the first time through, and I
knew it needed changing: note the change that Tony made in the quoted
servlet-mapping above. Your url-pattern should always start with a "/".
 
W

Wendy S

Ryan Stewart said:
In case the OP missed it, because I sure did the first time through, and I
knew it needed changing: note the change that Tony made in the quoted
servlet-mapping above. Your url-pattern should always start with a "/".

Oops! Thanks for the correction.

But I'm not sure it's *always* the case that url-pattern should start with
/. The following works fine, and is what I copied from to make my
suggestion.

<servlet-mapping>
<servlet-name>csv</servlet-name>
<url-pattern>*.csv</url-pattern>
</servlet-mapping>

I answer these because it's SO frustrating to not be able to get your very
first Servlet to work. And 99% of the time it's the Invoker Servlet,
because all the early examples and books assume that it's enabled. :(

But in this case, I may not have been much help!
 
R

Ryan Stewart

Wendy S said:
Oops! Thanks for the correction.

But I'm not sure it's *always* the case that url-pattern should start with
/. The following works fine, and is what I copied from to make my
suggestion.

<servlet-mapping>
<servlet-name>csv</servlet-name>
<url-pattern>*.csv</url-pattern>
</servlet-mapping>

I answer these because it's SO frustrating to not be able to get your very
first Servlet to work. And 99% of the time it's the Invoker Servlet,
because all the early examples and books assume that it's enabled. :(

But in this case, I may not have been much help!
Okay, that's true, so aside from wildcards, your url-pattern should start
with "/". But that pattern is basically the same as /*.csv (I'd assume this
works) because something like http://localhost:8080blah won't work. You have
to have a "/" in the address and it gets included when the server looks at
the mappings. Unless there's a way to change that as well...
 
M

maxwell

Thank you very much.

I fixed my web.xml to include the following:

-------------------------------------------
<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/servlet/Hello</url-pattern>
</servlet-mapping>

</web-app>
--------------------------------------------

Works great. Now I'm ready to have fun! I appreciate the help.

One thing I noticed: Seems like I have to restart Tomcat everytime I
recompile the servlet. No big deal, I can write a simple bash script
to do this.

Max
 
S

Steve R. Burrus

Hi tony, I am steve Burrus, and I must admit/confess that I am just
barely more advanced in being able to deploy a servlet than the "Newbie"
is, with his posting!!

Listen, I noticed on the bottom of your post that u are some kind of a
software engineer at Sun Microsystems!! I was just wondering if you
could please possibly give me some quick advice on how a jsp/servlet is
deployed with these 2 other well-known application servers, namely the
"WebLogic Server" from BEA Systems, and the "Websphere Server" from the
IBM Corporation??!

You see, I have read, rather extensively I must say, the various
documentation for both of these application servers, and I still find
myself quite "confounded" as to how you get it going with using these
servers to effectively deploy a servlet, and even some of the other
components of the whole J2EE architecture. And also, in your anticipated
response back to me, could also "throw in" how ejb's are deployed to an
EJB Server? I have been most "stumped" about how to do this for a long
time now. Many thanx in advance for your help. (Incidentally, "newbie"
misspelled servlet as "servelt". Did ya notice that? :))
 
S

Steve R. Burrus

Maxwell, how do you go about writing this >>"simple bash script"<<
anyway because I have also had to always re-start Tomcat to get the
newly re-compiled servlet to get deployed!! Did you mean a "batch"
script OR a "bash" script as you said?

*************************************************************************************************
 
T

Tony Morris

But I'm not sure it's *always* the case that url-pattern should start with

That is likely only to be working "on your container".
I haven't found anything in the specification, but I suspect it is there.
And 99% of the time it's the Invoker Servlet,
That is definitely only working "on your container".
There is no such concept of an "Invoker Servlet" in the specification.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
M

maxwell

Steve R. Burrus said:
Maxwell, how do you go about writing this >>"simple bash script"<<

Steve,

I'm a Linux user, so for me it would be a "bash" script. Something
like

----cut here-------

#!/usr/bin/bash
/usr/local/jakarta-tomcat-5.0.18/bin/shutdown.sh
sleep 1
/usr/local/jakarta-tomcat-5.0.18/bin/startup.sh

----cut here-------

Call this script "rst" (for restart tomcat), then make
it executable with the command "chmod +x rst", put the script in the
directory
where you edit your servlets, then type the command "./rst" to
shutdown and restart tomcat.

Good luck,

Max.
 
S

Steve Burrus

Hi Maxwell, this is steve Burrus, and I noticed that you said/indicated
that you are using a Linux "box" for the bash script to re-start the
tomcat server with, but I am using Microsoft's Windows XP Professional
as my platform!!, :-( so how (if you happen to know at all!) do i go
about creating a BATCH script for my windows machine anyway???!
 
A

Andrew Thompson

"Steve Burrus" ...
Hi Maxwell, this is steve Burrus,

Hi Steve, this is Andrew..
...and I noticed that you said/indicated that you

...and I noticed your 'yellow on black' html
formatted message and thought WINDOZE
USER....
...are using a Linux "box" for the bash script to
re-start the tomcat server with, but I am using
Microsoft's Windows XP Professional as my platform!!

So am I!!!!

We poor unfortunate things (also using OE on
XP) had to suffer the 'hangover causer' of
your message, whereas any sensible newsreader
probably would have stripped your html
formatting straight out.

Could you look in the settings of your
OE and set it to only post to usenet in
plain text format? That would be
appreciated.
 
S

Steve Burrus

Okay Andrew, I will graciously take your point about how my email looks
to you, i.e., the bgcolor and text color and all that, but would you
please reciprocate and tell me how to write that batch script to get the
tomcat server to re-start after I have re-compiled a particular
servlet??? This means a lot to me!!

*************************************************************************************
 
K

KC Wong

Okay Andrew, I will graciously take your point about how my email looks
to you, i.e., the bgcolor and text color and all that, but would you
please reciprocate and tell me how to write that batch script to get the
tomcat server to re-start after I have re-compiled a particular
servlet??? This means a lot to me!!

On WinXP?

NET STOP "Apache Tomcat"
[Clear tomcat's temp files if you want]
NET START "Apache Tomcat"

Replace the name with the one of your Tomcat installation.
 
S

Steve Burrus

Yes, very definitely on wiNdows XP, specifically the "Professional"
flavor of it!!! And the code which you sent me for the batch script,
when I get it all written, where should I "stick" it anyway??, i.e.,
into which folder/directory in my system? And also, could you please
help me out with the whole package structure for tomcat? I would LOVE to
be able to see a servlet in a folder other than the usual
"C:\jakarta-tomcat-5.0.*\webapps\ROOT\WEB-INF\classes", and I always
specify the folder where I want the servlet class to go in the package
statement at the top of every servlet source file, but alas, I always
get the old 404 server error page whenever I put the servlet class file
into a folder other than the usual "....\ROOT\WEB-INF\classes" folder!!!
Can you please help me with this problem? Thanx.

*************************************************************************************************


KC said:
Okay Andrew, I will graciously take your point about how my email looks
to you, i.e., the bgcolor and text color and all that, but would you
please reciprocate and tell me how to write that batch script to get the
tomcat server to re-start after I have re-compiled a particular
servlet??? This means a lot to me!!

On WinXP?

NET STOP "Apache Tomcat"
[Clear tomcat's temp files if you want]
NET START "Apache Tomcat"

Replace the name with the one of your Tomcat installation.
 
S

Steve Burrus

Could someone/somebody please
help me out with the whole package structure for tomcat? I would LOVE to
be able to see a servlet in a folder other than the usual
"C:\jakarta-tomcat-5.0.*\webapps\ROOT\WEB-INF\classes", and I always
specify the folder where I want the servlet class to go in the package
statement at the top of every servlet source file, but alas, I always
get the old 404 server error page whenever I put the servlet class file
into a folder other than the usual "....\ROOT\WEB-INF\classes" folder!!!
Can you please help me with this problem? Thanx.

*************************************************************************************************
 
C

Chris Smith

Steve said:
Could someone/somebody please
help me out with the whole package structure for tomcat? I would LOVE to
be able to see a servlet in a folder other than the usual
"C:\jakarta-tomcat-5.0.*\webapps\ROOT\WEB-INF\classes", and I always
specify the folder where I want the servlet class to go in the package
statement at the top of every servlet source file, but alas, I always
get the old 404 server error page whenever I put the servlet class file
into a folder other than the usual "....\ROOT\WEB-INF\classes" folder!!!
Can you please help me with this problem? Thanx.

What does your servlet.xml file look like? What's the URL you're using
to access the servlet?

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

steve r. burrus

Could someone/somebody please
help me out with the whole package structure for tomcat? I would LOVE to
be able to see a servlet in a folder other than the usual
"C:\jakarta-tomcat-5.0.*\webapps\ROOT\WEB-INF\classes", and I always
specify the folder where I want the servlet class to go in the package
statement at the top of every servlet source file, but alas, I always
get the old 404 server error page whenever I put the servlet class file
into a folder other than the usual "....\ROOT\WEB-INF\classes" folder!!!
Can you please help me with this problem? Thanx.

*************************************************************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top