Tomcat中Response对象的常用方法
- Response对象可以设置服务端返回给客户端的
响应头
,响应行
,响应体
的所有内容
package com.shanlei;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ResponseServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("this is post");
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("this is get");
response.setHeader("head", "info");
response.addHeader("city", "chengdu");
response.setHeader("Content-Type", "text/html");
response.getWriter().write("<h1>study hard</h1>");
}
}