jsp内置对象对象是web容器创建的一组对象,不使用new关键词久可以使用的内置对象。 九大内置对象包括以下:
JspWriter类实例,是向客户端负责输出内容的。 常用方法如下:
举例子:
“`
<% out.println(“
“); out.println(“床前明月光 ”); out.println(“疑是地上霜 ”); out.flush(); //out.clear();//这里会抛出异常。 out.clearBuffer();//这里不会抛出异常。 out.println(“举头望明月 ”); out.println(“低头思故乡 ”);
%>
缓冲区大小:<%=out.getBufferSize() %>byte<br>
缓冲区剩余大小:<%=out.getRemaining() %>byte<br>
是否自动清空缓冲区:<%=out.isAutoFlush() %><BR>
“`
客户端的请求被封装在request对象中,通过它可以了解客户端的请求,然后作出响应,request请求具有request请求域。 常用方法:
<body>
<h1>用户注册</h1>
<hr>
<%
int number=-1;
//说明用户第一次访问页面,计数器对象还未创建
if(application.getAttribute("counter")==null)
{
application.setAttribute("counter", 0);
}
number = Integer.parseInt(application.getAttribute("counter").toString());
number++;
application.setAttribute("counter", number);
%>
<!-- <form name="regForm" action="request.jsp" method="post"> -->
<form name="regForm" action="response.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="favorite" value="read">读书
<input type="checkbox" name="favorite" value="music">音乐
<input type="checkbox" name="favorite" value="movie">电影
<input type="checkbox" name="favorite" value="internet">上网
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
<br>
<br>
<a href="request.jsp?username=李四">测试URL传参数</a>
<br>
<br>
<center>
您是第<%=number %>位访问本页面的用户。
</center>
</body>
request.jsp
<body>
<h1>request内置对象</h1>
<%
request.setCharacterEncoding("utf-8"); //解决中文乱码问题,无法解决URL传递中文出现的乱码问题。
request.setAttribute("password", "123456");
%>
用户名:<%=request.getParameter("username") %><br>
爱好 :<%
if(request.getParameterValues("favorite")!=null)
{
String[] favorites = request.getParameterValues("favorite");
for(int i=0;i<favorites.length;i++)
{
out.println(favorites[i]+" ");
}
}
%> <br>
密码:<%=request.getAttribute("password") %><br>
请求体的MIME类型:<%=request.getContentType() %><br>
协议类型及版本号: <%=request.getProtocol() %><br>
服务器主机名 :<%=request.getServerName() %><br>
服务器端口号:<%=request.getServerPort() %><BR>
请求文件的长度 :<%=request.getContentLength() %><BR>
请求客户端的IP地址:<%=request.getRemoteAddr() %><BR>
请求的真实路径:<%=request.getRealPath("request.jsp") %><br>
请求的上下文路径:<%=request.getContextPath() %><BR>
</body>
response对象包含了响应客户端请求的有关信息,它具有页面作用域,该页面的作用域只对该页面有效。常用方法:
请求重定向和请求转发:
一些基本概念:
它的的一些常用方法:
举个例子:
<body>
<h1>session内置对象</h1>
<hr>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d = new Date(session.getCreationTime());
session.setAttribute("username", "admin");
session.setAttribute("password", "123456");
session.setAttribute("age", 20);
//设置当前session最大生成期限单位是秒
//session.setMaxInactiveInterval(10);//10秒钟
%>
Session创建时间:<%=sdf.format(d)%><br>
Session的ID编号:<%=session.getId()%><BR>
从Session中获取用户名:<%=session.getAttribute("username") %><br>
<a href="session_page2.jsp" target="_blank">跳转到Session_page2.jsp</a>
</body>
session的生命周期:
常用方法:
举个例子:
<body>
<h1>application内置对象</h1>
<%
application.setAttribute("city", "北京");
application.setAttribute("postcode", "10000");
application.setAttribute("email", "lisi@126.com");
%>
所在城市是:<%=application.getAttribute("city") %><br>
application中的属性有:<%
Enumeration attributes = application.getAttributeNames();
while(attributes.hasMoreElements())
{
out.println(attributes.nextElement()+" ");
}
%><br>
JSP(SERVLET)引擎名及版本号:<%=application.getServerInfo() %><br>
</body>
page对象就是指当前jsp页面本身,有点像this指针,它是java.lang.Object类的实例。常用的方法就是Object 类的方法。
方法:
它是在一个servlet初始化时,jsp页面用它传递信息,比如servlet初始化参数;以及服务器的有关信息。
即异常对象。如果一个jsp想要用此对象,就必须把isErrorPage 设为true.
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" isErrorPage="true" %>
<%
九大内置对象,讲解完毕,感谢大家,后一篇文章会讲述除了jsp的九大内置对象其他内容。