码迷,mamicode.com
首页 > 其他好文 > 详细

Respone笔记

时间:2018-01-20 18:52:10      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:gpo   手动   https   import   定向   pack   one   web   http   

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

标签:gpo   手动   https   import   定向   pack   one   web   http   

原文地址:https://www.cnblogs.com/jepson6669/p/8321393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!