• jsp里面实现asp.net的Global文件内容。


    Global.java文件:

     1 import javax.servlet.ServletContext;
     2 import javax.servlet.ServletContextEvent;
     3 import javax.servlet.ServletContextListener;
     4 import javax.servlet.ServletRequest;
     5 import javax.servlet.ServletRequestEvent;
     6 import javax.servlet.ServletRequestListener;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpSession;
     9 import javax.servlet.http.HttpSessionEvent;
    10 import javax.servlet.http.HttpSessionListener;
    11 
    12 public class Global implements ServletContextListener, HttpSessionListener,
    13         ServletRequestListener {
    14 
    15     @Override
    16     public void contextInitialized(ServletContextEvent sce) {
    17         // TODO Auto-generated method stub
    18         System.out.println("网站启动");
    19         ServletContext application = sce.getServletContext();
    20         application.setAttribute("count", 10);
    21     }
    22 
    23     @Override
    24     public void contextDestroyed(ServletContextEvent sce) {
    25         // TODO Auto-generated method stub
    26         ServletContext application = sce.getServletContext();
    27         application.setAttribute("count", 0);
    28         System.out.println("网站停止");
    29     }
    30 
    31     @Override
    32     public void requestInitialized(ServletRequestEvent arg0) {
    33         // TODO Auto-generated method stub
    34         System.out.println("服务器接收一个请求");
    35         ServletRequest request = arg0.getServletRequest();
    36         if (request instanceof HttpServletRequest) {
    37             HttpServletRequest httpRequest = (HttpServletRequest) request;
    38             String url = httpRequest.getRequestURI();
    39             System.out.println(url);
    40         }
    41     }
    42 
    43     @Override
    44     public void requestDestroyed(ServletRequestEvent arg0) {
    45         // TODO Auto-generated method stub
    46         System.out.println("服务器销毁一个请求");
    47     }
    48 
    49     @Override
    50     public void sessionCreated(HttpSessionEvent arg0) {
    51         // TODO Auto-generated method stub
    52         System.out.println("服务器创建一个会话");
    53         HttpSession session = arg0.getSession();
    54         session.setAttribute("count", 10);
    55 
    56         ServletContext application = session.getServletContext();
    57         Object obj = application.getAttribute("count");
    58         if (obj instanceof Integer) {
    59             Integer appCount = (Integer) obj;
    60             appCount += 1;
    61             application.setAttribute("count", appCount);
    62         }
    63     }
    64 
    65     @Override
    66     public void sessionDestroyed(HttpSessionEvent arg0) {
    67         // TODO Auto-generated method stub
    68         System.out.println("服务器销毁一个会话");
    69         HttpSession session = arg0.getSession();
    70         ServletContext application = session.getServletContext();
    71         Object obj = application.getAttribute("count");
    72         if (obj instanceof Integer) {
    73             Integer appCount = (Integer) obj;
    74             appCount -= 1;
    75             application.setAttribute("count", appCount);
    76         }
    77     }
    78 }
    Global

    web.xml文件:

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    4  <listener>
    5   <listener-class>Global</listener-class>
    6  </listener>
    7 </web-app>
    web.xml
  • 相关阅读:
    android wifi Direct Audio TX/RX延迟分析
    linux C语言结构体对齐
    android wifi SWOL低功耗模式
    android firmware 利用UDP socket发送Magic Packet--python版本
    android tcp协议主要函数
    android firmware 利用UDP socket发送Magic Packet--c语言版本
    linux tcp协议重传定时器
    linux tcp协议状态机
    /normalize.css 比cssreset更实用
    关于无法net start mysql无法启动数据库的解决办法
  • 原文地址:https://www.cnblogs.com/qiyebao/p/3683463.html
Copyright © 2020-2023  润新知