Request对象
2015-11-11 00:00
716 查看
摘要: java servlet request
###Requst对象简介
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,客户获取客户机的这些信息。
getRequestURL 返回客户端发出请求时的完整URL
getRequestURI 返回请求中的资源名部分
getQueryString 返回请求中的参数部分
getRemoteAddr 返回请求中客户机的IP地址
getRemoteHost 返回客户机的完整主机名
getRemotePort 返回客户机所使用的网络端口号
getLocalAddr 返回服务器的IP地址
getLocalName 返回服务器的主机名
getMethod 得到客户机请求方式
*. URL: www.sina.com/news/1.html 互联网资源
*. URI: 标示任意资源 例如:
news/1.html
c:/1.html
###request获取参数的方式
###通过表单收集客户机数据
后台servlet
#####如果以get方式提交,需要手工处理
###Requst对象简介
HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,客户获取客户机的这些信息。
getRequestURL 返回客户端发出请求时的完整URL
getRequestURI 返回请求中的资源名部分
getQueryString 返回请求中的参数部分
getRemoteAddr 返回请求中客户机的IP地址
getRemoteHost 返回客户机的完整主机名
getRemotePort 返回客户机所使用的网络端口号
getLocalAddr 返回服务器的IP地址
getLocalName 返回服务器的主机名
getMethod 得到客户机请求方式
*. URL: www.sina.com/news/1.html 互联网资源
*. URI: 标示任意资源 例如:
news/1.html
c:/1.html
###request获取参数的方式
1. request.getParameter("username") 2. Elemeration e = request.getParamterNames(); while(e.haseMoreElements()){ String name = (String)e.nextElement(); value = request.getParameter(name); System.out.println(name + ":" + value); } 3. String [] values= request.getaramterValues("username"); 4. Map map = request.getParameterMap(); User user = new User(); BeanUtils.populate(user,map) BeanUtils依赖commons.logging,BeanUtils.copyProperties(from,dest)此方法也比较常用。
###通过表单收集客户机数据
<form action="/day06/servlet/RequestDemo3" method="post"> 用户名:<input type="text" name = "username" /> 密码:<input type="password" name="password" /> 性别:<input type="radio" name="gender" value="male">男 <input type="radio" name="gender" value="female">女 所在地:<select name="city" <option value="beijing">北京</option> <option value="shanghai">上海</option> </select> 爱好: <input type="checkbox" name="likes" value="sing">唱歌 <input type="checkbox" name="likes" value="dance">跳舞 <input type="checkbox" name="likes" value="football">足球 备注: <textarea rows="6" cols="60" name="desc"></textarea> 照片:<input type="file" name="image"> <input type="hidden" name = "id" value="123"> <input type="submit" value="提交"></br>
request的乱码问题
#####如果以post提交<meta http-equiv="content-type" content="text/html;charset=UTF-8" <form action="/day06/servlet/RequestDemo2" method="post> 用户名:<input type="text" name="username" /> <input type="submit" value="提交" / > </form>
后台servlet
request.setCharacterEncoding("UTF-8"); request.getParamter("username");
#####如果以get方式提交,需要手工处理
String username = request.getParametr("username"); username = new String(username.getBytes("ISO-8859-1"),"UTF-8"); 超链接中的提交的中文,输入get请求,也需要手工处理 ``` 通过更改服务器的server.xml中的 URLEncoding="UTF-8"也能解决乱码问题,但是不推荐。 ###request实现转发的mvc设计模式 ``` String data= "aaa"; request.setAttributef("data",data); request.getRequestDispatcher("message.jsp").forward(request,response); ``` 前台: ``` 1. {data} 2. <% String data= (String) request.getAttribute("data"); out.write(data); %> ``` *. request对象实现请求转发,请求转发指一个web资源受到客户端请求后,通知服务器去调用另外一个web资源进行处理。 *. 请求转发的应用场景:mvc设计模式 *. request对象提供了一个getRequestDispatcher方法,该方法返回一个RequestDispatcher对象,调 用这个对象的forward方法可以实现请求转发。 *. request对象同时也是一个域对象,开发人员通过request对象在实现转发时,把数据通过request对象带给其它web资源处理 1.setAttribute方法 2.getAttribute方法 3.remoteAttribute方法 4.getAttributeNames方法 java.lang.IllegalStateException:cannot forward after response has been commited. 如果有两个forward,也会有以上异常出现,建议forward之后,return 请求转发的特点: *. 客户端只发一次请求,而服务器端有多个资源调用。 *. 客户端浏览器地址栏没有变化 ### web工程中各类地址的写法 ``` 1. request.getRequest("/form1.html").forward(request,response); 2. response.sendRedirect("/day06/form1.html"); 3. this.getServletContext().getRealPath("form1.html"); 4. this.getServletContext.getResourceAsStream("form1.html"); 5. <a href="/day06/form1.html">超链接 </a> 6. <form action="/day06/form1.do">.... ``` 对于客户端浏览器,要加上项目名称 对于服务器端,不加项目名称 ###利用referer防盗链 ``` String referer = request.getHeader("referer"); if(referer==null || referer.startwith("http://localhost")){ response.sendRedirect("/day06/index.jsp"); return; } response.getWriter().write("aaa"); ```
相关文章推荐
- 常用控件—UILabel
- Range Sum Query
- MiniGui3.0.12在主机上的配置及在开发板上的移植
- IOS UIImageView
- leetcode之Range Sum Query - Immutable
- UEFI Console Splitter 2nd
- Looper.myLooper().quit() 报 NullPointerException
- 进击的KFC:UI(一)UIView及其子类
- 爬爬爬之路:UI(一) UIView UILabel
- [TwistedFate]UIView UILabel
- UI学习第一天UIView 和UILabel
- hdu1159 Common Subsequence (最长公共子序列)
- 我的iOS学习历程 - UIView和UILabel
- 我的iOS学习历程 - UIView和UILabel
- 新手学EasyUI(七)----DataGrid CheckBox 动态选中
- UI第一天UIView
- UI基础知识 -- UIView UILabel
- UI基础-UIView及其子类
- 小技巧9-iOS UITextField结束编辑、收回键盘
- 随意细解:UI -- UIView及其子类