JSP的内置对象有request、response、pageContext、session、application、out、config、page、exception,这些内置对象在asp.net中也能找到对应或相似的对象。
JSP的内置对象都有对应的作用域,分别为:page,request,session,application。顾名思义,page指当前页有效;request指仅在一次请求范围内有效;session值在浏览器和服务器进行一次会话的范围内有效;application指整个web应用中都有效,直到服务器停止后失效。
内置对象名 | 作用域 |
request | request |
response | page |
pageContext | session |
session | page |
application | page |
out | page |
config | page |
page | page |
exception | page |
1.request
常用方法
方法 | 描述 |
Object 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) | 设置编码格式 |
requestobject.jsp
<body>
<%
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String strage = request.getParameter("age");
int age = Integer.parseInt(strage);
%>
用户名:<%=username %><br>
年龄:<%=age %>
</body>
request.jsp
<body>
<form method="post" action="requestobject.jsp">
用户名:<input type="text" name="username"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="提交" >
</form>
</body>
2.reponse
response对象包含从jsp页面返回客户端的所有信息,其作用域是它所在的页面。
方法 | 描述 |
void addCookie(Cookie cookie) | 添加一个Cookie对象 |
void addHeader(String name,String value) | 添加HTTP头信息 |
void containsHeader(String name) | HTTP头信息是否存在 |
void sendError(int) | 发送错误状态码 |
void sendRedorect(String url) | 重定向jsp文件 |
void setContentType(String contentType) | 设置MINE类型与编码方式 |
responseform.jsp
<form action="responseobject.jsp" method="post">
用户:<input type="text" name="username">
<br/>
<input type="submit" value="提交">
</form>
resonseobject.jsp
<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>
注意:response.sendRedirect()与<jsp:forward>最大差别在于<jsp:forward>只能在本网站内跳转,而response.sendRedirect()可以是任意地址页面
3.out对象
out内置对象是一个缓冲的输出流,用来向客户端返回信息。有些类似有asp.net的response.write
<body>
<%
int allbuf=out.getBufferSize(); //获取缓冲区大小
int remainbuf=out.getRemaining(); //获取剩余缓冲区大小
int usedbuf=allbuf-remainbuf;
out.println("已用缓冲区大小为:"+usedbuf);
%>
</body>
4.session对象
login.jsp
<body>
<%
response.setCharacterEncoding("UTF-8");
String name="";
if(!session.isNew()) //判断是否为新session
{
name=(String)session.getAttribute("username");
if(name==null)
{
name="";
}
}
%>
<p>欢迎</p>
<form action="check.jsp" method="post">
用户名:<input type="text" name="username" value=<%=name %>>
<input type="submit" value="提交">
</form>
</body>
check.jsp
<body>
<%
response.setCharacterEncoding("UTF-8");
String name=null;
name=request.getParameter("username");
if(name!=null)
{
session.setAttribute("username", name);
}
%>
<a href="login.jsp" >登录</a> |
<a href="loginout.jsp" >注销</a>
<p>当前用户为:<%=name %></p>
</body>
loginout.jsp
<body>
<%
response.setCharacterEncoding("UTF-8");
String name=(String)session.getAttribute("username");
session.invalidate(); //清空session
%>
<p><%=name %>再见</p>
<a href="login.jsp">重新登录</a>
</body>
5.application对象
<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>"%>
</body>
6.pageContext
pageContext是一个比较特殊的对象,使用它不仅可以设置page范围内属性,还可以设置其他范围内的属性。通过pageContext可以访问本页所有其他对象,如response,request,out等。类似于asp.net 的Context上下文
7.exception
exception对象是用来处理页面错误和异常。
exceptiontest.jsp
<html>
<head>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" errorPage="exceptionobject.jsp"
pageEncoding="ISO-8859-1"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<%
int a=5;
int b=0;
%>
result=<%=(a/b) %>
</body>
</html>
exceptionobject.jsp
<html>
<head>
<%@ page import="java.lang.Throwable" %>
<%@page import="java.io.PrintStream" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isErrorPage="true"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>???????</title>
</head>
<body>
<%= exception.getMessage()%><br> <!--返回异常信息-->
<%
exception.printStackTrace(new PrintWriter(out)); <!--打印堆栈信息-->
%>
</body>
</html>
注意:exception对象必须页面<%@page%>的isErrorPage属性设置成true才能得到