JavaWeb程序设计任务教程(黑马程序员 传智播客)测一测 编写一个Servlet,实现统计网站被访问次数的功能
生活随笔
收集整理的這篇文章主要介紹了
JavaWeb程序设计任务教程(黑马程序员 传智播客)测一测 编写一个Servlet,实现统计网站被访问次数的功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
詳細解釋已在注釋中給出
package 統(tǒng)計網(wǎng)站被訪問次數(shù);import java.io.*;import javax.servlet.*; import javax.servlet.http.*;public class Main_Class extends HttpServlet{private static final long seriaVersionUid = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html; charset=GB2312"); //設置中文編碼,否則會亂碼ServletContext context = getServletContext(); //唯一接口,封裝了web應用的信息PrintWriter out = resp.getWriter(); //設置輸出函數(shù)Integer times = (Integer)context.getAttribute("times"); //記錄網(wǎng)站的訪問次數(shù)if(times == null) times = new Integer(1);else times = new Integer(times.intValue()+1);out.println("<html><head><title>"); //通過html中的編碼設置字體out.println("頁面訪問統(tǒng)計~"); //頁面標題out.println("</title></head><body>"); //頁面正文out.println("當前頁面被訪問過了");out.println("<font color = red size=20>"+times+"</font>次");// 設置屬性,將times保存到當前的上下文中context.setAttribute("times", times);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// TODO Auto-generated method stubsuper.doPost(req, resp);}}總結(jié)
以上是生活随笔為你收集整理的JavaWeb程序设计任务教程(黑马程序员 传智播客)测一测 编写一个Servlet,实现统计网站被访问次数的功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (最优解法)46行代码AC_HDU124
- 下一篇: 代码分析+原理图解——棋盘覆盖问题-分治