HelloServlet.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:1k
源码类别:
Java编程
开发平台:
Java
- package bible.servlets;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- public class HelloServlet extends HttpServlet {
- private int counter;
- public HelloServlet() {
- // Perform instantiation tasks here (rare).
- super();
- System.out.println("HelloServlet instantiated.");
- }
- public void init() throws ServletException {
- // Perform one-time initialization tasks here.
- counter = 0;
- System.out.println("HelloServlet initialized.");
- }
- public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
- // Do the servlet's work here.
- counter++;
- String userName = request.getParameter("username");
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.println("<html>");
- out.println("<head><title>HelloServlet</title></head>");
- out.println("<body>");
- out.println("<p>Hello, " + userName + "!</p>");
- out.println("This servlet has been invoked " + counter + " times.");
- out.println("</body></html>");
- System.out.println("HelloServlet.doGet() invoked.");
- }
- public void destroy() {
- // Perform cleanup tasks here.
- System.out.println("HelloServlet destroyed.");
- }
- }