javaWeb_servletRequest 曾经终败给现在 2022-05-25 03:43 121阅读 0赞 Servlet的service方法有两个参数: @Override **public** **void** service(ServletRequest request, ServletResponse response) **throws** ServletException, IOException \{ \} ServletRequest:封装了请求信息,可以从中获取到任何的请求信息。 ServletResponse:封装了响应信息,如果想给用户什么响应,可以使用此接口的方法实现。 1. **获取请求的参数** 获取请求参数共有四个方法: 1. String getParameter(String name) :根据请求参数的名字,返回参数值 如: String sUser = request.getParameter("user"); System.***out***.println("user:" + sUser); String sPassword = request.getParameter("password"); System.***out***.println("password:" + sPassword); 测试:在页面中输入gary和123456. 后台输出: user:gary password:123456 2. String\[\] getParameterValues(String name):根据请求参数的名字,返回参数对应的字符串数组 如:在html中添加一个复选框 <form action=*"urlLogin"* mathod=*"post"*> user: <input type=*"text"* name=*"user"*/> password: <input type=*"password"* name=*"password"*/> <br/> 爱好: <br/> <input type=*"checkbox"* name=*"interesting"* value=*"reading"*/>阅读 <input type=*"checkbox"* name=*"interesting"* value=*"party"*/>聚会 <input type=*"checkbox"* name=*"interesting"* value=*"shopping"*/>购物 <input type=*"submit"* value=*"Submit"*/> </form> ![20180507173314793][] Service中如下获取: String \[\] oInterestings = request.getParameterValues("interesting"); **for** (String sInterest: oInterestings) \{ System.***out***.println("interest:" + sInterest); \} 测试, ![20180507173325197][] 后台输出: interest:reading interest:shopping 3. Enumeration getParameterNames() : 返回参数名对应的Enumeration对象 Service中添加代码: Enumeration<String> eNames = request.getParameterNames(); **while** (eNames.hasMoreElements())\{ String sName = eNames.nextElement(); String sValue = request.getParameter(sName); System.***out***.println("->" + sName + " : " + sValue); \} 后台输出: ![20180507173354888][] Interesting只输出了一个,是因为由名获取参数值的方法使用的是第一个,不是第二个获取数组的方法。 4. Map getParameterMap() : Service中添加代码: Map<String, String\[\]> oMap = request.getParameterMap(); **for** (Map.Entry<String, String\[\]> oEntry: oMap.entrySet())\{ System.***out***.println("\*\*" + oEntry.getKey() + " : " + Arrays.*asList*(oEntry.getValue())); \} 后台输出: ![20180507173429152][] 2. **获取请求的uri** 网页中输入:[http://localhost:8080/MyWebProject/login.html][http_localhost_8080_MyWebProject_login.html] 点击submit时实际访问的是: [http://localhost:8080/MyWebProject/urlLogin?user=gary&password=123456&interesting=reading&interesting=party][http_localhost_8080_MyWebProject_urlLogin_user_gary_password_123456_interesting_reading_interesting_party] 如: HttpServletRequest httpServletRequest = (HttpServletRequest)request; StringBuffer sURL = httpServletRequest.getRequestURL(); String sURI = httpServletRequest.getRequestURI(); // 请求方式 String sMethod = httpServletRequest.getMethod(); // 获取url后面带的参数(当请求方式为post时此值为null) String sQueryString = httpServletRequest.getQueryString(); // 获取servlet名 String sServletPath = httpServletRequest.getServletPath(); System.***out***.println("URL: " + sURL); System.***out***.println("URI: " + sURI); System.***out***.println("method: " + sMethod); System.***out***.println("QueryString: " + sQueryString); System.***out***.println("ServletPath: " + sServletPath); 站点:[http://localhost:8080][http_localhost_8080] **输出如下:** URL: http://localhost:8080/MyWebProject/urlLogin URI: /MyWebProject/urlLogin method: POST QueryString: null ServletPath: /urlLogin **将form表单中的method改为get后,再测试,输出如下:** ** ** URL: http://localhost:8080/MyWebProject/urlLogin URI: /MyWebProject/urlLogin method: GET QueryString: user=gary&password=123456&interesting=reading&interesting=party ServletPath: /urlLogin [20180507173314793]: /images/20220525/155dd172044a4bc2be63efbfac8394a6.png [20180507173325197]: /images/20220525/34b0da6dd6394e53984eea19e0766147.png [20180507173354888]: /images/20220525/0c14aa1978be46edb4581c932984de80.png [20180507173429152]: /images/20220525/6e9e65c0aa6549b6baec2104392e2dbc.png [http_localhost_8080_MyWebProject_login.html]: http://localhost:8080/MyWebProject/login.html [http_localhost_8080_MyWebProject_urlLogin_user_gary_password_123456_interesting_reading_interesting_party]: http://localhost:8080/MyWebProject/urlLogin?user=gary&password=123456&interesting=reading&interesting=party [http_localhost_8080]: http://localhost:8080
还没有评论,来说两句吧...