zoukankan      html  css  js  c++  java
  • 11月14日学习日志

    今天学习了基于servlet的网页点击计数器。

    实例:

    package com.runoob.test;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    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 PageHitCounter
     */
    @WebServlet("/PageHitCounter")
    public class PageHitCounter extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private int hitCount; 
        
        public void init() 
        { 
            // 重置点击计数器
            hitCount = 0;
        } 
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            response.setContentType("text/html;charset=UTF-8");
            // 增加 hitCount 
            hitCount++; 
            PrintWriter out = response.getWriter();
            String title = "总点击量";
            String docType = "<!DOCTYPE html> \n";
            out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<h2 align=\"center\">" + hitCount + "</h2>\n" +
                "</body></html>");
        }
        
        public void destroy() 
        { 
            // 这一步是可选的,但是如果需要,您可以把 hitCount 的值写入到数据库
        } 
    
    }

    现在通过访问 http://localhost:8080/TomcatTest/PageHitCounter 来调用这个 Servlet。

  • 相关阅读:
    centos安装vim
    thrift学习之二----学习资料积累
    thrift学习之一-------介绍
    组合模式
    一致性哈希算法(consistent hashing)
    php配置php-fpm启动参数及配置详解
    error while loading shared libraries的解決方法
    数据结构之二叉树
    768、最多能完成排序的块(贪心算法)
    VS code 配置C++编译环境
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14160123.html
Copyright © 2011-2022 走看看