1. 程式人生 > >一個servlet例子-統計網站訪問量

一個servlet例子-統計網站訪問量

1.建立JavaWeb專案,名WebCount

2.修改web.xml,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>WebCount1</display-name>
  <servlet>
    <servlet-name>CounterServlet</servlet-name>
    <servlet-class>com.CounterServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CounterServlet</servlet-name>
    <url-pattern>/counter</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>counter</welcome-file>
  </welcome-file-list>

</web-app>、

3.新建名稱為CounterServlet的servlet類,在該類dopost()方法中實現統計訪問數目。

package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CounterServlet
 */
@WebServlet("/CounterServlet")
public class CounterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CounterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#destroy()
     */
    public void destroy() {
        super.destroy();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲得ServletContext物件
        ServletContext context=getServletContext();
        //從ServletContext中獲取計數器物件
        Integer count=(Integer) context.getAttribute("counter");
        if(count==null){//如果為空,則在ServletContext中設定一個計數器的屬性
            count=1;
            context.setAttribute("counter", count);
        }else{
            context.setAttribute("counter", count+1);
        }
        
        response.setContentType("text/html");//響應正文的MIME型別
        response.setCharacterEncoding("UTF-8");//響應的編碼格式
        PrintWriter out=response.getWriter();
//        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//        out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
//        out.println("<HTML>");
        out.println("<HEAD><TITLE>統計網站訪問次數</TITLE></HEAD>");
        out.println("<BODY>");
//        out.println("<h2><font color='gray'");
        out.println("您是第"+context.getAttribute("counter")+"位訪客!");
//        out.println("</font>");
//        out.println("</h2>");
        out.println("</BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

}

4.將專案部署到伺服器,啟動伺服器。結果如下。