HttpRequest
2015-08-30 16:32
447 查看
The
For instance, here is the signature of the
In this text I will look at the
The purpose of the
The
Notice the “query string” part of the URL:
You can access these parameters from the
You would use the same code, if the request parameters were sent in the body part of the HTTP request. If no parameter exists with the given name, null is returned.
In general, if the browser sends an HTTP GET request, the parameters are included in the query string in the URL. If the browser sends an HTTP POST request, the parameters are included in the body part of the HTTP request.
You can access the request headers from the
This example reads the
The
In general, If no header exists with the name passed to
To give you access to the request body of an HTTP POST request, you can obtain an
NOTE: You will have to call this method before calling any
What you do with the data read from the
The session object can hold information about a given user, between requests. So, if you set an object into the session object during one request, it will be available for you to read during any subsequent requests within the same session time scope.
Here is how you access the session object from the
I will not get into more detail about the session object here. It is covered in more detail in its own text.
Here is how you access the
As you can see, you have to first get the session object, to get access to the
I will not get into more detail about the ServletContext object here. It will be covered in more detail in its own text.
HttpServletclass request processing methods take two parameters.
javax.servlet.http.HttpRequest javax.servlet.http.HttpResponse
For instance, here is the signature of the
HttpServlet.doGet()method:
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
In this text I will look at the
HttpRequestobject.
The purpose of the
HttpRequestobject is to represent the HTTP request a browser sends to your web application. Thus, anything the browser may send, is accessible via the
HttpRequest.
The
HttpRequestobject has a lot of methods, so I will just cover the most commonly used here. The rest you can read about in the JavaDoc, if you are interested.
Parameters
The request parameters are parameters that are sent from the browser along with the request. Request parameters are typically sent as part of the URL (in the “query string”), or as part of the body of an HTTP request. For instance:http://jenkov.com/somePage.html?param1=hello¶m2=world
Notice the “query string” part of the URL:
?param1=hello¶m2=worldThis part contains two parameters with parameter values:
param1=hello param2=world
You can access these parameters from the
HttpRequestobject like this:
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); }
You would use the same code, if the request parameters were sent in the body part of the HTTP request. If no parameter exists with the given name, null is returned.
In general, if the browser sends an HTTP GET request, the parameters are included in the query string in the URL. If the browser sends an HTTP POST request, the parameters are included in the body part of the HTTP request.
Headers
The request headers are name, value pairs sent by the browser along with the HTTP request. The request headers contain information about e.g. what browser software is being used, what file types the browser is capable of receiving etc. In short, at lot of meta data around the HTTP request.You can access the request headers from the
HttpRequestobject like this:
String contentLength = request.getHeader("Content-Length");
This example reads the
Content-Lengthheader sent by the browser.
The
Content-Lengthheader contains the number of bytes sent in the HTTP request body, in case the browser sends an HTTP POST request. If the browser sends an HTTP GET request, the Content-Length header is not used, and the above code will return
null.
In general, If no header exists with the name passed to
getHeader(), null is returned.
InputStream
If the browser sends an HTTP POST request, request parameters and other potential data is sent to the server in the HTTP request body. It doesn’t have to be request parameters that is sent in the HTTP request body. It could be pretty much any data, like a file or a SOAP request (web service request).To give you access to the request body of an HTTP POST request, you can obtain an
InputStreampointing to the HTTP request body. Here is how it is done:
InputStream requestBodyInput = request.getInputStream();
NOTE: You will have to call this method before calling any
getParameter()method, because calling the
getParameter()method on an HTTP POST request will cause the servlet engine to parse the HTTP request body for parameters. Once parsed, you cannot access the body as a raw stream of bytes anymore.
What you do with the data read from the
InputStreamis up to you. The servlet engine does not help you parse or interprete that data. You just get it raw.
Session
It is possible to obtain the session object from theHttpRequestobject too.
The session object can hold information about a given user, between requests. So, if you set an object into the session object during one request, it will be available for you to read during any subsequent requests within the same session time scope.
Here is how you access the session object from the
HttpRequestobject:
HttpSession session = request.getSession();
I will not get into more detail about the session object here. It is covered in more detail in its own text.
ServletContext
You can access theServletContextobject from the
HttpRequestobject too. The
ServletContextcontains meta information about the web application. For instance, you can access context parameters set in the
web.xmlfile, you can forward the request to other servlets, and you can store application wide parameters in the
ServletContexttoo.
Here is how you access the
ServletContextobject from the
HttpRequestobject:
ServletContext context = request.getSession().getServletContext();
As you can see, you have to first get the session object, to get access to the
ServletContextobject.
I will not get into more detail about the ServletContext object here. It will be covered in more detail in its own text.
相关文章推荐
- HttpServlet
- 使用GenyMotion模拟器+抓包工具SRSniffer分析网络请求
- HttpServletRequest和HttpServletResponse详解
- POJ1459-Power Network-网络流-最大流(EK模板题)
- Linux网络栈剖析—从socket到设备驱动程序
- hdu 4741 2013杭州赛区网络赛 dfs ***
- (转)HTTP协议详解
- ios开发进阶之网络04 数据解析 文件下载上传
- Android网络编程之使用post方式提交数据
- 个人IHttpHandler,IHttpModule认识
- 防止SYN泛洪攻击 开启路由器的TCP拦截
- openwrt linux portal 实现 支持 https 支持基于时长和流量控制
- HTTP状态码含义
- android之ConnectivityManager简介,网络连接状态
- TCP协议的一些问题
- HTTP协议--处理状态和返回状态码含义
- HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
- Android网络编程之使用get方式向服务端提交数据和乱码问题的解决
- (转)TCP协议那些事
- 【python】socket网络编程