HttpServletRequest常用方法介绍
2016-10-30 00:00
459 查看
摘要: 我正在学习httpServletRequest接口,我怕我忘记了,所以就写篇文章记录下来!
request.setCharacterEncoding("utf-8"); //设置请求的字符集,用户提交到servlet的数据以utf-8的形式
String user = request.getParameter("user"); //获取用户提交的数据.
String contentPath = request.getContextPath();获取项目的根目录!
Enumeration<String> en = request.getHeaderNames(); //获取请求头名称,是一组枚举数据,类型为String
String s = request.getHeader(obj.toString()); //通过头名称获取到请求的头.
以下是示例代码:
以下是输出信息:
userruankun
contentLength:-1
contentType:null contentPath: /mytb
obj=hosts= localhost:8080
obj=user-agent s= Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
obj=accept s= text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
obj=accept-language s= zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
obj=accept-encoding s= gzip, deflate
obj=referer s= http://localhost:8080/mytb/ obj=connection s= keep-alive
obj=upgrade-insecure-requests s= 1
request.setCharacterEncoding("utf-8"); //设置请求的字符集,用户提交到servlet的数据以utf-8的形式
String user = request.getParameter("user"); //获取用户提交的数据.
String contentPath = request.getContextPath();获取项目的根目录!
Enumeration<String> en = request.getHeaderNames(); //获取请求头名称,是一组枚举数据,类型为String
String s = request.getHeader(obj.toString()); //通过头名称获取到请求的头.
以下是示例代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String user = request.getParameter("user"); System.out.println("user" + user); int contentLength = request.getContentLength(); System.out.println("contentLength:" + contentLength); String contentType = request.getContentType(); String contentPath = request.getContextPath(); System.out.println("contentType:" + contentType + "contentPath: " + contentPath); Enumeration<String> en = request.getHeaderNames(); while(en.hasMoreElements()){ Object obj = en.nextElement(); String s = request.getHeader(obj.toString()); System.out.println("obj=" + obj.toString() + "s= "+s); } }
以下是输出信息:
userruankun
contentLength:-1
contentType:null contentPath: /mytb
obj=hosts= localhost:8080
obj=user-agent s= Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
obj=accept s= text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
obj=accept-language s= zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
obj=accept-encoding s= gzip, deflate
obj=referer s= http://localhost:8080/mytb/ obj=connection s= keep-alive
obj=upgrade-insecure-requests s= 1
相关文章推荐
- HttpServletRequest、request常用方法、request常见应用、请求转发、RequestDispatcher
- HttpServletRequest、request常用方法、request常见应用、请求转发、RequestDispatcher
- HttpServletRequest常用获取URL的方法
- HttpServletRequest常用获取URL的方法
- HttpServletRequest常用获取URL的方法
- HttpServletRequest常用简单的方法
- java WEB学习笔记32:HttpSession 接口常用方法 及 HttpServletRequest接口中的Session方法 Demo
- HttpServletRequest常用获取URL相关的方法
- Servlet--HttpServletRequest一些不常用的方法
- HttpServletRequest几个常用方法的区别
- HttpServletRequest几个常用方法的区别
- java中HttpServletRequest可获取的URI方法介绍
- HttpServletRequest常用获取URL的方法
- HttpServletRequest常用的方法
- 好记性不如烂笔头9-HttpServletRequest常用方法和样例
- HttpServletRequest常用获取URL的方法
- HttpServletRequest常用方法
- HttpServletRequest常用获取URL的方法
- 基于HttpServletRequest 相关常用方法的应用
- HttpServletRequest常用的方法