request对象常用方法总结 た 入场券 2021-06-24 15:58 524阅读 0赞 ## JSP内置对象:request对象 ## 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest类的实例。request对象具有请求域,即完成客户端的请求之前,该对象一直有效。常用方法如下: <table> <thead> <tr> <th align="left">常用方法</th> <th align="left">方法介绍</th> </tr> </thead> <tbody> <tr> <td align="left"><code>String getParameter(String name)</code></td> <td align="left">返回name指定参数的参数值</td> </tr> <tr> <td align="left"><code>String[] getParameterValues(String name)</code></td> <td align="left">返回包含参数name的所有值的数组</td> </tr> <tr> <td align="left"><code>void setAttribute(String,Object)</code></td> <td align="left">存储此请求中的属性</td> </tr> <tr> <td align="left"><code>Object getAttribute(String name)</code></td> <td align="left">返回指定属性的属性值</td> </tr> <tr> <td align="left"><code>String getContentType()</code></td> <td align="left">得到请求体的MIME类型</td> </tr> <tr> <td align="left"><code>String getProtocol()</code></td> <td align="left">返回请求用的协议类型及版本号</td> </tr> <tr> <td align="left"><code>String getServerName()</code></td> <td align="left">返回接受请求的服务器主机名</td> </tr> <tr> <td align="left"><code>int getServerPort()</code></td> <td align="left">返回服务器接受此请求所用的端口号</td> </tr> <tr> <td align="left"><code>String getCharacterEncoding()</code></td> <td align="left">返回字符编码方式</td> </tr> <tr> <td align="left"><code>void setCharacterEncoding()</code></td> <td align="left">设置请求的字符编码方式</td> </tr> <tr> <td align="left"><code>int getContentLength()</code></td> <td align="left">返回请求体的长度(以字节数)</td> </tr> <tr> <td align="left"><code>String getRemoteAddr()</code></td> <td align="left">返回发送此请求的客户端IP地址</td> </tr> <tr> <td align="left"><code>String getRealPath(String path)</code></td> <td align="left">返回一虚拟路径的真实路径</td> </tr> <tr> <td align="left"><code>String request.getContextPath()</code></td> <td align="left">返回上下文路径</td> </tr> </tbody> </table> ## 测试代码: ## * reg.jsp <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>用户注册</h1> <form name="regForm" action="request.jsp" method="get"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>爱好:</td> <td><input type="checkbox" name="favorite" value="read">读书 <input type="checkbox" name="favorite" value="music">音乐 <input type="checkbox" name="favorite" value="moive">电影 <input type="checkbox" name="favorite" value="internet">上网</td> </tr> <tr> <td colspan="2"><input type="submit" value="提交" /></td> </tr> </table> </form> <br> <br> <a href="request.jsp?username=xxx">测试URL传参数</a> </body> </html> * request.jsp <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>request内置对象</h1> <% request.setAttribute("password", "123456"); %> 用户名:<%=request.getParameter("username")%> <br> 爱好: <% if (request.getParameterValues("favorite") != null) { String[] fav = request.getParameterValues("favorite"); for (int i = 0; i < fav.length; i++) { out.println(fav[i] + " "); } } %> <br> 密码:<%=request.getAttribute("password")%><br> 请求体的mime类型:<%=request.getContentType()%><br> 协议类型及版本号:<%=request.getProtocol()%><br> 服务器主机名:<%=request.getServerName()%><br> 请求的端口号:<%=request.getServerPort()%><br> 字符编码方式:<%=request.getCharacterEncoding()%><br> 请求体的长度:<%=request.getContentLength()%><br> 返回发送请求的客户端IP地址:<%=request.getRemoteAddr()%><br> 返回虚拟路径的真实路径:<%=request.getRealPath("request.jsp")%><br> 返回上下文路径:<%=request.getContextPath()%><br> </body> </html> ### 代码运行结果 ### 代码运行后浏览器中显示如下: ![reg.jsp][]![request.jsp][] -------------------- end. [reg.jsp]: https://imgconvert.csdnimg.cn/aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMjI3MjAzNzQ2MDA0?x-oss-process=image/format,png [request.jsp]: /images/20210615/fcf506bdf32643d4b025fc7652c48ce1.png
还没有评论,来说两句吧...