JSP中的9大内置对象和作用、用法。

Jsp内置对象对象是web容器创建的一组对象,无需使用new关键词实例化,即可调用的内置对象。 
9大内置对象包括以下:

  • 1.输出对象out –JspWriter
  • 2.请求对象request –ServletRequest
  • 3.响应对象reponse –ServletResponse
  • 4.配置对象config –ServletConfig
  • 5.会话对象session –HttpSession
  • 6.应用程序对象application –ServlerContext
  • 7.页面对象page –HttpJspPage
  • 8.页面上下文对象pageContext –PageContext
  • 9.异常对象exception –Exception

1、out对象

JspWriter类实例,是向客户端负责输出内容的。 
常用方法如下:

  • println();
  • clear (),如果在flush()之后调用会抛出异常。
  • clearBuffer();
  • flush();
  • isAutoFlush();

例子:

out内置对象


<% 
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>    


“`

2、request对象

客户端的请求被封装在request对象中,通过它可以了解客户端的请求,然后作出响应,request请求具有request请求域。 
常用方法:

  • getParameter(String name)
  • getParamterValues(String name)
  • setAttribute(String name,Onject o)
  • getAttribute(string name)
  • getContetType();
  • getProtocol()
  • getServerName();

    举个例子:用户注册提交数据给request.jsp,在request.jsp页面根据request对象可以获取提交过来的数据。

    reg.jsp 注册jsp

    
    <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>
    
       
       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    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>
       
       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

3、response对象

response对象包含了响应客户端请求的有关信息,它具有页面作用域,该页面的作用域只对该页面有效。常用方法:

  • getCharacterEncoding()
  • setContentType();
  • getWriter();该方法打应输出流总是前于 out.println();
  • sendRedirect(String location)

请求重定向和请求转发:

  • 请求重定向:客户端行为:response.sendDirect();两次请求,前一次请求的请求对象不会保存,地址栏的url地址会发生改变
  • 请求转发:服务器行为,request.getResuestDispatcher().forward();一次请求,转发后请求对象会保存,地址栏url地址不会变。

4、session对象

一些基本概念:

  • session表示客户端与服务器的一次会话
  • web中session指的是用户在浏览某个网站,是进入网站到关闭浏览器这段时间
  • 它是保存在服务器的内存中,不同用户有不同的session
  • 它在第一个jsp页面被装载时自动创建,完成会话期管理。

它的的一些常用方法:

  • getCreationTime();
  • String getId();
  • setAttribute(String name,Object o);
  • getAttribute(String name);
  • String[] getValueNames();
  • int getMaxInactivieInterval();单位 秒
  • setMaxInactiveInterval();

举个例子:


<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>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

session的生命周期:

  • 创建: 当客户端第一次访问某个页面jsp或者servlet,服务器会创建一个 sessionId,每次客户端向服务器发送请求时,都会将sessionId携带过去,服务器会对sessionId进行校验。
  • 活动: 当客户端通过超链接打开新页面属于同一次会话;当浏览页面全部关闭,重新打开属于一次新的会话。
  • 销毁:调用sesson.invalidate();session过期,默认是30分钟;服务器重启;

5、application对象

  • application实现了用户数据共享,可存放全局变量。
  • application 开始于服务器的重启,终止于服务器的关闭
  • application 是ServletContext实例。

常用方法:

  • setAttribute(String ,Object);
  • getAttribute(String);
  • Enumeration getAttributeNames();
  • getServerInfo();返回Jsp 引擎名和版本号

举个例子:


<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>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

6、page对象

page对象就是指当前jsp页面本身,有点像this指针,它是Java.lang.Object类的实例。常用的方法就是Object 类的方法。

  • getClass()
  • hashCode();
  • equals();
  • copy();
  • clone()
  • toString();
  • notify();
  • notifyAll();
  • wait();

7、pageContext对象

  • pageContext 提供了对jsp页面所有的对象及名字空间的访问
  • 它可以取application 某一属性,也可以取session;
  • 相当于页面所有功能的集大成者。

方法:

  • getOut()
  • geSession();
  • getPage();
  • getReuest();
  • getResponse();
  • setAttribute();
  • getAttibute();
  • getAttributeScope();
  • forward();
  • include();

8、config对象

它是在一个servlet初始化时,jsp页面用它传递信息,比如servlet初始化参数;以及服务器的有关信息。

  • ServletContext getServletContext();
  • getInitParameter(String);
  • Enumeration getInitParameterNames();

9、exception对象

即异常对象。如果一个jsp想要用此对象,就必须把isErrorPage 设为true.

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" isErrorPage="true" %>
<%
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSP(Java Server Pages)是一种基于Java语言的服务器端网页开发技术,其包括九大内置对象和四大作用域。 九大内置对象是指在每一个JSP页面,系统自动为开发者创建的九个对象,分别是: 1. request对象:用于获取HTTP请求的信息,如请求参数、请求头等。 2. response对象:用于设置HTTP响应的信息,如设置返回的内容、重定向等。 3. out对象:用于向客户端输出内容,可以通过该对象在页面生成动态内容。 4. session对象:用于存储用户的会话信息,可以在不同的页面和请求之间共享数据。 5. application对象:用于存储全局的应用程序数据,可供所有用户访问。 6. config对象:用于获取当前JSP页面的配置信息,如JSP的初始化参数等。 7. page对象:代表当前JSP页面本身,可以用于调用页面的方法或属性。 8. exception对象:用于处理JSP页面的异常,可以获取异常的相关信息。 9. pageContext对象:包含了对其他八个内置对象的引用,可用于简化代码的编写。 四大作用域是指在JSP定义的存储数据的范围,分别是: 1. page作用域:数据的作用范围限定在当前JSP页面,即页面级别的作用域。 2. request作用域:数据的作用范围限定在一次HTTP请求与响应过程,可供同一请求的不同页面共享。 3. session作用域:数据的作用范围限定在用户的整个会话过程,可供不同请求和页面共享。 4. application作用域:数据的作用范围限定在整个Web应用程序,可供所有用户和所有请求共享。 通过合理的使用这九大内置对象和四大作用域,可以方便地处理页面间的数据传递和数据共享,提高JSP网页开发的效率和灵活性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值