前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >网站计数器 web映射

网站计数器 web映射

原创
作者头像
mySoul
发布2019-03-15 19:01:26
6020
发布2019-03-15 19:01:26
举报
文章被收录于专栏:mySoulmySoul

站点的网站计数器的操作

代码语言:txt
复制
<%@ page import="java.math.BigInteger" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.Scanner" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.PrintStream" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.util.zip.InflaterOutputStream" %><%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-12
  Time: 下午10:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    // 定义全局变量
    BigInteger count = null;
%>
<%!
    // 获取计数器
    public BigInteger load(File file){
        BigInteger count = null;
        try{
            // 若文件存在则读取
            if(file.exists()){
                Scanner scanner = null;
                // 获取到文件的输入流
                scanner = new Scanner(new FileInputStream(file));
                // 对计数器内容进行加1
                if(scanner.hasNext()){
                    count = new BigInteger(scanner.next());
                }
                scanner.close();
            }else{
                count = new BigInteger("0");
                save(file, count);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }
    // 计数文件保存回文件
    public void save(File file, BigInteger count){
        try{
            PrintStream printStream = null;
            printStream = new PrintStream(new FileOutputStream(file));
            // 流中输入对象
            printStream.println(count);
            printStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
%>
<%
    // 获取文件路径
    String fileName = this.getServletContext().getRealPath("/") + "count.txt";
    // 定义file对象
    File file = new File(fileName);
    // 进行加
    if(session.isNew()) {
        synchronized (this) {
            count = load(file);
            System.out.println(count);
            count = count.add(new BigInteger("1"));
            save(file, count);
        }
    }
%>
您是第<%=count%>
</body>
</html>

web映射

文件保存在WEB-INF文件夹下,永远无法访问,此时,需要修改/WEB-INF/web.xml文件即可,达到映射的目的

修改xml文件如下

代码语言:txt
复制
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>he</servlet-name>
    <jsp-file>/WEB-INF/he.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>he</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

直接更新,但不重启,发现不能加载.

此时.即Tomcat需要重写配置属性

此时访问

http://localhost:8080/hello

即可访问

config

config用于获得

qrcode_for_gh_9901b36b3b0e_258.jpg
qrcode_for_gh_9901b36b3b0e_258.jpg

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • web映射
  • config
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档