jsp九种内置对象:request, reponse, out, session, application, config, pagecontext, page, exception.
1.request对象
代表了客户端的请求信息,是接受信息传送到电脑服务器的数据。主要用于接受通过HTML协议转送到服务器的数据(包括头信息、系统信息、请求方式以及请求参数等)。request对象的作用域为一次请求。
<form action="request.jsp">
username:<input type="text" name="user"/><br/>
password:<input type="password" name="pwd"/><br/>
<input type="submit" name="请求"/>
</form>
2.reponse对象
response 代表的是对客户端的请求做出动态的响应,主要是负责将电脑处理过的对象在传回到客户端。它只在jsp页面有效。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<left>
跳转页面到腾讯首页
</left>
</body>
</html>
3.out对象
out 对象用于在Web浏览器内输出信息,并且管理应用服务器上的输出缓冲区(清理缓存).在使用out对象输出数据时,可以对数据缓冲区进行操作,及时清除缓冲区的残余数据,为其他的输出让出缓冲空间。待数据输出完毕后,要及时关闭输出流.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<pre>
<%
out.println("<b>使用println()方法向客户端浏览器输出文字:</b>");
out.println("Hello!");
%>
</pre>
</body>
</html>
4.session对象
session对象指的是客户端与服务器的一次会话,session可以在应用程序的web页面间进行跳转时,保存用户状态直到关闭浏览器,客户端长时间不发出请求,session对象会自动消失。
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>session1</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<form id="form1" name="form1" method="post" action="session2.jsp">
<div align="center">
<table width="40%" border="0">
<tr>
<td width="36%"><div align="center">您的名字是:</div></td>
<td width="64%">
<label>
<div align="center">
<input type="text" name="name" />
</div>
</label>
</td>
</tr>
<tr>
<td colspan="2">
<label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
5.application对象
application对象可将所有应用中的共有数据保存在服务器中,在服务器启动时自动创建,在服务器停止时销毁。若不销毁,则application对象中保存的信息会在整个应用中都有效。与session对象相比,application对象生命周期更长,类似于系统的“全局变量”。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<%
Object obj=application.getAttribute("counter");
if (obj==null){
application.setAttribute("counter",new Integer(1));
out.println("该页面被访问了1次<br/>");
}else {
int countValue=new Integer(obj.toString());
countValue++;
out.println("该页面被访问了"+countValue+"次<br/>");
application.setAttribute("counter",countValue);//java会自动装箱
}
%>
</body>
</html>