以下是一个简单的servlet程序,首先见立一个目录,在D:\Tomcat\webapps\下建立servletdemo,并添加一个子目录
WEB-INF文件夹
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
out.println("<BODY>");
out.println("<BIG>Hello World</BIG>");
out.println("</BODY></HTML>");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
在写一个部署的文件web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
HelloWorld
</servlet-name>
<servlet-class>
HelloWorld
</servlet-class>
</servlet>
<!--部署的第一种方式-->
<servlet-mapping>
<servlet-name>
HelloWorld
</servlet-name>
<url-pattern>
/HelloWorld
</url-pattern>
</servlet-mapping>
<!--方式2-->
<servlet-mapping>
<servlet-name>
HelloWorld
</servlet-name>
<url-pattern>
/HelloWorld.do
</url-pattern>
</servlet-mapping>
<!--方式3-->
<servlet-mapping>
<servlet-name>
HelloWorld
</servlet-name>
<url-pattern>
*.hello
</url-pattern>
</servlet-mapping>
<!--方式4-->
<servlet-mapping>
<servlet-name>
HelloWorld
</servlet-name>
<url-pattern>
/hello/*
</url-pattern>
</servlet-mapping>
</web-app>
这样你就可以在浏览器中用以下url打开
http://localhost:8080/servletdemo/HelloWorld
/servletdemo/HelloWorld
/hello.html
/well.hello
/fancy/meeting/you/here.hello
/hello/to/you
posted on 2006-08-12 10:32
fly 阅读(5582)
评论(0) 编辑 收藏 所属分类:
jsp学习