[OsChina] Web应用使用application对象

发表于:  阅读量:54936

摘要

???????application对象代表Web应用本身,对于整个Web应用有效,一旦JSP,Servlet将数据放入application中,该数据将可以被该应用下其他所有的JSP,Servlet访问。application对象通常有两个作用:???...

        application对象代表Web应用本身,对于整个Web应用有效,一旦JSP,Servlet将数据放入application

中,该数据将可以被该应用下其他所有的JSP,Servlet访问。application对象通常有两个作用:    

        1 在整个Web应用的多个JSP,Servlet之间共享数据                        

        2 访问Web应用的配置参数

         通过application对象在整个Web应用的多个JSP,Servlet之间共享数据:            

        application通过setAttribute(String attrName, Object value)方法将值value设置给application对象的属性

attrName,该属性的值在整个Web应用有效,该Web应用的每个JSP页面和Servlet都可以通过调用

getAttribute(String attrName)方法访问该属性。

         例如下面的页面set_application.jsp使用JSP声明语句声明一个变量count,又在JSP脚本中声明一个变

量local_count,每次刷新该页面,都把count和local_count自增一次,然后将这两个变量的值放入application

对象中,名称分别为access_count,local_count,代码清单如下:                                                               

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>set attribute of application</title>
</head>

<body>
   	<%-- JSP声明语句中定义的变量,会自动成为Servlet类型的属性 --%>
	<%!
		int count = 0;
	%>
	
	
	<%--JSP脚本中定义 的变量局部变量, 需要初始化 --%>
	<%
		int local_count = 0;
		application.setAttribute("access_count", String.valueOf(++count));
		application.setAttribute("local_count", String.valueOf(++local_count));
	%>
	<%=count %><br />
	<%=local_count %><br />
</body>
</html>

        然后在get_application.jsp页面中使用getAttribute(String attrName)方法分别访问这两个属性,代码清单

如下:                                                                                                                                                              

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>get attribute of application</title>
</head>
<body>
	<%
		String access_count = application.getAttribute("access_count").toString();
		String local_count = application.getAttribute("local_count").toString();
	%>
	 
	<%="access_count = " + access_count	%><br />
	<%="local_count = " + local_count %><br />
</body>
</html>

        将set_application.jsp页面刷新3次,然后再访问get_application.jsp页面,结果如下:                                    

        可以看到经过3次刷新后,access_count属性值变成3,local_count值属性值为1,说明属性值确实成功

设置到application对象中了,在其它JSP页面中可以访问。                     

        至于为什么access_count值变化了,local_count的值没有变化,是因为JSP声明语句中声明的变量会自

动转换成Servlet的属性,而JSP脚本中声明的变量转换成了_jspService方法的局部变量,set_application.jsp

对应的Servlet代码片段如下,从代码中可以看出来:                                                                                      


	//此处省略部分代码
public final class set_005fapplication_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent,
                 org.apache.jasper.runtime.JspSourceImports {

	int count = 0;
	
	//此处省略部分代码
  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
      throws java.io.IOException, javax.servlet.ServletException {

	//此处省略部分代码
	int local_count = 0;
  }
}

        所以刷新set_application.jsp页面时,count的值会保存,而local_count的值每次都会被重新初始化。 

        访问Web应用的配置参数                                                   

        在页面get_application_param.jsp通过application对象访问Web应用的配置参数,配置参数在web.xml

中,分别分配了driver,url,username,password四个参数,表示数据库驱动的名称,标识,用户名及密

码;这四项配置在web.xml中如下:                                                                                                                

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
	<context-param>
		<param-name>driver</param-name>
		<param-value>com.mysql.jdbc.Driver</param-value>
	</context-param>
	<context-param>
		<param-name>url</param-name>
		<param-value>jdbc:mysql://localhost:3306/db_note</param-value>
	</context-param>
	<context-param>
		<param-name>username</param-name>
		<param-value>zcj</param-value>
	</context-param>
	<context-param>
		<param-name>password</param-name>
		<param-value>zcj</param-value>
	</context-param>
</web-app>

        get_application_param.jsp页面代码清单如下:                                                                                       

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Get Application Parameters</title>
</head>
<body>
	<%
		String driver = application.getInitParameter("driver");
		String url = application.getInitParameter("url");
		String user = application.getInitParameter("username");
		String password = application.getInitParameter("password");
	%>
	
	<%="driver: " + driver %><br />
	<%="url: " + url %><br />
	<%="username: " + user %><br />
	<%="password: " + password %><br />
</body>
</html>

        在浏览器中访问get_application_param.jsp页面,内容如下:                                                                       

        最后要指出的是,虽然aplication对象可以方便的在JSP,Servlet之间共享数据,但不要仅为了JSP和

Servlet之间共享数据就将数据放入application对象中,这样会导致滥用application对象,通常只把Web应用

的状态数据放入application对象中。

关键词: applicationWebJSP

上一篇:  java8的list,map简化操作

下一篇:  iOSApns远程推送

渝ICP备16002246号 Copyright © 2017. Singee77.com