以Hello World为例, 其基本步骤是:
1. 选择File->New->Project
2. 在对话框中选择Tomcat Project,然后next
3. 输入Project Name,这里可以写HelloWorld,继续next
4. 勾上can update server.xml file选项,finish,好了,一个Poject已经创建好了。
5. 打开new class 向导,在Name中填入HelloWorld,在Superclass中填入javax.servlet.http.HttpServlet,按下finish,好了,一个servlet就创建好了
6. 在HelloWorld.java中重载doGet方法,代码如下:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("Hello World!");
}
}
7.在WEB-INF目录下新建web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>servlet/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
8.Ctrl+S保存,右键HelloWorld project,选择Tomcat Project->update context to server.xml
注意:这里很重要,此步骤eclipse会自动修改了server.xml文件,添加HelloWorld项目。
9.在Eclipse中启动Tomcat
10.在用浏览器访问
http://localhost:8080/HelloWorld/servlet/HelloWorld
posted on 2006-05-25 04:14
SmileFace 阅读(1925)
评论(2) 编辑 收藏 所属分类:
Java-Studying