zoukankan      html  css  js  c++  java
  • JSP内置对象--response对象 (addCookie(),setHeader(),sendRedirect())

    服务器接收客户端请求:request

    服务器对客户端的回应:response

    javax.servlet.http的接口HttpServletResponse extends ServletResponse

    重要方法:

    void addCookie(Cookie cookie):向客户端增加cookie

    void setHeader(java.lang.String name,java.lang.String value):设置回应的头信息

    void sendRedirect(java.lang.String location) throws java.io.IOException:页面跳转

    • 设置头信息 response.setHeader("refresh","2")

    所有头信息都是随着请求和回应自动发送到服务器端或者客户端上去,在response中比较常用的头信息就是刷新的指令,可以完成定时刷新的功能。

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <%!    // 定义全局变量
        int count = 0 ;
    %>
    <%
        response.setHeader("refresh","2") ;    // 页面2秒一刷新
    %>
    <h3>已经访问了<%=count++%>次!</h3>
    </body>
    </html>

    除了定时,还有定时跳转的功能:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <h3>3秒后跳转到hello.htm页面</h3>
    <%
        response.setHeader("refresh","3;URL=hello.htm") ;    // 3秒后跳转到hello.htm页面
    %>
    </body>
    </html>

    这种跳转不是万能的,有时候就根本无法跳转,此时需要加上   如果没有跳转请按<a href="hello.htm">:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <h3>3秒后跳转到hello.htm页面,如果没有跳转请按<a href="hello.htm">这里</a>!</h3>
    <%
        response.setHeader("refresh","3;URL=hello.htm") ;    // 3秒后跳转到hello.htm页面
    %>
    </body>
    </html>

    对于这种跳转的头信息也可以通过HTML方式进行设置,HTML本身也可以设置头信息:

    <META HTTP-EQUIV="refresh" CONTENT="3;URL=hello.htm">
    response_demo03.htm:
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <META HTTP-EQUIV="refresh" CONTENT="3;URL=hello.htm">
    <body>
    <h3>3秒后跳转到hello.htm页面,如果没有跳转请按<a href="hello.htm">这里</a>!</h3>
    
    </body>
    </html>

    以上地址栏都发生了改变,所以都是客户端跳转。

    现在到底是使用response设置还是META设置?

    在web中,只要包含了动态页的,都需要web容器的支持,而所有的静态页直接使用即可。

    • 页面跳转 response.sendRedirect()

    在response对象中提供了一个sendRedirect()方法完成跳转:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <%
        response.sendRedirect("hello.htm") ;
    %>
    </body>
    </html>

    此时跳转成功,而且地址栏改变,这种跳转就称为客户端跳转,客户端跳转是无法传递request属性内容的。

    现在已经学习了jsp:forward, response.sendRedirect()两种跳转语句,那么区别是什么?

    1. jsp:forward

    特点:

    1. 服务器端跳转,跳转后地址栏不改变,可以传递request属性

    2. 属于无条件跳转,执行到之后立即跳转,跳转之前语句会执行,之后不执行。

    那么如果JSP中使用JDBC后,必须在跳转之前进行数据库的关闭,否则数据库再也无法关闭

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <%    System.out.println("============ forward跳转之前 =========") ;%>
        <jsp:forward page="hello.htm"/>
    <%    System.out.println("============ forward跳转之后=========") ;%>
    </body>
    </html>

    2. response.sendRedirect()

    特点:

    1. 客户端跳转,地址栏改变,不传递request属性

    2. 在所有语句都执行完后再执行跳转,所以数据库哪里关闭就无所谓了

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <%    System.out.println("============ forward跳转之前 =========") ;%>
        <%    response.sendRedirect("hello.htm") ;    %>
    <%    System.out.println("============ forward跳转之后=========") ;%>
    </body>
    </html>

    要想深入了解两种跳转,必须结合后面的MVC设计模式来看,现在掌握基本语法即可。

    • 操作Cookie response.addCookie(Cookie c)

    Cookie 是服务器端保存在客户端的信息,安全性差,javax.servlet.http.Cookie

    在一般站点都存在记住用户名这样一个操作,换个电脑无效,这就是cookie技术。

    设置cookie:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <%
        Cookie c1 = new Cookie("lxh","LiXingHua") ;
        Cookie c2 = new Cookie("mldn","www.MLDNJAVA.cn") ;
        response.addCookie(c1) ;
        response.addCookie(c2) ;
    %>
    </body>
    </html>

    取得cookie:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <%
        Cookie c[] = request.getCookies() ;    // 取得客户端的全部Cookie
        System.out.println(c) ;
        for(int x=0;x<c.length;x++){
    %>
            <h3><%=c[x].getName()%> --> <%=c[x].getValue()%></h3>
    <%
        }
    %>
    </body>
    </html>

    现在看来,所有的cookie都应该保存在客户端,那么关闭浏览器,开启新的浏览器应该也保存。可是并不是这样。

    因为没设置保存的时间,所以默认是在一个浏览器上保存的,如果浏览器关闭,则cookie消失。如果想在客户端保留一段时间,要使用setMaxAge()完成,将第一个文件改成:

    <%@ page contentType="text/html" pageEncoding="GBK"%>
    <html>
    <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
    <body>
    <%
        Cookie c1 = new Cookie("lxh","LiXingHua") ;
        Cookie c2 = new Cookie("mldn","www.MLDNJAVA.cn") ;
        c1.setMaxAge(100) ;
        c2.setMaxAge(100) ;
        response.addCookie(c1) ;
        response.addCookie(c2) ;
    %>
    </body>
    </html>

    response对象主要就是服务器对客户端的回应,对于cookie操作本身有安全隐患。

    如果要存放客户端信息的时候,都使用cookie保存即可。

    总结:

    1. response对象主要就是服务器对客户端的回应

    2. 可以通过setHeader()设置一个响应的头信息

    3. 通过response可以向客户端设置cookie,可以通过request取得客户端设置的全部cookie

    4. response对象的sendRedirect属于客户端跳转,而<jsp:forward>属于服务器端跳转

  • 相关阅读:
    Linux下Java安装与配置
    HYSPLIT模式简介及在线平台使用
    HYSPLIT模式简介及单机版使用图文教程
    有关气象类资料数据下载网址
    Excel图表编程应用大全(2):图表编程基础
    当装系统时遇到“选中的磁盘采用GPT分区形式”
    SQL函数
    Centos6.7 Redis3.2.8的主从搭建
    Centos 6.7 中 Redis-3.2.8的安装
    Mysql innodb_buffer_pool_size的研究
  • 原文地址:https://www.cnblogs.com/wujixing/p/4952253.html
Copyright © 2011-2022 走看看