一、概述
Servlet可以访问由Servlet容器提供的ServletContext、ServletRequest、ServletResponse等对象,那么在JSP中,如何访问?对于JSP,这些对象称为内置对象或者隐式对象,JSP不需要做任何声明就可以直接在脚本代码引用这些对象。
隐式对象可分为四大类:1、输入\输出对象;2、作用域通信和控制对象;3、Servlet相关对象;4、错误处理对象。
JSP隐式对象类型
二、JSP九大隐式对象详解
- 输入\输出对象
1、request
request对象中封装客户端请求数据信息,通过它可以获取客户请求方法、协议、参数名及参数值等信息,它提供了存储和获取属性的方法;
我以用户注册信息为例,当用户在注册页面填写完账号、密码、爱好时,点击提交,将用户的注册信息及请求信息显示在页面中。
注册页面reg.jsp
显示页面all.jsp
<b style="color: red;">注册信息:</b>
账号:<%=session.getAttribute("username") %>
<%
String password=new String(request.getParameter("password").getBytes("ISO-8859-1"),"utf-8");
application.setAttribute("password", password);//将获取password的属性值存储到application对象中
%>
密码:<%=application.getAttribute("password")%>//取出application对象中的属性值
爱好:
<%
String [] hobby=request.getParameterValues("hobby");
for(String str : hobby){
%>
<%=str%>
<% } %>
<b style="color: red;">请求信息:</b>
获取URI:<%=request.getRequestURI()%>
获取URL:<%=request.getRequestURL()%>
获取http请求方法:<%=request.getMethod()%>
获取http协议版本:<%=request.getProtocol()%>
获取ip地址:<%=request.getRemoteAddr()%>
获取站点名称:<%=request.getContextPath()%>
out.print输出流:
<%
out.print("jsp页面元素、隐式对象及jsp动作...");
%>
2、response
response对象包含了响应客户请求信息,但在JSP中很少直接使用,response具有页面作用域,即访问一个页面时,该页面的response对象只能对这次访问有效,其它页面的response对象对当前页面无效。
response.getWriter();//返回可以向客户端输出字符对象;
response.setContentType("text/html;chaset=utf-8");//设置响应MIME类型
response.sendRedirect("reg.jsp");//请求重定向,调整到指定页面
3、out
out对象是JSPWriter类的实例,用于向JSP页面输出内容,如下:
out.print输出流:
<%
out.print("jsp页面元素、隐式对象及jsp动作...");
%>
- 作用于通信对象
1、Session
Session对象是基于会话的,代表服务器与客户端所建立的会话,当需要在不同的JSP中保留客户信息的情况下使用,以便服务器跟踪每个用户的操作状态。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//解决psot请求乱码问题
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;chaset=utf-8");
PrintWriter out = response.getWriter();
//获取账号
String username=new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8");
HttpSession session=request.getSession(); //创建session对象,
session.setAttribute("username",username);//将获取的username属性值保存到session中
//页面转向
request.getRequestDispatcher("/user/alluser.jsp").forward(request, response);
}
将Servlet中保存在session对象中的参数值取出显示在JSP页面中,这样当选择导航栏中的不同页面时,用户信息在30分钟之内一直存在,实现会话跟踪。<div class="menu">
<ul>
<li><a href="#"><%=session.getAttribute("username") %>,欢迎您...</a></li>
<li><a href="${pageContext.request.contextPath}/user/alluser.jsp">用户管理</a></li>
<li><a href="${pageContext.request.contextPath}/user/dept.jsp"">部门管理</a></li>
<li><a href="${pageContext.request.contextPath}/login.jsp">退出</a></li>
</ul>
</div>
2、Application
Application对象基于服务器的,作用域整个应用程序,所有的客户端都可以共享该对象,从而实现用户之间的数据共享,因此可存放全局变量,服务器的启动和关闭决定了Application生命周期,所以每一用户对此对象属性进行操作,都会影响其他用户对此属性的访问,它是ServletContext的实例。
3、PageContext
PageContext对象是基于页面的,作用域整个页面,用户可以访问页面内的所有隐式对象,他的作用范围仅在当前JSP页面。
pageContext对象提供4个属性所对应的4个作用域
<%
//存储在pageContext对象中属性的作用域
pageContext.setAttribute("zh","中国",pageContext.PAGE_SCOPE);
//存储在request对象中属性的作用域
pageContext.setAttribute("en","英国",pageContext.REQUEST_SCOPE);
//存储在session对象中属性的作用域
pageContext.setAttribute("us","美国",pageContext.SESSION_SCOPE);
//存储在application对象中属性的作用域
pageContext.setAttribute("jp","日本",pageContext.APPLICATION_SCOPE);
%>
国家地区:
<%=pageContext.getAttribute("zh")%>
<%=request.getAttribute("en")%>
<%=session.getAttribute("us")%>
<%=application.getAttribute("jp")%>
pageContext对象提供的方法<label style="color: red;">本周电影排行榜:</label><p>
<%
pageContext.setAttribute("movie", "《澳门风云》");
pageContext.setAttribute("ReleaseDate", "2016-02-16");
pageContext.setAttribute("actor", "周润发,刘德华,张学友");
//删除属性名称为movie属性对象
//pageContext.removeAttribute("movie");
%>
电影:<%=pageContext.getAttribute("movie")%>,
日期:<%=pageContext.getAttribute("ReleaseDate")%>,
主演:<%=pageContext.getAttribute("actor")%>,<p>
返回属性范围:<%=pageContext.getAttributesScope("movie") %><br>
查找所有范围内属性名称为actor的对象:<%=pageContext.findAttribute("actor") %><p>
- Servlet相关对象
1、page
page对象代表JSP本身,表示与JSP对应的Servlet类的实例,page指令的extends属性用于显示指定Servlet类。
2、config
config实现了javax.servlet.ServletConfig接口,用来存储与JSP相关的初始化数据,ServletConfig方法如下:
- 错误处理对象
1、exception
exception对象是java.lang.Throwable类的实例,只适用于处理错误的JSP页面,在某个JSP页面抛出异常时,将转发JSP错误处理页面,如exception对象,必须在page指令中指定,语法如下:
<%@ page isELIgnored="true" %>
Throwable提供的方法 示例:当reg.jsp页面引发异常时,会将此异常信息转交给error.jsp页面处理
reg.jsp
<%@ page errorPage="error.jsp" %>//如果此页面发生异常,则把异常信息提交给error.jsp去处理
<body>
<%=5/0%>
</body>
error.jsp<%@ page isErrorPage="true" %>
<body>
发生了异常,信息如下:
<%=exception.toString()%><br>
<%=exception.getMessage()%>
</body>