1. 程式人生 > >JSP學習筆記-03.2

JSP學習筆記-03.2

用JSP的指令碼標識寫一個網站瀏覽次數統計。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <%! class Counter{
  			int n = 0;
  }
  Counter counter = new Counter();
  %>
  <body>
  	<%  %>
    This is my JSP page. <% counter.n += 1; %><br>
    	頁面已訪問<%=counter.n %>次
  </body>
</html>

效果

頁面每重新整理一次,頁面訪問次數加一。

在這裡插入圖片描述