JSP的隐式对象

隐式对象:在JSP页面可以直接使用的Java对象

3.1 常见JSP隐式对象的变量名及类型

对象类型有效范围
request(输入和输出对象)javax.servlet.http.HttpServletRequset请求范围
response(输入和输出对象)javax.servlet.http.HttpServletResponse页面范围
out(输入和输出对象)javax.servlet.jsp.JspWriter页面范围
pageContext(作用域对象)javax.servlet.jsp.PageContext页面范围
session(作用域对象)javax.servlet.http.HttpSession会话范围
application(作用域对象)javax.servlet.ServletContext应用程序范围
page(servlet对象)javax.servlet.jsp.HttpJspPage页面范围
config(servlet对象)javax.servlet.ServletConfig页面范围
exception(错误对象)javax.lang.Throwable只在错误页面有效

3.2 隐式对象的含义及应用

3.2.1 request对象

封装了用户提交的庆区信息,使用方式等同于在Servlet中的使用方式

主要方法:

String getParamter(String name);
//返回name指定参数的参数值
String name = request.getParamter("name");
void setCharacterEncoding(String encoding);
//设置请求的字符的编码方式
request.setCharacterEncoding("utf-8");
RequestDispatcher getRequestDidpatcher(String url);
//获得请求分发器对象
request.getRequestDipatcher("forward.jsp").forward(request,respone);

示例:(example1.jsp通过表单想tree.jsp提交信息;tree.jsp通过request对象获得表单提交的信息,包括text的值以及按钮的值)

resquest.jsp:
<body bgcolor=green>
    <font size=1>
        <form action="tree.jsp" method=post name=form>
            <input type="text" name="boy">
            <input type="submit" value="Enter" name="submit">
        </form>
    </font>
</body>
req_tree.jsp:
<body bgcolor=green>
    <font size=1>
        <p>获取文本框提交的信息:
        <%String textContent = request.getParameter("boy");%>
        <br>
        <%=textContent %>
        <p>获取按钮的名字:
        <%String buttonName = request.getParameter("submit"); %>
        <br>
        <%=buttonName %>
    </font>
</body>

3.2.2 response对象:对客户端的请求做出响应

示例:(在example2.jsp单击yes选择将当前页面保存为一个word文档是,JSP页面动态的改变contentType属性值为application/msword;客户端的浏览器会提示用户用word格式显示页面)

response.jsp:
<body bgcolor=cyan>
    <font size=1>
        <p>我正在学习response对象
        <br>setContentType方法
        <p>将当前页面保存为word文档吗
        <form action="" method="get" name=form>
            <input type="submit" value="yes" name="submit">
        </form>
        <%String str = request.getParameter("submit");
            if(str==null){
                str="";
            }
            if(str.equals("yes")){
                response.setContentType("application/msword;charset=UTF-8");
            }
        %>
    </font>
</body>

3.2.3 session对象

1.session对象的ID:

示例:(客户在服务器的三个页面之间进行连接,只要不关闭浏览器,三个页面的session对象完全相同;从session.jsp到sess_tom.jsp再连接sess_jerry.jsp)

session.jsp:
<p>
    <%String s = session.getId(); %>
    <p>您的session对象的id是:
    <br>
    <%=s %>
    <p>输入您的姓名连接到sess_tom.jsp
    <form action="sess_tom.jsp" method=post name=form>
        <input type="text" name="boy">
        <input type="submit" name=submit value="送出">
    </form> 
sess_tom.jsp:
<p>我是sess_tom页面
    <%String s =session.getId(); %>
    <p>您在sess_tom页面中的session对象的id是:
    <%=s %>
    <p>单击超链接,连接到sess_jerry页面
    <a href="sess_jerry.jsp">
    <br>欢迎来到sess_jerry
    </a>
sess_jerry.jsp:
<body>
    <p>我是sess_jerry页面
    <%String s =session.getId(); %>
    <p>您在sess_jerry页面中的session对象的id是:
    <%=s %>
    <p>单击超链接,连接到session页面
    <a href="session.jsp">
    <br>欢迎来到session
    </a>
</body>

2.session对象与URL重写

实现URL重写:

String str = response.encodeRedirectURL("jerry.jsp")
    <%=str%>

示例:(t如果客户不支持Cookie,则可在下面的例子中将上例中的页面实行URL重写)

session.jsp:
<p>您的session对象的id是:
    <%
        String s = session.getId(); 
        String str = response.encodeRedirectURL("sess_tom.jsp");
    %>
    <p>您的session对象的id是:
    <br>
    <%=s %>
    <p>您向sess_tom.jsp写入的信息是:
    <%=str %>
    <form action="<%=str %>" method=post name=form>
        <input type="text" name="boy">
        <input type="submit" name=submit value="送出">
    </form> 
sess_tom.jsp:
<p>我是sess_tom页面
    <%
        String s =session.getId();
        String str = response.encodeRedirectURL("sess_jerry.jsp");
    %>
    <p>您在sess_tom页面中的session对象的id是:
    <%=s %>
    <p>您向sess_jerry页面写入的信息是:
    <br>
    <%=str %>
    <p>单击超链接,连接到jerry
    <a href="<%=str %>">
    <br>欢迎来到sess_jerry
    </a>
sess_jerry.jsp:
<p>我是sess_jerry页面
    <%
        String s =session.getId(); 
        String str = response.encodeRedirectURL("session.jsp"); 
    %>
    <p>您在sess_jerry页面中的session对象的id是:
    <%=s %>
    <p>您向session.jsp写入的信息是:
    <%=str %>
    <p>单击超链接,连接到session页面
    <a href="<%=str %>">
    <br>欢迎来到session
    </a>

3.2.4 application对象:用于记载所有访问该应用程序的客户端信息

常用方法:

(1)将参数object指定的对象obj添加到application,并未添加的对象指定了一个索引关键词
    public void setAtttibue(String key,Object obj)
(2)获取application对象中含有关键字是key的对象
    public Object getAttibue(String key)
(3)产生一个枚举对象,该枚举对象可以使用nextElements遍历application对象所含有的全部对象
    public Enumeration getAttributeNames()
(4)从当前application对象中删除关键字是key的对象
    public void removeAttribue(String key)
(5)获取Servlet编译器的当前版本信息
    public String getServletInfo()

3.2.5 out、page、pageContext对象

(1)out对象:是对javax.servlet.jsp.JspWriter的实例,用于发送到响应中

 

代码实例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
    <title>out隐式对象演示</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="keywords1,keywords2,keywords3">
    <meta http-equiv="description" content="This is my page">
    <!-- 
    <link rel="stylesheet" type="text/css" href="style.css">
     -->
</head>
<body>
    <%out.print("演示out隐式对象方法的使用"); %>
    <%
        int getBufferSize=out.getBufferSize();
        int getRemaining = out.getRemaining();
        out.print("当前缓冲区的大小:"+getBufferSize+"<br/>");
        out.print("当前可使用缓冲区的大小:"+getRemaining+"<br/>");
        /* out.clear();
        out.close(); */
    %>  
</body>
</html>

(2)page对象:表示当前一个JSP页面

(3)pageContext对象:javax.servlet.jsp.pageContext的实例;代表JSP页面的运行环境;不仅封装了对其他八大隐式对象的引用,自身还是一个作用域

功能:可以存取其他隐式对象;可以对其他四大作用域空间进行数据的存取;进行页面的转发和包含。

 

 

  

代码实例:

<%
     pageContext.setAttribute("name","request_mrchi",pageContext.REQUEST_SCOPE);
     pageContext.setAttribute("name", "session_mrchi",pageContext.SESSION_SCOPE);
    %>
    <h1>显示request作用域的值</h1>
    <h1><%=pageContext.getAttribute("name", pageContext.REQUEST_SCOPE) %></h1>
    <h1>显示session作用域的值</h1>
    <h1><%=pageContext.getAttribute("name", pageContext.SESSION_SCOPE) %></h1>
    <h1>pageContext的findAttribute方法从小到大依次访问</h1>
    <h1><%=pageContext.findAttribute("name") %></h1>

3. 四大作用域比较(从高到低)

1.应用程序作用域(application):所有属性被访问者共享

2.会话作用域(session):属性被同一会话的不同请求共享

3.请求作用域(request):属性被页面相同的请求的服务共享

4.页面作用域(page):属性仅在一个页面中共享

代码实例:(分别给四个作用域对象设置计数器属性)

scope.jsp:
<%
    if(application.getAttribute("applicationCount")==null){
        application.setAttribute("applicationCount", 1);
    }else{
        Integer applicationCount = (Integer)application.getAttribute("applicationCount");
        applicationCount++;
        application.setAttribute("applicationCount", applicationCount);
    }
    if(session.getAttribute("sessionCount")==null){
        session.setAttribute("sessionCount", 1);
        Integer sessionCount = (Integer)session.getAttribute("sessionCount");
        sessionCount++;
        session.setAttribute("sessionCount", sessionCount);
    }
    %>
    <%
    if(request.getAttribute("requestCount")==null){
        request.setAttribute("requestCount", 1);
    }else{
        Integer requestCount = (Integer)request.getAttribute("requestCount");
        requestCount++;
        request.setAttribute("requestCount", requestCount);
    }%>
    <% 
    if(pageContext.getAttribute("pageCount")==null){
        pageContext.setAttribute("pageCount", 1);
    }else{
        Integer pageCount = (Integer)pageContext.getAttribute("pageCount");
        pageCount++;
        pageContext.setAttribute("pageCount", pageCount);
    }
    %>
    applicationCount=<%=application.getAttribute("applicationCount") %><br>
    sessionCount=<%=session.getAttribute("sessionCount") %><br>
    requestCount=<%=request.getAttribute("requestCount") %><br>
    pageCount=<%=pageContext.getAttribute("pageCount") %><br>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值