jsp中的隐藏对象
隐含对象(Implicit Objects)
作为JSP开发人员,你经常会隐含的访问那些在所有的JSP文件中都可以使用的对象。
如果你使用了这些对象,它们会被JSP引擎分析出,并在生成servlet时插入到对应的位置。
Out对象
Out隐含对象来源于java.io.Writer类,它用于发送输出流到客户端。
最通用的时out.println()方法,它来打印文本信息到客户端浏览器。
列表2.8显示了使用out隐含对象的例子:
--------------------------------------------------------------------------
<%@ page errorPage="errorpage.jsp" %>
<html>
<head>
<title>Use Out</title>
</head>
<body>
<%
// Print a simple message using the implicit out object.
out.println("<center><b>Hello Wiley" +
" Reader!</b></center>");
%>
</body>
</html>
--------------------------------------------------------------------
为了执行这个例子,复制文件到
<CATALINA_HOME>/webapps/ wileyapp/ 目录下,在浏览器中输入如下地址:
http://localhost:8080/wileyapp/out.jsp
你会看到:Hello Wiley Reader!
Request对象
这个隐含对象来源于javax.servlet.http.HttpServletRequest接口。它被关联到每一个HTTP请求。常用它来访问请求参数。你可以调用request对象的带参数名称的getParameter()方法,它将返回一个和参数名称匹配的字符串。
Request对象的使用例子如下
列表2.9: request.jsp.
-----------------------------------------------------------------------
<%@ page errorPage="errorpage.jsp" %>
<html>
<head>
<title>UseRequest</title>
</head>
<body>
<%
out.println("<b>Welcome: " +
request.getParameter("user") + "</b>");
%>
</body>
</html>
----------------------------------------------------
JSP通过参数user调用request.getParameter()方法。这个方法寻找参数列表中的键值user来返回数据。在浏览器中输入如下:
http://localhost:8080/wileyapp/request.jsp?user=Robert
可以看到输出:Welcome:Robert。
Response对象
response隐含对象来源于javax.servlet.http.HttpServletResponse。response对象用于把取得的数据返回到客户端。这个隐含对象可以实现HttpServletRequest所有的功能,和你在servlet处理没有什么区别。Response对象经常用于向客户端输出信息。然而JSP API已经提供了一个流对象out来输出信息到客户端。
PageContext对象
PageContext对象提供访问JSP页面的命名空间。它也提供用来访问其他的JSP隐含对象。比较常用的是使用setAttribute() 和getAttribute()方法设置和寻找对象。
Session对象
Session对象来源于javax.servlet.http.HttpSession。它用于存储客户端请求的信息,因此它是有状态交互式的。
Session对象列表如下:
列表2.10: session.jsp.
-------------------------------------------------------------------------------------
<%@ page errorPage="errorpage.jsp" %>
<html>
<head>
<title>Session Example</title>
</head>
<body>
<%
// get a reference to the current count from the session
Integer count = (Integer)session.getAttribute("COUNT");
if ( count == null ) {
// If the count was not found create one
count = new Integer(1);
// and add it to the HttpSession
session.setAttribute("COUNT", count);
}
else {
// Otherwise increment the value
count = new Integer(count.intValue() + 1);
session.setAttribute("COUNT", count);
}
out.println("<b>You have accessed this page: "
+ count + " times.</b>");
%>
</body>
</html>
复制文件到
<CATALINA_HOME>/wileyapp/ 目录,在浏览器中输入地址:
http://localhost:8080/wileyapp/session.jsp
如果你刷新页面访问次数会增加。
Application对象
Application对象来源于javax.servlet.ServletContext,在本章的前面已讨论过ServletContext。Application对象用于访问存储在ServletContext中的全局范围的对象。Application对象的使用方法可以在本章前面部分看到,在次不做叙述。
Config对象
Config对象来源于ServletConfig,它包含了当前JSP/Servlet所在的WEB应用的配置信息。
Page对象
Page对象来源于当前被访问JSP页面的实例化。它实际使用的是JSP转换成的Servlet。
Exception对象
Exception对象用于捕获JSP抛出的异常。它只有在JSP页面属性isErrorPage=true时才可用。
关于Jsp中的Application对象
ServletContext
ServletContext是定义在javax.servlet包中的对象。它定义了用于WEB应用中的服务器端组件关联servlet容器的方法集合。
ServletContext经常被用于存储对象的区域,这些对象在WEB应用中的所有的服务器端组件中使用。你可以把ServletContext当作在WEB应用中共享的存储区域。把一个对象放置到ServletContext中时,它存在于WEB应用的整个生命周期中,除非它被明确的删除或替换。在ServletContext中定义了四个方法来实现存储区的共享功能。
表2.1描述了四个方法:
方法名 | 描述 |
setAttribute(String name,Object obj) | 通过名称绑定一个对象并存储对象到当前ServletContext。如果指定的名称已经被使用过,这个方法会删除旧对象绑定为新对象。 |
getAttribute(String name) | 返回指定名称的对象,如果名称不存在返回null。 |
removeAttribute(String name) | 从ServletContext中删除指定名称的对象
|
getAttributeNames()
| 返回在ServletContext中的指定名称的对象集合 |
而ServletContext在jsp中所对应的就是application, application为ServletContext的一个引用
示例:
Servlet中
// 获取ServletContext
ServletContext context = getServletContext();
// 获取
String userName = (String)context.getAttribute("USERNAME");
在对应的Jsp中
String userName = (String)application.getAttribute("USERNAME");