reponse設定響應頭
阿新 • • 發佈:2018-12-21
在web應用的src包下新建servlet,同時設定該servlet的url
servlet檔案中的程式碼如下
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class responseAboutHttp */ public class responseAboutHttp extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public responseAboutHttp() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // 1.通過setStatus設定響應行 response.setStatus(200); // 2.通過setHeader設定響應頭 response.setHeader("name", "malihua"); // 3.通過addHeader新增響應頭 response.addHeader("name", "yangbaoyu"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
在tomcat中執行此web應用抓包可得:
由此設定了響應行以及設定和添加了響應頭,設定響應行的狀態嗎為200,設定響應頭的key為name,value為malihua另外還添加了key的value