Java Web笔记 - JSP中常用内置对象的介绍与分析

发布于 2011-11-09 | 更新于 2020-09-20

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则是在整个服务器的运行过程中。

本文作者: arthinking

本文链接: https://www.itzhai.comjava-web-notes-jsp-built-in-objects-commonly-used-in-the-description-and-analysis.html

版权声明: 版权归作者所有,未经许可不得转载,侵权必究!联系作者请加公众号。

×
IT宅

关注公众号及时获取网站内容更新。