Request随笔
Posted LL.LEBRON
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Request随笔相关的知识,希望对你有一定的参考价值。
Request随笔
request
对象和response
对象的原理:
-
request
和response
对象是由服务器创建的。我们来使用它们。 -
request
对象是来获取请求消息,response
对象是来设置响应消息。 -
request
对象继承体系结构: -
request
功能:-
获取请求消息数据
-
获取请求行数据
例子:
GET /RequestDemo1 HTTP/1.1
方法:(重点记住打*号即可)
//1.获取请求方式 String getMethod() //2.获取虚拟目录(*) String getContextPath() //3.获取Servlet路径 String getServletPath() //4.获取get方式请求参数 String getQueryString() //5.获取请求URI,URL(*) /* URL:统一资源定位符 URI:统一资源标识符 */ String getRequestURI() StringBuffer getRequestURL() //6.获取协议及版本 String getProtocol() //7.获取客户机的IP地址 String getRemoteAddr()
代码:
package request; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet("/RequestDemo1") public class RequestDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取请求方式 System.out.println(request.getMethod()); //2.获取虚拟目录(*) System.out.println(request.getContextPath()); //3.获取Servlet路径 System.out.println(request.getServletPath()); //4.获取get方式请求参数 System.out.println(request.getQueryString()); //5.获取请求URL(*) System.out.println(request.getRequestURL()); //5.获取请求URI System.out.println(request.getRequestURI()); //6.获取协议及版本 System.out.println(request.getProtocol()); //7.获取客户机的IP地址 System.out.println(request.getRemoteAddr()); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } 输出: GET /RequestDemo1 null http://localhost/RequestDemo1 /RequestDemo1 HTTP/1.1 0:0:0:0:0:0:0:1
-
获取请求头数据
方法:(重点记住打*号即可)
//1.通过请求头的名称获取请求头的值(*) String getHeader(String name) //2.获取所有的请求头名称 Enumeration<String> getHeaderNames()
代码:
@WebServlet("/RequestDemo1") public class RequestDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求头数据:user-agent String agent = request.getHeader("user-agent"); if (agent.contains("Chrome")) { System.out.println("谷歌来了..."); } else if (agent.contains("Firefox")) { System.out.println("火狐来了..."); } //获取请求头数据:referer String referer = request.getHeader("referer"); if (referer.contains("/xpp")) { System.out.println("牛!!!!!!!"); } else { System.out.println("欢迎去优酷!"); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
-
获取请求体数据
请求体:只有
POST
请求方式,才有请求体,在请求体中封装了POST请求的请求参数。步骤:
-
获取流对象
-
在从流对象中拿数据
//1.获取字符输入流,只能操作字符数据 BufferedReader getReader() //2.获取字节输入流,可以操作所有类型数据 ServletInputStream getInputStream()
@WebServlet("/RequestDemo1") public class RequestDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求消息体-请求参数 //1.获取字符流 BufferedReader br = request.getReader(); //2.读取数据 String line=null; while((line=br.readLine())!=null){ System.out.println(line); } } } 输出: username=zhangsan&password=124
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="RequestDemo1" method="post"> <input type="text" placeholder="请输入用户名" name="username"><br> <input type="text" placeholder="请输入密码" name="password"><br> <input type="submit" value="注册"> </form> </body> </html>
-
-
-
其他功能
-
获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数。
//1. 根据参数名称获取参数值 username=zs&password=123 String getParameter(String name): //2.根据参数名称获取参数值的数组 hobby=xx&hobby=game String[] getParameterValues(String name) //3.获取所有请求的参数名称 Enumeration<String> getParameterNames() //4.:获取所有参数的map集合 Map<String,String[]> getParameterMap()
样例:
@WebServlet("/RequestDemo1") public class RequestDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); System.out.println(username); Enumeration<String> parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String name = parameterNames.nextElement(); String value = request.getParameter(name); System.out.println(name + "------" + value); } System.out.println("___________________"); Map<String, String[]> parameterMap = request.getParameterMap(); parameterMap.forEach((a, b) -> System.out.println(a + "-------" + b[0])); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); this.doGet(request, response); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="RequestDemo1" method="post"> <input type="text" placeholder="请输入用户名" name="username"><br> <input type="text" placeholder="请输入密码" name="password"><br> <input type="submit" value="注册"> </form> </body> </html>
输出:
小皮皮mzz username------小皮皮mzz password------232323 ___________________ username-------小皮皮mzz password-------232323
第二种方法演示:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="RequestDemo1" method="post"> <input type="text" placeholder="请输入用户名" name="username"><br> <input type="text" placeholder="请输入密码" name="password"><br> <input type="checkbox" name="hobby" value="game">游戏 <input type="checkbox" name="hobby" value="study">学习 <input type="submit" value="注册"> </form> </body> </html>
@WebServlet("/RequestDemo1") public class RequestDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] hobbies = request.getParameterValues("hobby"); for (String hobby : hobbies) { System.out.println(hobby); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); this.doGet(request, response); } } 输出: game study
中文乱码问题
-
get方式:tomcat 8 已经将get方式乱码问题解决了
-
post方式:会乱码
解决:在获取参数前,设置
request
的编码request.setCharacterEncoding("utf-8");
-
请求转发:一种在服务器内部的资源跳转方式。
步骤:
//1. 通过request对象获取请求转发器对象: RequestDispatcher getRequestDispatcher(String path) //2. 使用RequestDispatcher对象来进行转发: forward(ServletRequest request, ServletResponse response)
特点:
- 浏览器地址栏路径不发生变化。
- 只能转发到当前服务器内部资源中。
- 转发是一次请求。
@WebServlet("/RequestDemo2") public class RequestDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); System.out.println("Demo2被访问了!!!"); request.getRequestDispatcher("/RequestDemo3").forward(request, response); } }
-
共享数据
- 域对象:一个有作用范围的对象,可以在范围内共享数据。
request
域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据。
方法:
//1.存储数据 void setAttribute(String name,Object obj) //2.通过键获取值 Object getAttitude(String name) //3.通过键移除键 void removeAttribute(String name)
-
获取ServletContext
ServletContext getServletContext()
-
-
以上是关于Request随笔的主要内容,如果未能解决你的问题,请参考以下文章