Request和Response
1 简介
- web服务器收到客户端的http请求,会针对每一个请求,分别创建一个用于代表请求的request对象和代表响应的response对象。
- request和response对象既然代表请求和响应,那么获取客户端提交的数据,只需要找request对象,而向容器输出对象,只需要找response对象。
2 HttpServletResponse
- HttpServletResponse对象代表服务器的响应,这个对象中封装了向客户端发送数据、发送响应头、发送响应状态码的方法。
- 响应消息头:
public void addDateHeader(java.lang.String name,long date)
public void addHeader(java.lang.String name, java.lang.String value)
public void addIntHeader(java.lang.String name, int value)
public boolean containsHeader(java.lang.String name)
public void setHeader(java.lang.String name, java.lang.String value)
public void setIntHeader(java.lang.String name,int value)
public void setDateHeader(java.lang.String name, long date)
- 响应消息正文:
public ServletOutputStream getOutputStream() throws java.io.IOException
public java.io.PrintWriter getWriter() throws java.io.IOException
- 示例:字节流输出数据查本地码表
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; /** * 2017/11/5 * 说明: */ public class ResponseDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "我爱你"; OutputStream out = response.getOutputStream(); out.write(data.getBytes());//String 的getBytes()默认在windows系统中默认上的是GBK } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>ResponseDemo1</servlet-name> <servlet-class>com.servlet.ResponseDemo1</servlet-class> </servlet> <servlet-mapping> <servlet-name>ResponseDemo1</servlet-name> <url-pattern>/responseDemo1</url-pattern> </servlet-mapping> </web-app>
- 示例:字节流输出数据查UTF-8
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; /** * 2017/11/5 * 说明: */ public class ResponseDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "我爱你"; OutputStream out = response.getOutputStream(); out.write(data.getBytes("utf-8"));//String 的getBytes()默认在windows系统中默认上的是GBK } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>ResponseDemo1</servlet-name> <servlet-class>com.servlet.ResponseDemo1</servlet-class> </servlet> <servlet-mapping> <servlet-name>ResponseDemo1</servlet-name> <url-pattern>/responseDemo1</url-pattern> </servlet-mapping> </web-app>
- 解决方案:
- ①手动修改浏览器的码表为UTF-8(不建议) http://jingyan.baidu.com/article/d169e186a4e826436611d81e.html
- ②向客户端输出一个meta标签,模拟响应消息头 (不建议,这种方式目前好像只适合于IE)
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; /** * 2017/11/5 * 说明: */ public class ResponseDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "我爱你"; OutputStream out = response.getOutputStream(); out.write("<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>".getBytes()); out.write(data.getBytes("utf-8"));//String 的getBytes()默认在windows系统中默认上的是GBK } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- ③向客户端输出响应消息头:response.setHeader("Content-Typt","text/html;charset=UTF-8");
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; /** * 2017/11/5 * 说明: */ public class ResponseDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Content-Type","text/html;charset=UTF-8"); String data = "我爱你"; OutputStream out = response.getOutputStream(); out.write(data.getBytes("utf-8"));//String 的getBytes()默认在windows系统中默认上的是GBK } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- ④向客户端输出响应消息头:response.setContentType("text/html;charset=UTF-8");
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; /** * 2017/11/5 * 说明: */ public class ResponseDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); String data = "我爱你"; OutputStream out = response.getOutputStream(); out.write(data.getBytes("utf-8"));//String 的getBytes()默认在windows系统中默认上的是GBK } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 示例:字符流输出中文内容
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; /** * 2017/11/5 * 说明: */ public class ResponseDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "我爱你"; PrintWriter out = response.getWriter(); out.print(data); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 出现乱码,因为response对象是由Tomcat实现的,而Tomcat内部的码表是ISO8859-1。
- 改变字符流默认的编码:response.setCharacterEncoding("UTF-8");
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; /** * 2017/11/5 * 说明: */ public class ResponseDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); String data = "我爱你"; PrintWriter out = response.getWriter(); out.print(data); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 因为:字符流是UTF-8,而浏览器本地的编码是GBK
- 修改浏览器本地的编码:response.setContentType("text/html;charset=UTF-8");
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; /** * 2017/11/5 * 说明: */ public class ResponseDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String data = "我爱你"; PrintWriter out = response.getWriter(); out.print(data); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 示例:;定时刷新
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 2017/11/5 * 说明: */ public class ResponseDemo2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("refresh","2;URL=http://www.baidu.com"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 示例:控制缓存
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 2017/11/5 * 说明: */ public class ResponseDemo3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //清除缓存 response.setHeader("expires","-1"); response.setHeader("pragma","no-cache"); response.setHeader("cache-control","no-cache"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 示例:请求重定向
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 2017/11/5 * 说明: */ public class ResponseDemo3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setStatus(302); response.setHeader("location",request.getContextPath()+"/responseDemo2"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 2017/11/5 * 说明: */ public class ResponseDemo3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect(request.getContextPath()+"/responseDemo2"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
3 HttpServletRequest
- 常用方法:
public java.lang.String getMethod()
public java.lang.String getRequestURI()
public java.lang.StringBuffer getRequestURL()
public java.lang.String getProtocol()
public java.lang.String getParameter(java.lang.String name)
public java.lang.String[] getParameterValues(java.lang.String name)
public java.util.Enumeration getParameterNames()
public java.util.Map getParameterMap()
public ServletInputStream getInputStream()
public java.lang.String getRemoteAddr()
- 示例:
package com.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 2017/11/5 * 说明: */ public class RequestServlet1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String method = request.getMethod();//获取请求方式 String uri = request.getRequestURI(); String url = request.getRequestURL().toString(); String protocol = request.getProtocol(); String ip = request.getRemoteAddr();//ip response.getWriter().println("方法:"+method+"<br/>"); response.getWriter().println("uri:"+uri+"<br/>"); response.getWriter().println("url:"+url+"<br/>"); response.getWriter().println("protocol:"+protocol+"<br/>"); response.getWriter().println("ip:"+ip+"<br/>"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 示例:获取请求参数
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册</title> </head> <body> <table> <form action="${pageContext.request.contextPath}/register" method="post"> <tr> <td>用户名</td> <td> <input type="text" name="username"/> </td> </tr> <tr> <td>密码</td> <td> <input type="password" name="password"/> </td> </tr> <tr> <td>爱好</td> <td> <input type="checkbox" name="hobby" value="游戏"/>游戏 <input type="checkbox" name="hobby" value="开发"/>开发 <input type="checkbox" name="hobby" value="电视"/>电视 </td> </tr> <tr> <td colspan="2"> <input type="submit" value="注册"/> </td> </tr> </form> </table> </body> </html>
package com.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Arrays; /** * 2017/11/5 * 说明: */ public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); String[] hobby = request.getParameterValues("hobby"); System.out.println("用户名:"+username); System.out.println("密码:"+password); System.out.println("爱好:"+ Arrays.toString(hobby)); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 示例:post请求
package com.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Arrays; /** * 2017/11/5 * 说明: */ public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String[] hobby = request.getParameterValues("hobby"); System.out.println("用户名:"+username); System.out.println("密码:"+password); System.out.println("爱好:"+ Arrays.toString(hobby)); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 通过request.setCharacterEncoding("utf-8") 设置请求消息内容的编码为utf-8
- 示例:get请求
package com.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Arrays; /** * 2017/11/5 * 说明: */ public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); String[] hobby = request.getParameterValues("hobby"); System.out.println("用户名:"+username); System.out.println("密码:"+password); System.out.println("爱好:"+ Arrays.toString(hobby)); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
- 注意:Tomcat8.0以上的版本使用的编码是UTf-8,所以不需要改变手动转换,如果是Tomcat8.0以下的。就需要手动装换了。
Request和Response的更多相关文章
- Request 和 Response 原理
* Request 和 Response 原理: * request对象和response对象由服务器创建,我们只需要在service方法中使用这两个对象即可 * 继承体系结构: ...
- Request 、Response 与Server的使用
纯属记录总结,以下图片都是来自 ASP.NET笔记之 Request .Response 与Server的使用 Request Response Server 关于Server.MapPath 方法看 ...
- request 和response
当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基本协议相当重要, 因为它让我们理解了We ...
- Request和Response对象
Request 和 Response 对象起到了服务器与客户机之间的信息传递作用.Request 对象用于接收客户端浏览器提交的数据,而 Response 对象的功能则是将服务器端的数据发送到客户端浏 ...
- Java 中的 request 和response 理解
request和response(请求和响应) 1.当Web容器收到客户端的发送过来http请求,会针对每一次请求,分别创建一个用于代表此次请求的HttpServletRequest对象(reque ...
- 【转】request和response的页面跳转传参
下面是一位园友的文章: jsp或Servlet都会用到页面跳转,可以用 request.getRequestDispatcher("p3.jsp").forward(request ...
- LoadRunner中取Request、Response
LoadRunner中取Request.Response LoadRunner两个“内置变量”: 1.REQUEST,用于提取完整的请求头信息. 2.RESPONSE,用于提取完整的响应头信息. 响应 ...
- Spring mvc中使用request和response
@ResponseBody @RequestMapping(value="/ip", method=RequestMethod.GET) public String getIP(H ...
- springMVC获取request和response
转载:http://blog.sina.com.cn/s/blog_7085382f0102v9jg.html 1.参数 例如: @RequestMapping("/test") ...
- ASP.NET中的Request、Response、Server对象
Request对象 Response.Write(Request.ApplicationPath) //应用根路径 Request.AppRelativeCurrentExecutionFilePat ...
随机推荐
- C#学习笔记-策略模式
题目:做一个商场收银的小程序,可能会出现的情况包括:正常收费,九折优惠,七折优惠,满300减50等各种不同随时会变化的优惠活动. 界面如下: 分析: 首先我们对于收钱写一个父类CashSuper.这个 ...
- Codeforces #448 Div2 E
#448 Div2 E 题意 给出一个数组,有两种类型操作: 选定不相交的两个区间,分别随机挑选一个数,交换位置. 查询区间和的期望. 分析 线段树区间更新区间求和. 既然是涉及到两个区间,那么对于第 ...
- MyBatis + MySQL返回插入的主键id
这是最近在实现perfect-ssm中的一个功能时碰到的一个小问题,觉得需要记录一下,向MySQL数据库中插入一条记录后,需要获取此条记录的id值,以生成对应的key值存入到redis中,id为自增i ...
- C# 获取当前方法的名称空间、类名和方法名称
1.(new StackTrace()).GetFrame(1) // 0为本身的方法:1为调用方法2.(new StackTrace()).GetFrame(1).GetMethod().Name; ...
- hdu 6191--Query on A Tree(持久化字典树)
题目链接 Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A l ...
- jQuery判断Dom对象是否存在
我们时常要检测一个DOM对象是否为空. var $jObject = $('#btn'); alert($jObject ); 我们会发现,$jObject 永远不会为空.为什么呢?$ 方法查找对象, ...
- 基于Windows下处理Java错误:编码GBK的不可映射字符的解决方案
基于Windows下处理Java错误:编码GBK的不可映射字符的解决方案 最近在研究Java,涉及命令行编译,使用notepad++编辑器,然后使用javac编译: 之前的几个文件没有中文的内容,都没 ...
- Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】
A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard ...
- as提示功能
- Redis Cluster集群搭建与应用
1.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper,但从redis 3.0之后版本支持redis-cluster集群,redis-cluster采用无中心结 ...