aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Tomcat . Help" Watch "Tomcat . Help" New topic
Author

Tomcat . Help

fengzixuan zheng
Ranch Hand

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

I write a servlet Hello.java and put it under the Tomcat/webapps directory. How can I ran the servlet?
//codes of Hello.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Very simplistic servlet.
* Part of tutorial on servlets and JSP that appears at
* http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
* 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>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
</web-app>
//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: 16140
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
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:
<tomcathome>/examples/WEB-INF/web.xml

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: http://aspose.com/file-tools
 
subject: Tomcat . Help