美文网首页
response对象

response对象

作者: kylelin | 来源:发表于2014-08-05 11:15 被阅读73次

response对象的主要作用是对客户端的请求进行响应,将Web服务器处理后的结果发送给客户端。response对象属于javax.servlet.http.HttpServletResponse接口的示例。

response对象的常用方法

方法 描述
public void addCookie(Cookie cookie) 向客户端增加Cookie
public void setHeader(String name, String value) 设置回应的头信息
public void sendRedirect(String location) throws java.io.IOException 页面跳转

1. 设置头信息

定时刷新页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>标题</title>
</head>
<body>
    <%!
        int count = 0;
    %>
    <%
        response.setHeader("refresh", "1");
    %>
    <h1>已经访问了<%=count++ %></h1>
</body>
</html>

定时跳转页面,例如:3秒后跳转到百度首页
方式一、

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>标题</title>
</head>
<body>
    <%
        response.setHeader("refresh", "3;url=http://www.baidu.com");
    %>
</body>
</html>

方式二、

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="refresh" content="3;url=http://www.baidu.com">
    <title>标题</title>
</head>
<body>

</body>
</html>

2. 页面跳转
response.sendRedirect属于客户端跳转。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>标题</title>
</head>
<body>
    <%
        response.sendRedirect("http://www.baidu.com");
    %>
</body>
</html>

3. 操作Cookie

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>标题</title>
</head>
<body>
    <%
        Cookie c1 = new Cookie("name", "zhangsan");
        Cookie c2 = new Cookie("age", "25");
        c1.setMaxAge(10);//设置Cookie保存时间为10秒
        response.addCookie(c1);
        response.addCookie(c2);
    %>
</body>
</html>

相关文章

  • response入门

    response response对象表示对程序发出的http请求的响应。 response对象常用属性和方法 r...

  • Servlet学习3 -- Response对象和Servlet

    一.Response对象 Response的作用Response对象是用来设置HTTP响应消息的,包括了响应行,响...

  • 请求

    Request: request对象和response对象的原理1. request和response对象是由服务...

  • JavaWeb——Request&Response&am

    Request&Response原理 Request对象和response对象的原理 request和respon...

  • 6.3KOA 数据响应 Response

    数据响应 Response 获取 Response 对象 API详细地址 Response 别名 ctx.body...

  • JavaWeb——request

    1. request 对象和 response 对象的原理 request 和response 对象是由服务器创建...

  • Response

    Response对象

  • response对象

    response对象 response对象包含了相应客户请求的有关信息,但是在jsp中很少直接用到它。它是Http...

  • response对象

    response对象的主要作用是对客户端的请求进行响应,将Web服务器处理后的结果发送给客户端。response对...

  • Python之Requests库

    安装 pip install requests Response对象 Response对象包含服务器返回的所有信息...

网友评论

      本文标题:response对象

      本文链接:https://www.haomeiwen.com/subject/yacetttx.html