JSP 中的Application隐式对象及示例

Application隐式对象是javax.servlet.ServletContext的一个实例。它主要用于获取初始化参数并在整个 JSP 应用程序中共享属性及其值,这意味着应用程序隐式对象设置的任何属性都可用于所有 JSP 页面。

方法:

  • 对象获取属性(字符串属性名称)
  • void setAttribute(字符串属性名,对象对象)
  • 无效删除属性(字符串对象名称)
  • 枚举 getAttributeNames()
  • 字符串 getInitParameter(字符串参数名称)
  • 枚举 getInitParameterNames()
  • String getRealPath(字符串值)
  • 无效日志(字符串消息)
  • URL 获取资源(字符串值)
  • InputStream getResourceAsStream(字符串路径)
  • 字符串 getServerInfo()
  • 字符串 getMajorVersion()
  • 字符串 getMinorVersion()
  1. Object getAttribute(String attributeName):它返回存储在给定属性名称中的对象。例如,下面的语句将返回存储在属性“MyAttr”中的对象。
        String s = (String)application.getAttribute("MyAttr");
    
  2. void setAttribute(String attributeName, Object object):它设置属性的值,或者换句话说,它将属性及其值存储在应用程序上下文中,可用于跨 JSP 应用程序使用。例子 -
        application.setAttribute(“MyAttribute”, “This is the value of Attribute”);
    

    上面的语句将存储属性及其值。如果我们在任何 JSP 页面中使用以下语句,'s' 的值是多少?

        String s= (String) application.getAttribute(“MyAttribute”);
    

    String 的值将是“This is the value of Attribute”,因为我们已经使用 setAttribute 方法设置了它。

  3. void removeAttribute(String objectName):此方法用于从应用程序中删除给定的属性。例如——它会从应用程序中删除属性“MyAttr”。如果我们尝试使用 getAttribute 方法获取已删除属性的值,它将返回 Null。
        application.removeAttribute(“MyAttr”);
    
  4. 枚举 getAttributeNames():该方法返回存储在应用程序隐式对象中的所有属性名称的枚举。
        Enumeration e= application.getAttributeNames();
    
  5. String getInitParameter(String paramname):它返回给定参数名称的初始化参数值。示例 – web.xml
        <web-app>
        …
        <context-param>
        <param-name>parameter1</param-name>
        <param-value>ValueOfParameter1</param-value>
        </context-param>
        </web-app>

    假设上面是我的 web.xml 文件

        String s=application.getInitParameter(“parameter1”);
    

    s 的值将是“ValueOfParameter1”。还是很困惑它是从哪里来的?请参阅 web.xml 文件中的 param-value 标记。

  6. 枚举 getInitParameterNames():它返回所有初始化参数的枚举。
        Enumeration e= application.getinitParameterNames();
    
  7. String getRealPath(String value):它将给定路径转换为文件系统中的绝对路径。
        String abspath = application.getRealPath(“/index.html”);
    

    abspath 的值将是基于现有文件系统的完整 http URL。

  8. void log(String message):此方法将给定消息写入与应用程序关联的 JSP 引擎(JSP 容器)的默认日志文件。
        application.log(“This is error 404 Page not found”);
    

    上述调用会将消息“This is error 404 Page not found”写入默认日志文件。

  9. String getServerInfo():此方法返回 JSP 容器(JSP 引擎)的名称和版本。
        application.getServerInfo();
    

应用程序隐式对象示例

使用应用程序捕获点击次数的 JSP 页面。在此示例中,我们使用应用程序隐式对象计算对 JSP 页面的点击次数。

counter.jsp

<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Application Implicit Object Example</title>
</head>
<body>
<%
 //Comment: This would return null for the first time
 Integer counter= (Integer)application.getAttribute("numberOfVisits");
 if( counter ==null || counter == 0 ){
 //Comment: For the very first Visitor 
 counter = 1;
 }else{
 //Comment: For Others 
 counter = counter+ 1;
 }
 application.setAttribute("numberOfVisits", counter);
%>
<h3>Total number of hits to this Page is: <%= counter%></h3>
</body>
</html>

输出截图

首次访问者的点击数为 1。

当我刷新页面时,点击次数增加了。

如果您喜欢本教程,请与您的朋友分享。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值