1. 在eclipe中开发如下HelloServlet(注先引入jsp-api.jar, servlet-api.jar,它们可以从tomcat安装目录下找到. e.g: D: omcat-6.0.35lib下找到):
1 package com.annie; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 12 public class HelloServlet extends HttpServlet { 13 public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ 14 PrintWriter out = response.getWriter(); 15 out.println("<html>"); 16 out.println("<font size=30 color=red>www.hello.org</font><br>"); 17 out.println("<marquee>" + new java.util.Date() + "<marquee>"); 18 out.println("<html>"); 19 } 20 }
将代码进行clean up , build.
2. 部署Servlet代码至tomcat中,找到tomcatat所在的目录,e.g: D: omcat-6.0.35webapps,新建子目录结构如下:HelloServletWEB-INFclassesservlet代码包结构,将HelloServelt.class从eclipse下project目录移动至 D: omcat-6.0.35webappsHelloServletWEB-INFclassescomannie
3. 新建 D: omcat-6.0.35webappsHelloServletWEB-INweb.xml,并注册HelloServlet,信息如下:
4.启动tomcat,观察当前tomcat服务器所占端口:
5. 打开浏览器,输入 http://localhost:8080/HelloServlet/annie , 验证页面内容: