编译运行步骤:
1、将HelloWorldServlet.java编译成.class文件。
2、将HelloWorldServlet.class文件放到WEB-INF\classes\servlet目录下(servlet为自定义包目录)。
3、在WEB-INF\web.xml描述文件中,将的<web-app></web-app>标签中添加如下描述:
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>servlet.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
4、在浏览器中输入http://localhost:8080/helloworld即可。
HelloWorldServlet.java:
package servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
public void doPost(HttpServletRequest request , HttpServletResponse response)
throws IOException , ServletException{
doGet(request , response);
}
}
posted on 2009-05-27 22:16
天堂露珠 阅读(234)
评论(0) 编辑 收藏 所属分类:
HelloWorld