• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tomcat . Help

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
fengzixuan zheng
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Holloway
Can you tell me how to do the second point for I don't know. Give a example.
With regards.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic