JSP内置9大对象学习总结(三)

来源:互联网 发布:安特数据 编辑:程序博客网 时间:2024/06/08 10:27

 

四:application对象
application对象可将信息保存在服务器中,直到服务器关闭,否则application对象中保存的信息会在整个应用中都有效。与session对象相比,application对象的生命周期更长。类似于系统的“全局变量”。
application对象其实是实现javax.servlet.ServletContext接口类的实例对象,所有内容都应该首先参考J2EE_API下载地址http://download.csdn.net/source/2921112

application对象的常用方法,具体详见J2EE_API,下载地址http://download.csdn.net/source/2921112
Object
getAttribute(String name)
Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
Enumeration
getAttributeNames()
Returns an Enumeration containing the attribute names available within this servlet context.
String
getContextPath()
Returns the context path of the web application.
String
getInitParameter(String name)
Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
Enumeration
getInitParameterNames()
Returns the names of the context's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.
String
getMimeType(String file)
Returns the MIME type of the specified file, or null if the MIME type is not known.
String
getRealPath(String path)
Returns a String containing the real path for a given virtual path.
void
removeAttribute(String name)
Removes the attribute with the given name from the servlet context.
void
setAttribute(String name, Object object)
Binds an object to a given attribute name in this servlet context.
String
getInitParameter(String name)
Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

1.访问应用程序初始化参数
我们可以通过很多种方式类配置应用程序初始化参数,比如与数据库建立建立时,我们可以用一个文件来保存数据库的名字,加载驱动,端口号,用户名,登录密码等。在tomcat中用web.xml来对应用程序属性进行配置。
这样的话当我们要把一个web工程重新部署时,只需修改配置文件即可。
例如:在web.xml文件中通过配置<context-param>元素初始化参数。
<context-param>                          <!—定义连接数据库URL—>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/db_test</param-value>
</context-param>
<context-param>                                                         <!—定义连接数据库用户名—>
<param-name>name</param-name>
<param-value>bluesky_sky </param-value>
</context-param>
<context-param>                                                         <!—定义连接数据库密码—>
<param-name>password</param-name>
<param-value>123456</param-value>
</context-param>
在index.jsp页面中,访问web.xml文件获取初始化参数。
<%
String url=application.getInitParameter("url");
String name=application.getInitParameter("name");
String pwd=application.getInitParameter("password");
out.print(url);
out.print(name);
out.print(pwd);
%>
用这种方法可以方便的配置数据库的连接。我们还可以用application对象实现网页计数器。
五:out对象
out对象用于在web浏览器内输出信息,并且管理应用服务器上的输出缓冲区。是javax.servlet.jsp.jspwriter的对象。
1. 管理响应缓冲
在使用out输出数据时,需要用clear()或者clearBuffer()方法清除缓冲区内容,以便重新开始操作。通过clear()方法清除缓冲区,如果相应内容已经提交,则会报IOException异常,使用clearBuffer()方法则不会。

abstract  void
clear()
 Clear the contents of the buffer.
abstract  void
clearBuffer()
 Clears the current contents of the buffer.

 

abstract  void
close()
 Close the stream, flushing it first.
abstract  void
flush()
 Flush the stream.
int
getBufferSize()
 This method returns the size of the buffer used by the JspWriter.
abstract  int
getRemaining()
 This method returns the number of unused bytes in the buffer.
boolean
isAutoFlush()
 This method indicates whether the JspWriter is autoFlushing.
abstract  void
newLine()
 Write a line separator.

2.向客户端输出数据
使用print(“”)或者println(“”)向客户端输出数据。
六:其它内置对象
除了上面的内置对象,JSP中还包含pageContext、config、page及exception对象。
1.pageContext对象
pageContext对象的作用是取得任何范围的参数,通过该对象可以获取out、request、response、session、application等对象。详见Javax.servlet.jsp.PageContext中的方法。
2. config对象
config对象的主要作用是取得服务器的配置信息,从web.xml中取得。
String
getInitParameter(String name)
Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
Enumeration
getInitParameterNames()
Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
ServletContext
getServletContext()
Returns a reference to the ServletContext in which the caller is executing.
String
getServletName()
Returns the name of this servlet instance.
3. page对象
page对象代表了JSP页面本身,只有在页面内才是有效的。
4. exception对象
exception对象的作用是显示异常信息,只有在isErrorPage=”true”的页面中才可以被调用,在一般页面中使用将无法编译。由于该类继承自java.lang.Exception,因此很多方法均可以使用。
例如:
index.jsp
<%@page language=”java” import=”java.util.*” pageEcoding=”gbk” errorPage=”error.jsp” %>
<%
      int price=Integer.parseInt(“aaa”);
      out.println(“单价为:”+price);
%>
error.jsp
<%@page language=”java” import=”java.util.*” pageEcoding=”gbk” isErrorPage=”true” %>
<body>
      错误原因为:<%=exception.getMessage() %>
</body>
原创粉丝点击