在web开发中,为方便开发者,JSP定义了一些由JSP容器实现和管理的内置对象,这些对象可以直接被开发者使用,而不需要再对其进行实例化!本文详解,JSP2.0之后由ServletApi提供的9个内置对象,如下:
内置对象名称 | 相对应的类 | 作用域 |
request | javax.servlet.ServletRequest | request |
response | javax.servlet.ServletResponse | page |
pageContext | javax.servlet.pageContext | session |
session | javax.servlet.HttpSession | page |
application | javax.servlet.ServletContext | application |
out | javax.servlet.JspWriter | page |
config | javax.servlet.ServletConfig | page |
page | javax.servlet.Object | page |
exception | javax.servlet.Throwable | page |
由此可见,JSP内置对象作用域范围,分别是:page,request,session,application
介绍一下:
1、page范围:指的是所设置的属性只在当前页面有效,使用pageContext.setAttribute()设置值,page.getAttribute()取值
2、request范围:指的是仅在一次请求的范围内有效request.setAttribute()设置值,request.getAttribute()取值
3、session范围:指的是属性仅在浏览器与服务器进行一次会话时有效,服务器断开之后,就失去作用,一般与用户有关
session.setAttribute()设置值,session.getAttribute()取值
4、application:是指在整个web应用中都有效,直到服务器停止为止,application.setAttribute() 设置值,application.getAttribute()取值,一般与用户无关
第二部分:
1、介绍request对象:
request中主要方法有getAttribute()-获取指定的属性值,getParameter("name")获取请求参数名为name的值如下示例源码:
以requestform.jsp作为提交表单,以requestobject.jsp作为接受对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page contentType="text/html;charset=GB2312" %>
<
html
>
<
head
>
<
title
>表单页</
title
>
</
head
>
<
body
>
<!-- 使用form标签创建表单-->
<
form
action
=
"requestobject2.jsp"
method
=
"post"
>
<
p
>用户名:<
input
type
=
"text"
name
=
"username"
/></
p
>
<
p
>年龄: <
input
type
=
"text"
name
=
"age"
/></
p
>
<
input
type
=
"submit"
value
=
"提交"
/>
</
form
>
</
body
>
</
html
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page contentType="text/html;charset=GB2312" %>
<
html
>
<
head
>
<
title
>request object test</
title
>
</
head
>
<
body
>
<!--获取表单域的值-->
<%
request.setCharacterEncoding("gb2312"); //中文名时需要设置编码格式
String username=request.getParameter("username"); //获取用户名
String strage=request.getParameter("age"); //获取用户年龄,此时为String类型
int age=Integer.parseInt(strage); //将字符串解析为整数
%>
<!--下面输出表单域的值-->
用户名:<%=username%><
br
>
年龄:<%=age%>
</
body
>
</
html
>
|
(源码2)
2、介绍response对象:
response常用方法有
addHeader(String name,String value)添加HTTP头信息,该Header信息奖杯发送到客户端
addCookie(Cookie cookie)添加cookie对象
setRedirect(url)重定向jsp文件
例如我们将源码2的代码稍微修改一下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<%@ page contentType="text/html;charset=GB2312" %>
<
html
>
<
head
>
<
title
>response object test</
title
>
</
head
>
<
body
>
<!--获取表单域的值-->
<%
String str=null;
str=request.getParameter("username");
if(str==null)
{str="";
}
byte b[]=str.getBytes("ISO-8859-1");
str=new String(b);
if(str.equals(""))
{
response.sendRedirect("responseform.jsp");
}
else
{
out.println("欢迎您来到本网页!");
out.println(str);
}
%>
</
body
>
</
html
>
|
(源码3)
意思是说,当输入值不为空时,打印输入的值,为空时,跳转到提交页面!
这里需要说明的是:response实现的重定向和<jsp:forward>最大的区别在于<jsp:forward>智能在本网站内跳转,而response.sendRedirect则可以任意跳转到任何一个地址的页面。
3、介绍out对象:
out对象常用方法有
clear()清楚缓冲区的内容,clearBuffer()清楚缓冲区的内容同时将数据输出到客户端
getBufferSize()获取缓冲区的大小
最常见的是直接打印输出内容
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<%@ page contentType="text/html;charset=GB2312" %>
<
html
>
<
head
>
<
title
>out object test</
title
>
</
head
>
<
body
>
<!-- 使用out对象输出-->
<%
out.print("Hello World"); //不换行
out.println("Hello World"); //换行
%>
</
body
>
</
html
>
|
第二种使用out对象获取缓冲区的大小
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ page contentType="text/html;charset=GB2312" %>
<
html
>
<
head
>
<
title
>out object test</
title
>
</
head
>
<
body
>
<!-- 使用out获取缓冲区信息-->
<%
int allbuf=out.getBufferSize(); //获取缓冲区的大小
int remainbuf=out.getRemaining(); //获取剩余缓冲区的大小
int usedbuf=allbuf-remainbuf; //已用缓冲区
out.println("已用缓冲区大小为:"+usedbuf); //输出已用缓冲区大小
%>
</
body
>
</
html
>
|
4、介绍session对象:
session是会话对象主要是用来记录每个客户端访问状态,这个对象使用十分频繁!例如,我前面博文介绍的可以限制用户单号登录,防止表单重复提交都是用session来完成!
常用方法:
setAttribute(String name,Object value)设置session范围内的name属性的属性值为value,并将其存储在session的对象中
getSession()获取session范围内的name属性的值
boolean isNew 是否存有新的session
removerAttribute(String name)删除session值
下面是hi一个简单的例子:
login.jsp,check.jsp,logout.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<%@ page contentType="text/html;charset=GB2312" %>
<%
String name="";
//判断是否为新的session
if(!session.isNew()){
name=(String)session.getAttribute("username");
if(name==null)
name="";
}
%>
<
p
>欢迎光临!</
p
>
<
p
>Session ID:<%=session.getId()%></
p
>
<
html
>
<
head
>
<
title
>用户登录</
title
>
</
head
>
<
body
>
<!-- 使用form标签创建表单-->
<
form
action
=
"check.jsp"
method
=
"post"
>
<
p
>用户名:<
input
type
=
"text"
name
=
"username"
value=<%=name%>></
p
>
<
input
type
=
"submit"
value
=
"提交"
/>
</
form
>
</
body
>
</
html
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page contentType="text/html;charset=GB2312" %>
<
html
>
<
head
>
<
title
>进入空间</
title
>
</
head
>
<
body
>
<%
String name=null;
name=request.getParameter("username");
if(name!=null)
session.setAttribute("username",name);
%>
<
a
href
=
"login.jsp"
>登录</
a
>
<
a
href
=
"logout.jsp"
>注销</
a
>
<
p
>当前用户为:<%=name %></
p
>
<
p
>中共有10条未读消息</
p
>
</
body
>
</
html
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<%@ page contentType="text/html;charset=GB2312" %>
<
html
>
<
head
>
<
title
>注销页面</
title
>
</
head
>
<
body
>
<%
String name=(String)session.getAttribute("username");
session.invalidate();
%>
<%=name %>,再见!
<
p
>
<
p
>
<
a
href
=
"login.jsp"
>重新登录</
a
>
</
body
>
</
html
>
|
5、介绍application对象:
application对象主要用于获取和设置Servlet的相关信息,因为它的生命周期是从服务器创建知道关闭为止,对象将会一直存在
主要方法setAttribute(String name ,Object value)Object getAttribute(String name)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ page contentType="text/html;charset=GB2312" %>
<
html
>
<
head
>
<
title
>application object test</
title
>
</
head
>
<
body
>
<%
String count=(String)application.getAttribute("count");
if(count==null)
{
count="1";
}
else
{
count=Integer.parseInt(count)+1+"";
}
application.setAttribute("count",count);
%>
<%="<
h1
>到目前为止,访问该网站的人数为:"+count+"</
h1
><
br
>" %>
</
body
>
</
html
>
|
6、介绍pageContext对象
pageContext是一个特殊的属性,因为不仅仅可以设置page范围的属性,还可以设置其他范围的属性,通过pageContext可以访问本页面的所有其他对象,如request,response,out,而因为request,response本身已提供给我们,所以pageContext使用并不多
详细获取方法如下代码,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<%@ page contentType="text/html;charset=GB2312" %>
<
html
>
<
head
>
<
title
>pageContext object test</
title
>
</
head
>
<
body
>
<%
pageContext.setAttribute("attributename","page_scope"); //设置page范围内的属性
request.setAttribute("attributename","request_scope"); //设置request范围内的属性
session.setAttribute("attributename","session_scope"); //设置session范围内的属性
application.setAttribute("attributename","application_scope"); //设置application范围内的属性
String str1=(String)pageContext.getAttribute("attributename",pageContext.PAGE_SCOPE);
String str2=(String)pageContext.getAttribute("attributename",pageContext.REQUEST_SCOPE);
String str3=(String)pageContext.getAttribute("attributename",pageContext.SESSION_SCOPE);
String str4=(String)pageContext.getAttribute("attributename",pageContext.APPLICATION_SCOPE);
%>
attributename在不同范围的属性值
<
br
>
<%="page范围:"+str1 %><
br
>
<%="request范围:"+str2 %><
br
>
<%="session范围:"+str3 %><
br
>
<%="application范围:"+str4 %><
br
>
</
body
>
</
html
>
|
7、介绍page对象:
page对象是指的是JSP页面的本身,她是Object对象的类,通过page对象可以调用到Servlet类定义的方法,page对象在开发中并不经常用到,这里就不讲解了!
8、介绍config对象:
config对象是ServletConfig类的一个实例,在Servlet初始化时,可以通过config向Servlet传递信息,在JSP开发中config使用并不多,只有需要重新重载Servlet的init()方法时才会用到config方法
其中有一个方法使用较多,ServletContext.getServletContext获得一个包含服务器相关信息的ServlletContext对象
9、介绍exception对象:
主要是处理页面异常和错误,实际开发中此对象使用也不多,让用户直接看到错误,是很不好的吧?
所以一般都是调试项目会使用
使用这个对象必须将isErrorPage设置为true
如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page contentType="text/html;charset=GB2312" errorPage="exceptionobject.jsp"%>
<
html
>
<
head
>
<
title
>pageContext object test</
title
>
</
head
>
<
body
>
发生错误的位置!<
br
>
<%
int a=5;
int b=0;
%>
输出结果=<%=(a/b)%>
</
body
>
</
html
>
|
打印错误代码
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<%@ page contentType="text/html;charset=GB2312" isErrorPage="true"%>
<%@ page import="java.io.PrintStream"%>
<
html
>
<
head
>
<
title
>exception object test</
title
>
</
head
>
<
body
>
<%=exception.getMessage() %><
br
>
<%
exception.printStackTrace(new java.io.PrintWriter(out));
%>
</
body
>
</
html
>
|
综上总结在实际开发中,我们经常使用的就是request,response,session,out,这四个!也是我们必须掌握的,以上代码经本人亲测,完全正确,文中有遗漏的地方欢迎举手!也算是自己一个小总结!