Java Web笔记 – JSP中常用内置对象的介绍与分析
本文由发表于6年前 | J2EE | 评论数 1 |  被围观 5,776 views+
1、九种常用的内置对象:1.1、request:请求对象
相关说明:
setAttribute和getAttribute方法说明:
request对象的范围:
1.2、response:响应对象
相关说明:
1.3、pageContext:页面上下文对象1.4、session会话对象
相关说明:
session对象的范围:
1.5、application:应用程序对象
相关方法:
application(应用对象)的范围:
aplication的其他常用方法:
1.6、out:输出对象1.7、config:配置对象1.8、page:页面对象1.9、exception:异常对象2、request、session和application三个对象的范围总结:
1、九种常用的内置对象:
1.1、request:请求对象
javax.servlet.ServletRequest的子类
request
相关说明:

request对象代表的是来自客户端的请求。

常用方法:

getParameter
String getParameter(String name)
Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues(java.lang.String).
If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues.
If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputStream() or getReader() can interfere with the execution of this method.
Parameters:
name - a String specifying the name of the parameter
Returns:
a String representing the single value of the parameter
See Also:
getParameterValues(java.lang.String)

getParameter方法的作用是获取到客户端通过表单或者URL请求参数所发送过来的参数值,是客户端与服务器端之间的交互,服务器端要获取客户端发送的数据,需要使用getParameter方法来获取。

getParameterNames
Enumeration getParameterNames()
Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration.
Returns:
an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters
getParameterValues
String[] getParameterValues(String name)
Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
If the parameter has a single value, the array has a length of 1.
Parameters:
name - a String containing the name of the parameter whose value is requested
Returns:
an array of String objects containing the parameter's values
See Also:
getParameter(java.lang.String)

getParameter方法是getParameterValues方法的特例,表示请求参数值只有一个,如果请求参数值有多个,使用getParameterValues方法。如在多选框的情况下。

setAttribute和getAttribute方法说明:
setAttribute
public void setAttribute(String name,
                         Object o)
The default behavior of this method is to return setAttribute(String name, Object o) on the wrapped request object.
Specified by:
setAttribute in interface ServletRequest
Parameters:
name - a String specifying the name of the attribute
o - the Object to be stored
getAttribute
public Object getAttribute(String name)
The default behavior of this method is to call getAttribute(String name) on the wrapped request object.
Specified by:
getAttribute in interface ServletRequest
Parameters:
name - a String specifying the name of the attribute
Returns:
an Object containing the value of the attribute, or null if the attribute does not exist

在用户重定向一个新的Servlet时,访问了新的Servlet,所以也生成了新的ServletRequest,里面之前保存的信息也访问不了了,getAttribute只能获取服务器端设置的属性。

setAttribute与getAttribute方法一般是成对出现的。使用getAttribute方法获取到对象后一般都需要进行向下类型转换。setAttribute与getAttribute方法都是在服务器端执行的,客户端不知道服务器是否执行了该方法。与客户端传递参数给服务器方法不同,这种只能使用getParameter方法获取到,并且不能保持,使用setAttribute方法设置的参数在同一个客户端请求范围内都有效,在同一个请求中的任何地方(任何JSP或Servlet)都可以获取到之前设置的attribute参数。

request对象的范围:

request对象数据的声明周期是在request对象的声明周期范围内,当客户端向服务器发送一个请求,服务器向客户端返回一个响应后请求对象就被销毁了。之后再向服务器发送新的请求,服务器会创建新的request对象。

1.2、response:响应对象
javax.servletResponse的子类
page
相关说明:

response对象组织发送到客户端的数据,但是由于组织的方式比较底层,所以不建议读者使用。可以使用out对象向客户端发送文字即可。

1.3、pageContext:页面上下文对象
javax.servlet.jsp.PageContext
page
1.4、session会话对象
javax.servlet.http.HttpSession
session
相关说明:

表示服务器与客户端建立的会话。具体作用为:在不同的JSP页面间保留客户信息。
产生原因:

HTTP是无状态的协议
服务器对每个客户端请求都没有历史记录
而session用来保存客户端状态信息
public interface HttpSession
Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
相关方法:
void setAttribute(String name,
                  Object value)
Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.
After this method executes, and if the new object implements HttpSessionBindingListener, the container calls HttpSessionBindingListener.valueBound. The container then notifies any HttpSessionAttributeListeners in the web application.
If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called.
If the value passed in is null, this has the same effect as calling removeAttribute().
Parameters:
name - the name to which the object is bound; cannot be null
value - the object to be bound
Throws:
IllegalStateException - if this method is called on an invalidated session
getAttribute
Object getAttribute(String name)
Returns the object bound with the specified name in this session, or null if no object is bound under the name.
Parameters:
name - a string specifying the name of the object
Returns:
the object with the specified name
Throws:
IllegalStateException - if this method is called on an invalidated session
session对象的范围:

session对象内的数据声明周期就是session对象的声明周期。只要浏览器不关闭,session对象就会一直存在。在同一个浏览器窗口中,session对象只有一个。

1.5、application:应用程序对象
javax.servlet.ServletContext
application
public interface ServletContext
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.)
In the case of a web application marked "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead.
The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.
相关方法:
setAttribute
void setAttribute(String name,
                  Object object)
Binds an object to a given attribute name in this servlet context. If the name specified is already used for an attribute, this method will replace the attribute with the new to the new attribute.
If listeners are configured on the ServletContext the container notifies them accordingly.
If a null value is passed, the effect is the same as calling removeAttribute().
Attribute names should follow the same convention as package names. The Java Servlet API specification reserves names matching java.*, javax.*, and sun.*.
Parameters:
name - a String specifying the name of the attribute
object - an Object representing the attribute to be bound
getAttribute
Object getAttribute(String name)
Returns the servlet container attribute with the given name, or null if there is no attribute by that name. An attribute allows a servlet container to give the servlet additional information not already provided by this interface. See your server documentation for information about its attributes. A list of supported attributes can be retrieved using getAttributeNames.
The attribute is returned as a java.lang.Object or some subclass. Attribute names should follow the same convention as package names. The Java Servlet API specification reserves names matching java.*, javax.*, and sun.*.
Parameters:
name - a String specifying the name of the attribute
Returns:
an Object containing the value of the attribute, or null if no attribute exists matching the given name
See Also:
getAttributeNames()
application(应用对象)的范围:

声明周期范围最大的对象,只要服务器没有关闭,application对象中的数据就会一直存在。在整个服务器运行过程中,application对象只有一个。

aplication的其他常用方法:
支持的Java EE的Servlet规范的版本号:
getMajorVersion()  getMinorVersion()
getResource():
getRealPath():给定虚拟路径的真实路径
1.6、out:输出对象
javax.servlet.jsp.JspWriter
page
public abstract class JspWriter
extends Writer
The actions and template data in a JSP page is written using the JspWriter object that is referenced by the implicit variable out which is initialized automatically using methods in the PageContext object.
This abstract class emulates some of the functionality found in the java.io.BufferedWriter and java.io.PrintWriter classes, however it differs in that it throws java.io.IOException from the print methods while PrintWriter does not.

对象代表客户端发送数据的对象。

与response对象不同,通过out对象发送的内容是浏览器需要显示的内容,是文本级的。可以通过out对象向客户端写一个由程序动态生成的HTML文档。

1.7、config:配置对象
javax.servlet.ServletConfig
page

config对象提供一些配置信息,相关的方法:

getInitParameter
getInitParameterNames

获取初始化时的参数。

1.8、page:页面对象
java.lang.Object
page

page对象代表正在运行的由JSP文件产生的类对象,不建议使用。

1.9、exception:异常对象
java.lang.Throwable
page

代表JSP文件生成的异常对象,只能在引入了

<%@ page isErrorPage="true"%>

的JSP文件中使用。

2、request、session和application三个对象的范围总结:

这三个对象的范围是逐个增加的,request只在请求的范围内;session在浏览器窗口的范围内;application则是在整个服务器的运行过程中。

除了文章中有特别说明,均为IT宅原创文章,转载请以链接形式注明出处。
本文链接:http://www.itzhai.com/java-web-notes-jsp-built-in-objects-commonly-used-in-the-description-and-analysis.html
arthinking Java技术交流群:280755654,入门群:428693174 more
分享到:
 
2011 11/9
如果您有更好的原创技术博文或者观点,欢迎投稿:admin@itzhai.com,或者关注订阅左侧浮动面板的微信号订阅IT宅itread)发送消息。
文章评论
    一条评论
给我留言

有人回复时邮件通知我
J2EE的相关文章
随机文章 本月热门 热评
1 IKAnalyzer词典占用内存大小分析 2014/6/9
2 Struts2的一个和多个文件上传的实现 2011/5/8
3 How to Check if the Checkbox is Selected in jQuery 2011/5/11
4 ExtJS根据后台数据动态创建CheckboxGroup的方法 2011/10/2
5 ExtJS在树TreePanel之间拖放结点 2011/4/11
6 Git分布式文件管理工具与使用GitHub托管项目 2012/6/5
友情推荐 更多
破博客 文官洗碗安天下,武将打怪定乾坤。多么美好的年代,思之令人泪落。
Mr.5's Life 白天是一名程序员,晚上就是个有抱负的探索者
行知-追寻技术之美 关注大数据,分布式系统
我爱编程 编程成长轨迹
Cynthia's Blog 学习笔记 知识总结 思考感悟
 
猜您喜欢
欢迎关注我的公众号 IT宅
关于IT宅 文章归档

IT宅中的文章除了标题注明转载或有特别说明的文章,均为IT宅的技术知识总结,学习笔记或随笔。如果喜欢,请使用文章下面提供的分享组件。转载请注明出处并加入文章的原链接。 感谢大家的支持。

联系我们:admin@itzhai.com

Theme by arthinking. Copyright © 2011-2015 IT宅.com 保留所有权利.