File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Tomcat . Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Tomcat . Help" Watch "Tomcat . Help" New topic

Tomcat . Help

fengzixuan zheng
Ranch Hand

Joined: Aug 02, 2001
Posts: 31
How can I run my servlet with Tomcat ?

I write a servlet and put it under the Tomcat/webapps directory. How can I ran the servlet?
//codes of
import javax.servlet.*;
import javax.servlet.http.*;
/** Very simplistic servlet.
* Part of tutorial on servlets and JSP that appears at
* 1999 Marty Hall; may be freely used or adapted.
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
//I try to make a web.xml file like this:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE web-app (View Source for full doctype...)>
- <web-app>
- <servlet>
//and I want to test it via http://localhost:8080/Hello/Hello
//but it can not be showed. Please help.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

There are 2 things you do in the web.xml file:
1. Associate a logical servlet name with a servlet class
2. Associate a URL with the logical servlet name.
You did 1, but not 2. Actually if you had done neither it might have worked (depending on the Tomcat version) by assuming that a servlet named "Hello" (Case sensitive) was mapped to Hello.class in the WEB-INF/classes directory. As it is, you're halfway between the implied way and the explicit way.

An IDE is no substitute for an Intelligent Developer.
fengzixuan zheng
Ranch Hand

Joined: Aug 02, 2001
Posts: 31
Thank you, Holloway
Can you tell me how to do the second point for I don't know. Give a example.
With regards.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

look here:

You'll see that they map 'snoop' to the SnoopServlet class.
Look below this for a tag <servlet-mapping>, which will map the 'snoop' name to an URL.

Those are the two tags you need to run a servlet. <servlet> and <servlet-mapping>
[This message has been edited by Mike Curwen (edited October 22, 2001).]
I agree. Here's the link:
subject: Tomcat . Help
jQuery in Action, 3rd edition