JSP 九大内置对象及四个作用域

来源:互联网 发布:网络视频传输套接字 编辑:程序博客网 时间:2024/06/07 03:49

第一节:Jsp 九大内置对象及四大作用域概述

在 Jsp 开发中,Jsp 提供了 9 个内置对象,这些内置对象将由容器为用户进行实例化,用户直接使用即可。这个
9 个内置对象分别是:pageContext,request,response,session,application,config,out,page,exception;常用的是前面 5
个,需要熟练掌握;
在 Jsp 开发中,可以保存数据,Jsp 提供了四种数据保存范围;分别是 page,request,session,application;

第二节:Jsp 四大作用域

Page 范围:只在一个页面中保存数据; javax.servlet.jsp.PageContext(抽象类)

<body><%    //设置两个request范围的数据key-->value    pageContext.setAttribute("name", "page王二小");    pageContext.setAttribute("age", 12);%><%     String name=(String)pageContext.getAttribute("name");    Integer age=(Integer)pageContext.getAttribute("age");%><font><%=name %></font><font><%=age %></font></body>

Request 范围:只在一个请求中保存数据; javax.servlet.http.HttpServletRequest(接口)

requestScope.jsp

<body><%    //设置两个request范围的数据key-->value    request.setAttribute("name", "request王二小");    request.setAttribute("age", 12);%><jsp:forward page="requestTarget.jsp"></jsp:forward></body>

requestTarget.jsp

<%@ page import="java.util.*" %><body><%     //取值,只在目标forward页面可以取到值    String name=(String)request.getAttribute("name");    Integer age=(Integer)request.getAttribute("age");%><%    //获取头信息    Enumeration enu=request.getHeaderNames();    while(enu.hasMoreElements()){        String headerName=(String)enu.nextElement();        String headerValue=request.getHeader(headerName);%>    <h4><%=headerName %>&nbsp;&nbsp;&nbsp;<%=headerValue %></h4><%    }%><font><%=name %></font><font><%=age %></font></body>

Session 范围:在一次会话范围中保存数据,仅供单个用户使用;javax.servlet.http.HttpSession(接口)
sessionScope.jsp

<body><%    //设置两个request范围的数据key-->value    session.setAttribute("name", "session王二小");    session.setAttribute("age", 12);%><h1>session值设置完毕</h1></body>

sessionTarget.jsp

<body>    <%        //取值,直接通过服务器就可以取值,当前会话不关闭就一直可以接受,默认30分钟内有效。        String name = (String) session.getAttribute("name");        Integer age = (Integer) session.getAttribute("age");    %>    <font><%=name%></font>    <font><%=age%></font></body>

Application 范围:在整个服务器上保存数据,所有用户共享;javax.servlet.ServletContext(接口)

applicationScope.jsp

<body><%    //设置两个application范围的数据key-->value    application.setAttribute("name", "application王二小");    application.setAttribute("age", 12);%><h1>application值设置完毕</h1></body>

applicationTarget.jsp

<body>    <%        //保存在服务器中,所有用户共享        String name = (String) application.getAttribute("name");        Integer age = (Integer) application.getAttribute("age");    %>    <font><%=name%></font>    <font><%=age%></font></body>

第三节:response 对象

Response 内置对象和 request 内置对象是相对应的,response 内置对象用于响应客户请求,向客户端输出信息;
javax.servlet.HttpServletResponse 接口
1,自动刷新应用
2,页面重定向应用 客户端跳转
3,操作 cookie 应用 post get 方法比较 post 放数据包里 get 放 Url 后面 get 数据量小,不安全;
4,cookie 和 session 的比较 cookie 信息是存客户端的,session

<body><%    //每隔一秒刷新一次页面    response.setHeader("refresh","1");    //获取当前时间    Date myDate = new Date();%>当前时间:<%=myDate.toLocaleString() %></body>
<body><%    //重定向,客户端跳转    response.sendRedirect("index.html");%></body>

第四节:out 对象

Out 内置对象主要用来向客户端输出各种类型的数据,同时还可以管理应用服务器上的输出缓冲区。所以 out 内
置对象的方法是向客户端输出数据和管理缓冲区; 底层 javax.servlet.jsp.JspWriter 抽象类

<body><%     out.println("<h1>");    out.println("hello JSP");    out.println("</h1>");%></body>
<body>    <%        int totalbuffer=out.getBufferSize(); //获取总共缓冲区的大小        int remainbuffer=out.getRemaining(); //获取未使用的缓冲区的大小        int usebuffer=totalbuffer-remainbuffer;        out.println("总共缓冲区的大小:"+totalbuffer);        out.println("未使用的缓冲区的大小"+remainbuffer);        out.println("使用的缓冲区大小"+usebuffer);    %></body>

第五节:config 对象

Config 内置对象是 JSP 页面通过 JSP 容器进行初始化时被传递的对象。javax.servlet.ServletConfig 。在 Servlet
初始化的时候,JPS 引擎通过 config 向它传递信息。这种信息可以是属性名和属性值匹配的参数,也可以是通过
ServletContext 对象传递的服务器的有关信息;

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    id="WebApp_ID" version="2.5">    <display-name>HeadfirstJspServletChap03</display-name>    <welcome-file-list>        <welcome-file>index.html</welcome-file>        <welcome-file>index.htm</welcome-file>        <welcome-file>index.jsp</welcome-file>        <welcome-file>default.html</welcome-file>        <welcome-file>default.htm</welcome-file>        <welcome-file>default.jsp</welcome-file>    </welcome-file-list>    <servlet>        <servlet-name>init</servlet-name>        <jsp-file>/sysInit.jsp</jsp-file>        <init-param>            <param-name>jdbcName</param-name>            <param-value>com.mysql.jdbc.Driver</param-value>        </init-param>        <init-param>            <param-name>dbUrl</param-name>            <param-value>jdbc:mysql://localhost:3306/db_xx</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>init</servlet-name>        <url-pattern>/init</url-pattern>    </servlet-mapping></web-app>

sysInit.jsp

<body><%    String jdbcName=config.getInitParameter("jdbcName");    String dbUrl=config.getInitParameter("dbUrl");%><h1>驱动名称:<%=jdbcName %></h1><h1>连接地址:<%=dbUrl %></h1></body>

第六节:exception 对象

Exception 内置对象用来处理 JSP 文件在执行时发生的所有异常,它是 java.lang.Throwable 类的一个对象。

exception.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page errorPage="error.jsp"%> //将异常信息传给error.jsp去处理<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><%    int a=1;    int b=0;    out.println(a/b);%></body></html>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page isErrorPage="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><%    if(exception!=null){        out.println("程序错误信息:");        out.println(exception.getMessage());    }%></body></html>

第七节:pageContext 对象

pageContext 内置对象是一个比较特殊的对象。它相当于页面中所有对象功能的集合,即使用它可以访问到本页面
中所有对象。pageContext 内置对象由 Jsp 容器创建并初始化,pageContext 对象提供了对 JSP 页面所有对象及控件
的访问。

<body><%    pageContext.setAttribute("name0", "pageInfo");    request.setAttribute("name1", "requestInfo");    session.setAttribute("name2", "sessionInfo");    application.setAttribute("name3", "applicationInfo");    out.println("使用pageContext<br/>");    out.println("page中的属性:"+pageContext.getAttribute("name0")+"</br>");    out.println("request中的属性:"+pageContext.getRequest().getAttribute("name1")+"</br>");    out.println("session中的属性:"+pageContext.getSession().getAttribute("name2")+"</br>");    out.println("application中的属性:"+pageContext.getServletContext().getAttribute("name3")+"</br>");%></body>
0 0