欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Servlet實現(xiàn)統(tǒng)計頁面訪問次數(shù)功能

 更新時間:2021年04月08日 11:25:15   作者:Putarmor  
這篇文章主要介紹了Servlet實現(xiàn)統(tǒng)計頁面訪問次數(shù)功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Servlet實現(xiàn)統(tǒng)計頁面訪問次數(shù)的具體代碼,供大家參考,具體內(nèi)容如下

實現(xiàn)思路:

1.新建一個CallServlet類繼承HttpServlet,重寫doGet()和doPost()方法;

2.在doPost方法中調(diào)用doGet()方法,在doGet()方法中實現(xiàn)統(tǒng)計網(wǎng)站被訪問次數(shù)的功能,用戶每請求一次servlet,使得訪問次數(shù)times加1;

3.獲取ServletContext,通過它的功能記住上一次訪問后的次數(shù)。

在web.xml中進行路由配置:

<!-- 頁面訪問次數(shù) -->
  <servlet>
    <servlet-name>call</servlet-name>
    //CallServlet為處理前后端交互的后端類
    <servlet-class>CallServlet</servlet-class>  
  </servlet>
  <servlet-mapping>
    <servlet-name>call</servlet-name>
    <url-pattern>/call</url-pattern>
</servlet-mapping>

CallServlet類:

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA
 * Details about unstoppable_t:
 * User: Administrator
 * Date: 2021-04-07
 * Time: 14:57
 */

//獲得網(wǎng)站被訪問的次數(shù)
public class CallServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        ServletContext context = getServletContext();
        Integer times = (Integer) context.getAttribute("times");
        if (times == null) {
            times = new Integer(1);
        } else {
            times = new Integer(times.intValue() + 1);
        }
        PrintWriter out= resp.getWriter();
        out.println("<html><head><title>");
        out.println("頁面訪問統(tǒng)計");
        out.println("</title></head><body>");
        out.println("當(dāng)前頁面被訪問了");
        out.println("<font color=red size=20>"+times+"</font>次");
        context.setAttribute("times",times);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}

前端展示結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論