首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。