`

JSP中使用response设置HTTP头信息

jsp 
阅读更多

使用response对象可以设置HTTP的头信息。
格式:response.setHeader("头信息名称","参数");
其中一个重要的头信息是:refresh(刷新)。

实例:每秒刷新一次页面,显示刷新次数

response01.jsp:
程序代码 程序代码

<%@page contentType="text/html;charset=GB2312" %>
<html>
  <head>
    <title>response01.jsp</title>
  </head>
 
  <body>
      <%!int i=0; %>
      <%
          //每秒刷新一次
          response.setHeader("refresh","1");
      %>
      <%=i++ %>
  </body>
</html>


使用HTTP头信息设置,我们还可以实现在规定的时间内跳转:
格式:response.setHeader("refresh","时间;URL=跳转页面地址");

实例:打开response02.jsp,2秒之后跳转到response03.jsp:

response02.jsp:
程序代码 程序代码

<%@page contentType="text/html;charset=GB2312" %>
<html>
  <head>
    <title>response02.jsp</title>
  </head>
 
  <body>
    <%
        response.setHeader("refresh","2;URL=response03.jsp");
    %>
    <h1>两秒后跳转到response03.jsp</h1>
    如果没有跳转,请点<a href="response03.jsp">这里</a>
  </body>
</html>


response03.jsp:
程序代码 程序代码

<%@page contentType="text/html;charset=GB2312" %>
<html>
  <head>
    <title>response03.jsp</title>
  </head>
 
  <body>
    <h1>Welcome to response03.jsp!</h1>
  </body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics