输入输出有关的内置对象
request
request即请求对象,表示客户端对服务器发送的请求;用于接受客户端通过http协议传送给服务器的数据。与Servlet中的请求对象为同一对象。request对象的作用域为一次request请求。
request对象拥有HttpServletRequest接口的所有方法;常见如下:
void setCharacterEncoding(String charset)设置请求参数的解码字符集
String getParameter(String name)根据参数名获取单一参数值
String[] getParameterValues(String name)根据参数名获取一组参数值
void setAttribute(String name,Object value)以键值对的方式存储请求域属性
Object getAttribute(String name)根据属性名获取存储的对象数据
request对象可以通过setAttribute()和getAttribute()方法存取请求域属性,在实际开发中,多用于存储、传递本次请求的处理结果。
loginCheck.jsp
<%
//获取请求参数
request.setCharecterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
StringBuffer errorMsg = new StringBuffer();
//参数信息验证
if ("".equals(username)) errorMsg.append("用户名不能为空!<br>"):
if ("".equals(password)) errorMsg.append("密码不能为空!<br>");
request.setAttribute("errorMsg", errorMsg.toString());
if (errorMsg.toString().equals("")) out.println("您的信息验证成功");
else {
%>
<jsp:forward page="login.jsp'></jsp:forward>
<% } %>
login.jsp
<%
String error = (String)request.getAttribute("errorMsg");
if (error != null) out.println(error);
%>
<form action="loginCheck.jsp" method="post">
<P>用户名:<input name="username" type="text"></p>
<P>密 码:<input name="password" type="password"></p>
<P><input name="submit" type="submit" value="登录"></p>
</form>
上述代码中验证错误信息以请求域属性的形式保存在request对象中,通过请求转发的凡是将对象转发到login.jsp页面中,获得属性值,并再次传回
response
response对象为响应对象。主要用来将JSP处理后的结果传回到客户端。response对象与Servlet中的响应对象为同一对象。
response对象拥有HttpServletResponse接口的所有方法
void setContentType(String name)设置响应内容的类型和字符编码
void sendRedirect(String URL)重定向到指定的URL资源
out
out对象为输出对象用来控制管理输出的缓冲区(buffer)和输出流(output stream)向客户端页面输出数据。out对象与HttpServletResponse接口的getWriter()方法获得的PrintWriter对象功能相同。
方法 |
描述 |
---|---|
print/println(基本数据类型) | 输出一个基本数据类型的值 |
print/println(Object obj) | 输出一个对象的引用地址 |
print/println(String str) | 输出一个字符串的值 |
newLine() | 输出一个换行符 |
out对象的newLine()和println()方法在页面显示上并不会有换行的效果,但是在生成的HTML源码中,会在输出的数据后面换行
方法 | 描述 |
---|---|
void clear() | 清除缓冲区的内容。若缓冲区为空,则产生IOException异常 |
void clearBuffer() | 清除缓冲取的内容。若缓冲区为空,不会产生异常 |
void flush() | 直接将目前暂存的数据刷新输出 |
void close() | 关闭输出流,一旦关闭,不能多out做任何操作 |
int getBufferSizr() | 获取目标缓冲区的大小(kb) |
int getRemaining() | 获取目标使用后还剩下的缓冲区大小(kb) |
boolean isAutoFlush() | 返回true表示缓冲区满时自动刷新;false表示缓冲区满不会自动清除并产生异常 |
Content有关的内置对象
session
session对象即会话对象。一次会话的含义是从客户端浏览器连接服务器开始,到服务器端会话过期或用户主动退出后,会话结束。这个过程可以包含客户端和服务器的多次请求和响应。
session具有HttpSession接口的所有方法
void setAttribute(String name,Object value)以键值对的方式存储session域属性
Object getAttribute(String name)根据属性名获取属性值
void invalidate()使session对象失效,释放所有的属性空间
存储在session范围中的属性即使经过重定向的多次请求任然有效。在浏览器未关闭的情况下,若要让其失效,可以使用invalidate()方法,该方法可以实现安全退出功能。通常只应该把与用户会话状态相关的信息放入session范围内,随意将不是很重要的信息放入session中,会增大服务器的开销,一般的交换信息可以放置在request中。
application
application对象为应用程序上下文对象,表示当前应用程序运行环境,用以获取应用程序上下文环境中的信息。application对象在容器启动时实例化,在容器关闭时销毁。作用域为整个web容器的生命周期。
application对象具有ServletContent接口的所有功能
void setAttribute(String name,Object value以键值对的方式存储application域属性
Object getAttribute(String name)根据属性名获取属性值
void removeAttribute(String name)根据属性名从application域中移除属性
//主体代码省略
<script type="text/javascript">
function validate() {
var username = document.getElementById("username");
var massage = document.getElementById("message");
if (username.value == "") {
alert("please enter your name");
username.focus();
return false;
} else if (message.value == "") {
alert("please leave a message");
message.focus();
return false;
}
return true;
}
</script>
<form action="guestBook.jsp" method="post" onsubmit="return validate();">
<p>请输入您的名字:<input name="username" id="username" type="text”></p>
<p>请输入您的留言:<textarea name="message" id="message" cols="50' rows="3"></textarea></p>
<p><input type="submit" value="提交留言"></p>
</form>
<%
request.setCharecterEncoding("utf-8");
String username = request.getParameter("username");
String message = request.getParameter("message");
//获取留言对象
Vector<String> book = (Vector<String>)application.getAttribute("messageBook");
//判断是否有留言,分别处理
if (book == null) book = new Vector<String>;
if (username != null && message != null) {
String info = username + "#" + message;
book.add(info);
application.setAttribute("messageBook",book);
}
//显示所有留言
if (book.size()>0) {
for (String msg : book) {
String[] arr = msg.split("#");
out.print("<p>姓名:"+arr[0]+"<br>留言:"+arr[1]+"</p>");
}
} else {
out.print("还没有留言");
}
%>
pageContext
pageContext即页面上下文对象,表示当前页面运行环境,用于获取当前JSP页面的相关信息,作用范围为当前JSP页面,可以访问当前JSP页面所有内置对象,还提供存取页面域属性的方法。
方法 | 描述 |
---|---|
ServletRequest getRequest() | 获取当前页面请求对象 |
ServletResponse getResponse() | 获取当前页面响应对象 |
HttpSession getSession() | 获取和当前页面有联系的会话对象 |
ServletConfig getServletConfig() | 获取当前页面的ServletConfig对象 |
ServletContext getServletContext() | 获取当前页面的运行环境application对象 |
Object getPage() | 获取当前页面的Servlet实体page对象 |
Exception getException() | 获取当前页面的异常对象,需要设置该页面的isErrorPage属性为true |
JspWriter getOut() | 获取当前页面的输出流out对象 |
方法 | 描述 |
---|---|
Object getAttribute(String name,int scope) | 获取范围为scope,名为name的属性对象 |
void setAttribute(String name,Object value, int scope) | 以键值对的方式存储scope范围域属性 |
void removeAttribute(String name, int scope) | 从scope范围移除名为name的属性 |
Enumeration getAttributeNamesInScope(int scope) | 从scope范围中获取所有属性的名称 |
scope参数有4个常量:
PAGE_SCOPE=1代表页面域,REQUEST_SCOPE=2代表请求域
SESSION_SCOPE=3代表会话域,APPLICATION_SCOPE=4代表应用域
Servlet有关的内置对象
page
page对象即this,代表JSP被翻译后的Servlet,可以调用Servlet类所定义的方法。一般很少使用。
config
config对象即页面配置对象,表示当前JSP页面翻译后的Servlet的ServletConfig对象,存储一些初始的数据结构。一般很少用到。
Error有关的内置对象
exception对象。当JSP页面有错误产生异常,exception对象就用来对这个异常做处理。
如果一个JSP页面要应用此对象,必须将此页面中的page指令的isErrorPage属性设置为true,否则无法编译。
exception对象是java.lang.Throwable对象
JSP的四种作用域
对象的生命周期和可访问性称为作用域(scope)。
- 页面域,生命周期是指页面执行期间。存储在页面域的对象只对于它所在的页面是可访问的
- 请求域,生命周期是指一次请求过程,包括请求被转发(forward)或者被包含(include)的情况。存储在请求域中的对象只有在此次请求过程中才可以被访问
- 会话域,生命周期是指某个客户端和服务器所连接期间。客户端在第一次访问时创建会话,在会话过期或者用户主动退出,会话结束。存储在其中的属性在整个会话期间都可以被访问
- 应用域,生命周期是指从服务器开始执行服务到服务器关闭为止。存储在应用域中的对象在整个程序运行期间都可以被所有JSP和Servlet共享访问,需要注意存储数据的大小和安全,否则可能会造成服务器负载过重,产生线程安全问题