①手动去设置HTTP响应行中的状态码,这里用到了response的setStatus(int sc);这个方法
package com.itheima.line;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 StatusServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//手动去设置HTTP响应行中的状态码response.setStatus(1014);//这里的状态码设置为1014}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
建立的工程如下:
最后的结果如下:
②通过response设置响应头
addHeader(String name, String value)
addIntHeader(String name, int value)
addDateHeader(String name, long date)
setHeader(String name, String value) 重点
setIntHeader(String name, int value)
setDateHeader(String name, long date)
其中,add表示添加,而set表示设置
为编辑之前的
package com.itheima.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 {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Date date = new Date();//设置响应头response.addHeader("name", "beyondyy");//若重复,则接着添加response.addHeader("name", "beyondwsq");response.addIntHeader("age", 19);response.addDateHeader("birthday", date.getTime());}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
编辑之后的结果:
同样的道理,add表示有相同的往后接着添加,set只保留一个,前面的会被后面的给取代。
③重定向(302)
老版
重定向:说白了就是,当你访问servlet1时,它没有资源,它给你说servlet2有,然后你就会再访问servlet2。
重定向 状态码 302
响应头 Location (代表重定向地址)
//servlet1
package com.itheima.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 {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//没有响应资源 告知客户端去重定向到servlet2//1.设置状态码为302response.setStatus(302);//2.设置一个响应头Locationresponse.setHeader("Location", "/WEB14/servlet2");}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
//servl2
package com.itheima.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 {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.getWriter().write("hello beyondsq...");}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
由于重定向的方法每次都那么写麻烦,javaEE进行封装成一个叫重定向的方法sendRedirect(url);
封装之后的servlet1
新版
//servlet1
package com.itheima.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 {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//没有响应资源 告知客户端去重定向到servlet2//封装成一个重定向的方法sendRedirect(url);response.sendRedirect("/WEB14/servlet2");}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
④定时刷新(跟重定向一样,就是多了一个时间延迟而已)
//refresh
package com.itheima.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 RefreshServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置定时刷新的头response.setHeader("refresh", "5;url=//www.baidu.com");//5s之后再跳转到url为百度的地址}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
⑤js完成页面的定时跳转
案例:5秒后自动跳转到百度
<!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);//1000ms表示1s变一次}
</script>
</head>
<body>恭喜你,注册成功,<span style="color:red" id="second">5</span>秒后跳转。如不跳转,请点击<a href="http://www.baidu.com">这里</a>
</body>
</html>
⑥通过response设置响应体
旧
//text
package com.itheima.content;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class TextServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置response查询的码表response.setCharacterEncoding("UTF-8");//通过一个头Content-Type告知客户端使用何种码表进行解码response.setHeader("Context-Type", "text/html;charset=UTF-8");//文本为HTML类型,编码为UTF-8 PrintWriter writer = response.getWriter();writer.write("原谅我这一生不羁放纵爱自由wsq");}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
工程创建
新(进行代码封装之后)
package com.itheima.content;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class TextServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//进行方法封装之后 response.setContentType("text/html;charset=UTF-8");PrintWriter writer = response.getWriter();writer.write("原谅我这一生不羁放纵爱自由wsq");}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
牢记这行代码即可:
response.setContentType(“text/html;charset=UTF-8”);
⑦response的outputstream来获得服务器上的图片
响应头设置字节
ServletOutputStream getOutputStream()
获得字节流,通过该字节流的write(byte[] bytes)可以向response缓冲区中写入字 节,在由Tomcat服务器将字节内容组成Http响应返回给浏览器。
//byte
package com.itheima.content;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class ByteServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//使用response获得字节输出流ServletOutputStream out = response.getOutputStream();//获得服务器上的图片String realPath = this.getServletContext().getRealPath("a.jpg");//相对地址,相对web应用的InputStream in = new FileInputStream(realPath);int len = 0;byte[] buffer = new byte[1024];while((len = in.read(buffer))>0){out.write(buffer,0,len);}in.close();out.close(); }public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}