Respone笔记

Posted jepson6669

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Respone笔记相关的知识,希望对你有一定的参考价值。

1 设置定时刷新的头

//设置定时刷新的头
response.setHeader("refresh", "5;url=http://www.baidu.com");

2重定向的实现

1 servlet1代码

package header;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet1 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //没有响应 告知客户端去重定向到servlet2
        //1、设置状态码302
        //response.setStatus(302);
        //2、设置响应头Location
        //response.setHeader("Location", "/WEB14/servlet2");
        
        //封装成一个重定向的方法sendRedirect(url)
        response.sendRedirect("/WEB14/servlet2");
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

2 servlet2代码

package header;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet2 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().write("hello haohao...");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

3 手动设置状态码

//手动设置http响应行中的状态码
response.setStatus(404);

4 设置响应头

package header;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HeaderServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Date date = new Date();
        
        //设置响应头
        response.addHeader("name", "zhangsan");
        //response.addIntHeader("age", 28);
        //response.addDateHeader("birthday", date.getTime());
        
        response.addHeader("name", "lisi");
        
        response.setHeader("age", "28");
        response.setHeader("age", "50");
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

5 适用js完成页面定时跳转

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    window.onload = function(){
        var time = 5;
        var secondEle = document.getElementById("second");
        var timer = setInterval(function(){
            secondEle.innerHTML = time;
            time--;
            if(time==0){
                clearInterval(timer);
                location.href="http://www.baidu.com";
            }
                
        },1000);
    }
</script>
</head>
<body>
    恭喜你,注册成功,<span style="color:red" id="second">5</span>秒钟后跳转,如不跳转点击<a href="http://www.baidu.com">这里</a>!
</body>
</html>

 

以上是关于Respone笔记的主要内容,如果未能解决你的问题,请参考以下文章

爬取汽车之家新闻图片的python爬虫代码

Respone弹窗

学习笔记:python3,代码片段(2017)

field load respone data

chain.doFilter(request,respone) 报空指针

学习笔记17_网站异常和日志处理