资讯专栏INFORMATION COLUMN

网站计数器 web映射

mcterry / 2332人阅读

摘要:站点的网站计数器的操作下午定义全局变量获取计数器若文件存在则读取获取到文件的输入流对计数器内容进行加计数文件保存回文件流中输入对象获取文件路径定义对象进行加您是第映射文件保存在文件夹下永远无法访问此时需要修改文件即可达到映

站点的网站计数器的操作

<%@ 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" %>


    Title


<%
    // 定义全局变量
    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%>


web映射

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




  Archetype Created Web Application
  
    he
    /WEB-INF/he.jsp
  
  
    he
    /hello
  


直接更新,但不重启,发现不能加载.
此时.即Tomcat需要重写配置属性
此时访问
http://localhost:8080/hello
即可访问

config

config用于获得

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73699.html

相关文章

  • 提升网站页面速度的14条最佳实践(二)

    摘要:唯一的格式约束是该字符串必须用引号引起来。对于网站背后的服务器来说,不大相同。不过用户不必忍受整个页面的重新加载了,而且用户在等待时,仍然可以响应。 本篇是对《高性能网站建设指南》一书的后7种规则进行梳理。 八、使用外部Jacascript和css 1.如果需要呈现给用户的页面可以实现完整缓存,那么使用外部Jacascript和css会提高网站性能。 2.在使用使用外部Jacascr...

    rainyang 评论0 收藏0
  • 提升网站页面速度的14条最佳实践(二)

    摘要:唯一的格式约束是该字符串必须用引号引起来。对于网站背后的服务器来说,不大相同。不过用户不必忍受整个页面的重新加载了,而且用户在等待时,仍然可以响应。 本篇是对《高性能网站建设指南》一书的后7种规则进行梳理。 八、使用外部Jacascript和css 1.如果需要呈现给用户的页面可以实现完整缓存,那么使用外部Jacascript和css会提高网站性能。 2.在使用使用外部Jacascr...

    shengguo 评论0 收藏0

发表评论

0条评论

mcterry

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<