网站计数器 web映射

简介: 站点的网站计数器的操作<%@ page import="java.math.BigInteger" %><%@ page import="java.io.File" %><%@ page import="java.

站点的网站计数器的操作

<%@ 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文件如下

<!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用于获得

目录
相关文章
|
2月前
|
网络协议 Shell 网络安全
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
实验目的1.编译安装httpd2.优化路径3.并将鲜花网站上传到web服务器为网页目录4.在客户机访问网站http://www.bdqn.com
168 0
|
5月前
|
数据采集 Linux 数据库
Linux高级应用——web网站服务(2)
Linux高级应用——web网站服务(2)
47 0
|
3天前
|
网络协议 关系型数据库 Linux
使用PHPStudy搭建本地web网站并实现任意浏览器公网访问
使用PHPStudy搭建本地web网站并实现任意浏览器公网访问
9 0
|
4天前
|
应用服务中间件 nginx
如何在树莓派部署Nginx并实现无公网ip远程访问内网制作的web网站
如何在树莓派部署Nginx并实现无公网ip远程访问内网制作的web网站
8 0
|
12天前
|
机器学习/深度学习 前端开发 数据可视化
数据分析web可视化神器---streamlit框架,无需懂前端也能搭建出精美的web网站页面
数据分析web可视化神器---streamlit框架,无需懂前端也能搭建出精美的web网站页面
|
13天前
|
存储 Serverless 网络安全
Serverless 应用引擎产品使用之阿里云函数计算中的Web云函数可以抵抗网站对DDoS攻击如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
21 0
|
2月前
|
数据库
最全三大框架整合(使用映射)——struts.xml和web.xml配置
最全三大框架整合(使用映射)——数据库资源文件jdbc.properties
10 0
|
2月前
|
存储 编解码 前端开发
Star 15.4k!「Movie-web」一个非常简洁独特的电影网站开源项目
Star 15.4k!「Movie-web」一个非常简洁独特的电影网站开源项目
|
2月前
|
开发框架 前端开发 JavaScript
推荐5款热门的Web前端开发框架,助你快速构建优秀网站
推荐5款热门的Web前端开发框架,助你快速构建优秀网站
115 1
推荐5款热门的Web前端开发框架,助你快速构建优秀网站
|
2月前
|
Windows
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)
Windows Server 各版本搭建 Web 服务器实现访问本地 Web 网站(03~19)