二、JSP之JSP内置对象

本文详细介绍了JSP内置对象的功能和使用方法,包括out、request、response等对象的应用场景,并对比了请求转发与请求重定向的区别,同时深入探讨了session和application对象在会话管理和全局数据共享中的作用。

一、JSP内置对象简介

Web容器创建的一组对象,不使用new关键字就可以使用的内置对象。

  • out
  • request
  • response
  • session
  • application
  • Page pageContext exception config

二、out对象

这里写图片描述


三、提交表单的方式

这里写图片描述

login.jsp :

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>My JSP 'login.jsp' starting page</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="dologin.jsp" name="loginForm" method="post">
    <table>
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="username"/></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="password" name="password"/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="登录"></td>
        </tr>
    </table>
</form>
</body>
</html>

dologin.jsp :

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'dologin.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <h1>登录成功</h1>
    <hr>
  </body>
</html>

四、request对象

这里写图片描述
这里写图片描述

reg.jsp :

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <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>
</html>

request.jsp :

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <h1>request内置对象</h1>
    <% 
       request.setCharacterEncoding("utf-8"); //解决中文乱码问题,无法解决URL传递中文出现的乱码问题。
//        解决URL传递中文出现的乱码: 在server.xml中的Connector标签下设置URIEncoding="utf-8"
       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]+"&nbsp;&nbsp;");
               }
            }
        %> <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>
</html>

五、response对象

这里写图片描述

response.jsp :

<%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=utf-8"%>
<%
    response.setContentType("text/html;charset=utf-8"); //设置响应的MIMI类型

    out.println("<h1>response内置对象</h1>");
    out.println("<hr>");
    //out.flush();

    PrintWriter outer = response.getWriter(); //获得输出流对象
    outer.println("大家好,我是response对象生成的输出流outer对象"); //如果不用out.flush(),那么PrintWriter输出的结果总是提前于内置对象的输出
//    response.sendRedirect("reg.jsp");//请求重定向
    //请求重定向
    //response.sendRedirect("request.jsp");
    //请求转发
    request.getRequestDispatcher("request.jsp").forward(request, response);
%>

六、请求转发与请求重定向

请求重定向是客户器端行为而请求转发是服务器端行为

这里写图片描述


七、session对象

这里写图片描述

这里写图片描述

session_page1.jsp :

<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

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

    <%--target="_blank"表示打开新窗口--%>
    <a href="session_page2.jsp" target="_blank">跳转到Session_page2.jsp</a>     

  </body>
</html>

session_page2.jsp :

<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <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的ID编号:<%=session.getId()%><BR>
    从Session中获取用户名:<%=session.getAttribute("username") %><br>
    Session中保存的属性有:<% 
                     String[] names =session.getValueNames();
                     for(int i=0;i<names.length;i++)
                     {
                        out.println(names[i]+"&nbsp;&nbsp;");
                     }

    %> <br>    
  </body>
</html>

浏览器不关闭,服务器就会保存用户sessions状态,多个页面可属于同一个session会话。
会话保存在服务器的内存里,保存着不同的用户的session。
本次会话的所有页面都关闭后再重新访问某个JSP或者Servlet将会创建新的会话

这里写图片描述

这里写图片描述


八、application对象

这里写图片描述
相当于静态变量(全局)

这里写图片描述
setAttribute方法参数第一个为字符串,第二个参数为需要保存的对象
使用application 实现计数器效果,在application中保存整型变量num,setAttribute(”counter”,num);


九、page对象

这里写图片描述

当前page页面对象的字符串描述:<%=page.toString() %><br>
结果:
当前page页面对象的字符串描述:org.apache.jsp.page_jsp@32ac7d17

十、pageContext对象和config对象

这里写图片描述
forward使当前页面跳转到另一个页面,地址栏不会放生变化,有点类似于请求转发。

pageContext对象的用法

这里写图片描述


十一、exception对象

这里写图片描述

errorPage: 指定异常页面,当发生异常时交由其处理
isErrorPage:设置该页面为处理异常的页面
当异常发生时,会跳转到异常处理页面,地址栏不会发生变化,相当于请求转发。

exception_test.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" errorPage="exception.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <h1>测试异常的页面</h1>
    <hr>

    <% 
      System.out.println(100/0); //抛出运行时异常,算数异常
    %>
  </body>
</html>

exception.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" isErrorPage="true" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <h1>exception内置对象</h1>
    <hr>

        异常的消息是:<%=exception.getMessage()%><BR>
        异常的字符串描述:<%=exception.toString()%><br>
  </body>
</html>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值