📄 106.html
字号:
<STYLE type=text/css>
<!--
body,td { font-size:9pt;}
hr { color: #000000; height: 1px}
-->
</STYLE>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD><TITLE>FAQ汇萃 >> Servlet >> 如何设置application 变量</title>
</head>
<body >
<p><IMG SRC="../image/jsp001_middle_logo.gif" WIDTH="180" HEIGHT="60" BORDER=0 ALT=""></p>
<table width=100% bgcolor="#cccccc" align=center cellpadding="2" cellspacing="0" border=1 bordercolorlight="#000000" bordercolordark="#FFFFFF">
<tr bgcolor="#EFF8FF"><td>
<a href=http://www.jsp001.com/list_thread.php?int_attribute=1>FAQ汇萃</a>
>> <a href=http://www.jsp001.com/list_thread.php?forumid=9&int_attribute=1>Servlet</a>
>> 如何设置application 变量 [<a href=http://www.jsp001.com/forum/showthread.php?goto=newpost&threadid=106>查看别人的评论</a>]<br>
<hr><p>由 webmaster 发布于: 2001-01-21 11:06</p><p> </p><p>application变量是对一个web application有效的全局变量,在保存一些全局有效的值时非常有用。与session变量不同,application变量可以用application.getAttribute()和application.setAttribute()来访问。<br><br>在JSP中,可以自动引用application变量,指代当前的web 应用。(有关web 应用,我将在另外的地方讨论。)<br><br>在引用application 变量时,必须使用同步。并且,需要测试一个application属性是否已经存在。请仔细研究下面的代码,在下面,设置了一个叫“change"的application 属性,保存一个整型值。关于在servlet中使用application变量,方法与下面的代码一致,不过,需要先说明一个application如下:<br><br>servletContext application=getServletContext();<br><br>后面的一段函数是env.java的片断,可以看看在servlet中如何使用application变量的。<br><br>Integer count = null;<br><br>synchronized (application) {<br><br> count = (Integer) application.getAttribute("change");<br><br> if (count == null)<br><br> count = new Integer(0);<br><br> count = new Integer(count.intValue() + 1);<br><br> application.setAttribute("change", count);<br><br>}<br><br><br><br>private void printApplication(PrintWriter pw, HttpServletRequest req)<br><br> throws IOException<br><br> {<br><br> ServletContext application = getServletContext();<br><br><br><br>pw.println("<h1>Application Information:</h1>");<br><br> pw.println("<table>");<br><br> pw.print("<tr><td>Major Version<td>");<br><br> pw.println(application.getMajorVersion());<br><br> pw.print("<tr><td>Minor Version<td>");<br><br> pw.println(application.getMinorVersion());<br><br>pw.print("<tr><td>Server Info<td>");<br><br> pw.println(application.getServerInfo());<br><br> pw.print("<tr><td>Real Path (of pathinfo)<td>");<br><br> pw.println(application.getRealPath(req.getPathInfo()));<br><br> pw.print("<tr><td>Mime-type (of pathinfo)<td>");<br><br> pw.println(application.getMimeType(req.getPathInfo()));<br><br> pw.println("</table>");<br><br><br><br> pw.println("<h1>Application Variables:</h1>");<br><br> pw.println("<table>");<br><br><br><br> Enumeration e = application.getAttributeNames();<br><br>while (e.hasMoreElements()) {<br><br> String name = (String) e.nextElement();<br><br> pw.print("<tr><td>");<br><br> pw.print(name);<br><br> pw.print("<td>");<br><br> pw.println(application.getAttribute(name));<br><br> }<br><br><br><br> pw.println("</table>");<br><br>}<br><br><br><br>%><br><br><br><br><html><br><br><head><title>Counter</title></head><br><br><body bgcolor=#ffffff><br><br><br><br><h1>Welcome, visitor: <%= count %></h1><br><br><br><br></body><br><br></html><br></p></td>
</tr>
</table>
<p>
<CENTER><a href="http://www.jsp001.com/forum/newreply.php?action=newreply&threadid=106">点这里对该文章发表评论</a></CENTER>
<p>该文章总得分是 <font color=red>0</font> 分,你认为它对你有帮助吗?
[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=106&intVote=4","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>非常多</a>](<font color=red>0</font>)
[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=106&intVote=2","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>有一些</a>](<font color=red>0</font>)
[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=106&intVote=1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>无帮助</a>](<font color=red>0</font>)
[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=106&intVote=-1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>是灌水</a>](<font color=red>0</font>) </p>
<script language="javascript" src="http://www.jsp001.com/include/read_thread_script.php?threadid=106"></script>
<p><CENTER>
Copyright © 2001 - 2009 JSP001.com . All Rights Reserved <P>
<IMG SRC="../image/jsp001_small_logo.gif" WIDTH="85" HEIGHT="30" BORDER=0 ALT="">
</CENTER></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -