前言:
把介绍JSP的资料整理了一下,发现JSP区别于HTML的核心内容比较少,加上前两篇的JSP指令和动作元素,和这一篇的内置对象,JSP的东西就差不多就讲完了,剩下的就靠熟能生巧了。这三篇文章就当资料库,以后在Web项目开发中,遇到JSP内容,可以随时来翻看。
正文:
内置对象名称 | 相对应的类 | 作用域 |
---|---|---|
request | javax.servlet.ServletRequest | request |
response | javax.servlet.ServletResponse | page |
pageContext | javax.servlet.jsp.pageContext | session |
session | javax.servlet.http.HttpSession | page |
application | javax.servlet.ServletContext | application |
out | javax.servlet.jsp.JspWriter | page |
config | javax.servlet.ServletConfig | page |
page | java.lang.Object | page |
exception | java.lang.Throwable | page |
- page范围:指所设置的属性仅在当前页面内有效。使用pageContext的setAttribute()方法可以设置属性值,使用pageContext的getAttribute()方法可以获得属性值;
- request范围:指属性仅在一次请求的范围内有效。使用request的setAttribute()方法可以设置属性值,使用request的getAttribute()方法可以获得属性值;
- session范围:指的是属性仅在浏览器与服务器进行一次会话的范围内有效,当和服务器断开连接后,属性就会失效。使用session的setAttributr()方法可以设置属性值,使用session的getAttribute()方法可以获得属性值;
- application范围:值属性在整个Web应用中都有效,直到服务器停止后才失效。使用application的setAttribute()方法可以设置属性值,使用application的getAttribute()方法可以获得属性值;
1、request对象
request对象用于获取客户端信息。JSP容器会将客户端的请求信息封装在request对象中,在客户端发出请求时会创建request对象,在请求结束后,则会销毁request对象。
主要方法:
Obejct getAttribute(String name) //获取指定的属性值
void setAttribute(String name, Object value) //将指定属性的值设置为value
String getParameter(String name) //获取请求参数名为name的参数值
Enumeration getParameterNames() //获取所有请求参数的名字集合
String[] getParameterValues(String name) //获得name请求参数的参数值
Map getParameterMap() //获取所有请求参数名和请求参数值所组成的Map对象
void setCharacterEncoding(String encoding) //设定编码格式
eg:
<%
String param = "";
request.setCharacterEncoding("gb2312"); //接收中文
Enumeration params = request.getParamterNames(); //获取所有参数值
while(params.hasMoreElements){
//遍历输出参数名和参数值
param = (String)params.nextElement();
out.println("ParamName : " + param + "<br>");
out.println("ParamValue : " + request.getParameter(param) + "<br>");
}
%>
2、response对象
response对象包含了从JSP页面返回客户端的所有信息,其作用域是它所在的页面。它被作为_jspService()方法的一个参数而由引擎传递给JSP,在这里离JSP要改动它。
主要方法:
void addCookie(Cookie cookie) //添加一个Cookie对象,用于在客户端保存特定的信息
void assHeader(String name, String value) //添加HTTP头信息,该Header信息将被发送到客户端
void containsHeader(String name) //用于判断指定名字的HTTP头文件是否存在
void sendError(int) //向客户端发送错误的状态码
void sendRedirect(String url) //重定向JSP
void setContentType(String contentType) //设置MIME类型与编码方式
eg:
<%
String str = request.getParameter("userName");
if(str!=null && !str.equals("")){
response.sendRedirect("http://新的连接地址"); //使用sendRedirect方法实现重定向
}
%>
response对象实现的重定向和<jsp:forward>
动作元素的最大区别在于,<jsp:forward>
只能在本网站内跳转,客户端地址不变,而response.sendRedirect则可以跳转到任何一个地址的页面,客户端地址改变。
3、out对象
out内置对象是一个缓冲的输出流,用来向客户端返回信息。它是javax.servlet.jsp.JspWriter的一个实例。由于向客户端输出时要先进行连接,所以总是采用缓冲输出的方式,因此out是缓冲输出流。
常用方法
public abstract void clear() throws java.io.IOException //清除缓冲区中的内容,但不把数据输出到客户端
public abstract void clearBuffer() throws java.io.IOException //清除缓冲区中的内容,同时将数据输出到客户端
public abstract void close() throws java.io.IOException //关闭缓冲区并输出缓冲区中的数据
public abstract void flush() throws java.io.IOException //输出缓冲区中的数据
public int getBufferSize() //获取缓冲区的大小
public abstract int getRemaining() //获取剩余缓冲区的大小
public boolean isAutoFlush() //缓冲区是否进行自动清除
public abstract void newLine() throws java.io.IOException //输出一个换行符
public abstract void print(String str) throws java.io.IOException //向客户端输出数据
public abstract void println(String str) throws java.io.IOException //向客户端输出数据并换行
eg:
<%
int allBuf = out.getBufferSize(); //获取缓冲区大小
int remainBuf = out.getRemaining(); //获取剩余缓冲区大小
int usedBuf - allBuf - remainBuf;
out.println("已用缓冲: " + usedBuf); //输出一用缓冲区大小
%>
4、session对象
HTTP协议是一种无状态协议,一个客户向服务器发出请求(request),然后服务器返回相应(response),伺候连接就被关闭了,在服务器中不会保留与本次连接相关的信息。因此,当下次客户再与服务器建立时,服务器中已经没有之前的连接信息了,因而无法判断本次连接与以前的连接是否都是属于同一客户,在这种情况下,就可以采用会话(session)来记录连接的信息。
会话:从一个客户打开浏览器与服务器建立连接,到这个客户关闭浏览器与服务器断开连接的过程。
常用方法:
Object getAttribute(String name) //获取session范围内name属性的值
void setAttribute(String name, Object value) //设置session范围内name属性的属性值为value,并存储在session对象中
void removeAttribute(String name) //删除session范围内name属性的值
Enumeration getAttributeNames() //获取所有session对象中存放的属性名称
long getCreationTime() //返回session被创建的时间
String getId() //返回session创建时由JSP容器所设定的唯一标识
long getLastAccessedTime() //返回用户最后一次通过session发送请求的时间,单位为毫秒
int getMaxInactiveInterval() //返回session的失效时间,即两次请求间间隔多长时间该session就被取消,单位为秒
boolean isNew() //判断是否为新的session
void invalidate() //清空session的内容
ge:
<%
String name = request.getParameter("userName");
if(name!=null && !name.equals("")){
session.setAttribute("userName",name); //设置session的属性值
}
%>
<%
String sessionID = session.getId();
String name = "";
//判断是否为新的session
if(!session.isNew()){
name = (String)session.getAttribute("userName"); //获取session中的userName属性值
out.println(name);
session.invalidate(); //清空session
}
%>
5、application对象
application对象用于获取和设置Servlet的相关信息,它的生命周期是从服务器启动直到服务器关闭为止。
常用方法:
void setAttribute(String name, Object value) //以键值对的方式,将一个对象的值存放到application中
Object getAtrribute(String name) //根据属性名获取application中存放的值
eg:
<%
//简易的统计网站访问人数
String count = (String)application.getAttribute("count"); //获取属性值
if(count == null){
count = "1";
}else{
count = Integer.parseInt(count) + 1 + "";
}
application.setAttribute("count",count); //设置属性值
%>
6、pageContext对象
pageContext对象是一个比较特殊的对象,使用它不仅可以设置page范围内的属性,还可以设置其他范围内的属性。但在实际JSP开发过程中pageContext对象使用的并不多。
常用方法:
ServletRequest getRequest() //获取当前页面中的request对象
ServletResponse getResponse() //获取当前页面中的response对象
HttpSession getSession() //获取当前页面中的session对象
ServletContext getServletContext() //获取当前页面中的application对象
ServletConfig getServletConfig() //获取当前页面中config对象
Object getPage() //获取当前页面中的page对象
JspWriter getOut() //获取当前页面中的out对象
Exception getException() //获取当前页面中的exception对象
Object getAttribute(String name) //获取page范围内的name属性值
Object getAttribute(String name, int scope) //获取指定范围内的name的属性值,scope的取值
Enumeration getAttributeNamesInScope(int scope) //获取指定范围内的所有属性值
int getAttributeScope(String name) //获取属性name的作用范围
void setAttribute(String name,Object obj) //设置page范围内的name的属性
void setAttribute(String name, Object obj, int scope) //设置指定范围内的name的属性
Object findAttribute(String name); //寻找name属性并返回该属性,如果找不到则返回null
void removeAttribute(String name); //删除属性名为name的属性
void removeAttribute(String name, int scope) //删除指定范围内的name属性
scope的取值:
PageContext.PAGE_SCOPE ---- page范围
PageContext.REQUEST_SCOPE ----- request范围
PageContext.SESSION_SCOPE ---- session范围
PageContext.APPLICATION_SCOPE ---- application范围
eg:
<%
//设置不同范围的attribute属性值
pageContext.setAttribute("attributeName", "pageScope"); //设置page范围内的attribute值
request.setAttribute("attributeName", "requestScope"); //设置request范围内的attribute值
session.setAttribute("attributeName", "sessionScope"); //设置session范围内的attribute值
application.setAttribute("attributeName", "applicationScope"); //设置application范围内的attribute值
//获取不同范围的attribute属性值
String str1 = (String)pageContext.getAttribute("attributeName",PageContext.PAGE_SCOPE); //获取Page范围内的attribute值
String str2 = (String)pageContext.getAttribute("attributeName",PageContext.REQUEST_SCOPE); //获取Request范围内的attribute值
String str3 = (String)pageContext.getAttribute("attributeName",PageContext.SESSION_SCOPE); //获取Session范围内的attribute值
String str4 = (String)pageContext.getAttribute("attributeName",PageContext.APPLICATION_SCOPE); //获取Application范围内的attribute值
%>
7、page对象
page对象指的是当前的JSP页面本身,它是Java.lang.object对象,通过page对象可以方便地调用Servlet类中定义的方法。page对象在实际开发过程中并不经常使用。
常用方法:
class getClass() //返回当前Object的类
int hashCode() //返回当前Object的哈希吗
String toString() //将此Object对象转换成字符串
boolean equals(Object obj) //比较此对象是否与指定的Object对象相等
void copy(Object obj) //将此Object对象复制到指定的Object对象中
8、config对象
config对象是ServletConfig类的一个实例,在Servlet初始化时,可以通过config向Servlet传递信息。只有在编写Servlet时,需要重载Servlet的init()方式时才会用到config对象。
String getServletName() //获取Servlet名称
ServletContext getServletContext() //获取一个包含服务器相关信息的ServletContext对象
String getInitParameter(String name) //获取Servlet初始化参数的值
Enumeration getInitParameterNames() //获取Servlet初始化所需要的所有参数名,返回类型是枚举类型
9、exception对象
exception对象是java.lang.Throwable类的对象,用来处理页面的错误和异常。如果在JSP页面中有未捕获的异常,则会生成exception对象,然后该exception对象传送到page指令中设置的异常处理页面中,在异常处理页面中对exception对象进行处理。在异常处理页面中需要将其page指令的isErrorPage属性设置为true才可以使用exception对象。
常用方法:
String getMessage() //返回exception对象的异常信息
String getLocalizedMessage() //返回本地化语言的异常错误
void printStackTrace() //打印异常的栈反响跟踪轨迹
String toString() //返回关于异常的简单描述