首页 > 代码库 > Servlet------>request和response

Servlet------>request和response

request代表了请求

response代表响应

HttpServletResponse

setStatus();----->发送状态码

setHeader();---->发送响应头

getWriter();和getOutputStream();------>发送数据

addHeader();---->添加一个头

containsHeader();--->包含几个头

如果不加:response.setHeader("Content-type","text/html;charset=UTF-8");

浏览器读出涓球,默认国标读,不是utf-8

package cn.itcaste.web.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/ResponseDemo1")
public class ResponseDemo1 extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//以什么编码发就用什么编码读
		response.setHeader("Content-type","text/html;charset=UTF-8");
		String data="http://www.mamicode.com/中国";
		OutputStream out=response.getOutputStream();
		out.write(data.getBytes("UTF-8"));
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

Servlet------>request和response