一、概述
Servlet可以访问由Servlet容器提供的ServletContext、ServletRequest、ServletResponse等对象,那么在JSP中,如何访问?对于JSP,这些对象称为内置对象或者隐式对象,JSP不需要做任何声明就可以直接在脚本代码引用这些对象。
隐式对象可分为四大类:1、输入\输出对象;2、作用域通信和控制对象;3、Servlet相关对象;4、错误处理对象。
JSP隐式对象类型
二、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动作..."); %>
response.getWriter();//返回可以向客户端输出字符对象; response.setContentType("text/html;chaset=utf-8");//设置响应MIME类型 response.sendRedirect("reg.jsp");//请求重定向,调整到指定页面
out.print输出流: <% out.print("jsp页面元素、隐式对象及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>
<% //存储在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>
<%@ page isELIgnored="true" %>Throwable提供的方法
<%@ page errorPage="error.jsp" %>//如果此页面发生异常,则把异常信息提交给error.jsp去处理 <body> <%=5/0%> </body>error.jsp
<%@ page isErrorPage="true" %> <body> 发生了异常,信息如下: <%=exception.toString()%><br> <%=exception.getMessage()%> </body>