HttpServletRequest
来源:互联网 发布:淘宝宝贝标题怎么优化 编辑:程序博客网 时间:2024/06/08 13:07
获取请求参数[通用单值]
HTTP 请求包中的元素通常为字符串类型, 索引就是 HTML表单元素的 name 或者URL中等式右边名, 使用 getParameter 获取
诺出现 name相同的, 则取 URL中排在前面的, 表单中相对考前的[不好说], 当然, name相同的参数要消除
HTML 表单
<form action="SetAttribute" method="post" > user : <input type="text" name = "user" value = "admin" ><br> pwd : <input type="text" name = "pwd" value = "1234" ><br> <input type="submit" value="submit" > </form>
带参数的请求 URL, 表单做 GET 方式提交即可生成
http://127.0.0.1:8080/day06/SetAttribute?user=admin&pwd=1234
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");System.out.println( request.getParameter("user") );//输出adminSystem.out.println( request.getParameter("pwd") );//输出1234}
获取请求参数[复杂参数项]
radio
<form action="SetAttribute" method="post" > sex : <input type="radio" checked="checked" name="sex" value="boy" > <input type="radio" name="sex" value="girl" ><br> <input type="submit" value="submit" > </form>
表单中最好初始化某个 radio 为选中状态, 避免提交空值
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");System.out.println( request.getParameter("sex") );//输出 boy}
参数获取时, 与上无异, 但是 radio值可在 浏览器端或者URL参数方式中自由修改, 要求高的话, 需要在服务器端校验radio项参数值合法性
checkbox 多值参数
该类型参数 URL上传方式为 多个 name相同的 等式, 服务器端获取使用 getParameterValues 获取一个 String[]
html表单
<form action="SetAttribute" method="POST" > interest : A : <input type = "checkbox" name = "interest" value = "A" checked="checked" > B : <input type = "checkbox" name = "interest" value = "B" checked="checked" > C : <input type = "checkbox" name = "interest" value = "C" > <br> <input type="submit" value="submit"> </form>
URL 请求方式
http://127.0.0.1:8080/day06/SetAttribute?interest=A&interest=B
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");String[] interests = request.getParameterValues("interest");for( String value : interests ){System.out.println(value);//分别输出 A B}}
设置请求编码格式, 获取中文值
POST 方式比较简单, 调用 request 中的 setCharacterEncoding 通知 tomcat该次请求的编码格式
request.setCharacterEncoding("UTF-8");
GET 方式这么搞没用, 等看看哪个项目中怎么搞的, 应该不是视频里面那么来的, 我看那老师自己也晕了
0 0
- HttpServletRequest
- Httpservletrequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- httpservletrequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- HttpServletRequest
- Linux命令行报bash:.....:command not found的解决办法
- B+ Tree & Unicode & UTF-8 & 判断是否为UTF-8 & 几种常见中文的编解码表
- C语言宏定义函数的使用(定义单行和多行)
- PHP系统学习(1)
- prototype.js 1.4版开发者手册(强烈推荐)
- HttpServletRequest
- http status汇总
- linux多线程实现黑客帝国字符滑屏效果
- HDU 3874
- fatal error C1083: Cannot open include file: 'qwtplot.h':
- 荷兰国旗问题
- lucene入门01
- ubuntu 更换最新的163源
- 非线性,加权,放大和缩小,卷积