2016.5.9 (use servlet to process HTTP requests)

Source: Internet
Author: User
Tags http post

Servlet accepts request information

The HttpServletRequest object is mainly used to obtain the request headers, parameters, files, data, etc. sent by the client. The main purpose of the servlet's existence is to process requests


Get/post Submission Method

The browser sends an HTTP request to the Web server
Users click on a hyperlink on the Web page
Users submit forms that are filled out on the web
The user enters the URL address in the browser address bar and returns

By default, the request is submitted using the Get method of the HTTP protocol

customizing browser behavior
<form name= ' loginform ' method= ' POST ' action= '/loginservlet ' >


Get/post Submission Method Differences

When to use the Get/post method:
1: Use the Get method when requesting a static page or graphic file, because only the file name needs to be sent;
2: When sending big data, use the Post method;
3: When uploading a file, use the Post method;
4: Use the Post method when sending a user name, password, or other confidential information


Get HTTP protocol Request lines

The HttpServletRequest object is accessed in the following way
GetMethod (): Gets the HTTP request method, such as GET, post, etc.
Getrequesturi (): Gets the requested URI resource
Getrequesturl (): Gets the requested URL that contains the protocol name, server name or IP, port number, and request resource but does not include query string parameters
GetQueryString (): Gets the query string after the request URL
Getprotocol (): Gets the protocol name and version number of the HTTP
Getcontextpath (): Gets the context path of the request URI resource
Getservletpath (): Gets the mapping path of the servlet


Get HTTP protocol request header

The HTTP request header is used to tell the server client what software to use and how the client wants the server to return the requested information
The HttpServletRequest object is accessed in the following way
GetHeader (name): Returns the value of the specified request header
Getheaders (name): Returns a enumeration (enum) containing all the values in the request header
Getheadernames (): Name of all request headers accepted in a particular request
Getintheader (name): Gets the value of a specific request header and converts it to an int type
Getdateheader (name): Gets the value of a specific request header and converts it to a date type


Get all Request headers

enumeration enum = Request.getheadernames ();
while (Enum.hasmoreelements ()) {
String headername = (string) enum.nextelement ();
String Headervalue = Request.getheader (headername);
Out.print ("<b>" +headername + "</B>:");
Out.println (Headervalue + "<br>");
}

Get browser type

String useragent = Request.getheader ("user-agent");
if ((useragent! = null) && (Useragent.indexof ("MSIE")!=-1)) {
Message = "You are using Microsoft IE browser";} else if (
(useragent! = null) && (Useragent.indexof ("Firefox")!=-1)) {
Message = "You are using Firefox firefox browser";}

Gets the request message body

The body of the message can be plain text or binary data
The HttpServletRequest object can use a common method to get the form data


Get form data

The HttpServletRequest object uses the following methods to access the request parameters
enumeration enum = Request.getparameternames ();
while (Enum.hasmoreelements ()) {
String pName = (string) enum.nextelement ();
string[] pvalues = request.getparametervalues (pName);
Out.print ("<b>" +pname + "</B>:");
for (int i=0;i<pvalues.length;i++) {
Out.print (Pvalues[i]);
}
Out.print ("<br>");
}


Get raw form data

The POST request not only transmits text information but also transmits binary data
GetParameter () method can get binary data?
Direct access to form data using the getInputStream () or Getreader () method provided by the HttpServletRequest object
Analyze the original input with the input stream
Removes all the request header information, processes the raw bytes in the request message body, and even writes the bytes to a file on the server

Request for distribution

Each customer's request can be passed to many servlets and other resources in the Web application
The whole process is done entirely on the server side.
Does not require any client browser behavior
No need to pass special information between the client browser and the server side
Implemented by Javax.servlet.RequestDispatcher objects
Redirects are done by the client browser, while request dispatch is performed on the server side

Implementation of request distribution

Methods to invoke the ServletRequest object:
Getrequestdispatcher (Path): Method returns the RequestDispatcher object for the given path
Getnameddispatcher (name): Returns the RequestDispatcher object for a servlet of a name

The RequestDispatcher object provides two methods:
Forward (ServletRequest, Servletresponse): Distributes request and response objects to new resources identified by RequestDispatcher objects (Servlet, JSP, HTML, etc.)
Include (ServletRequest, Servletresponse) The key to this approach is to include server-side resources
The forward () method means that when the request and response are sent to another resource, no more processing is done.
The Nclude () method means asking someone to help with the request, but this is not a complete handover, but temporarily handing over control to someone else.

Request Scope
Variables can be saved in the request scope
Access is not available outside the scope of the request


HttpServletRequest objects are primarily used to process analysis requests
By default, requests are submitted using the Get method of the HTTP protocol
HTML forms enable the browser to use the HTTP POST method through the "Method" property
The Get/post method is suitable for different occasions
The most common method of HttpServletRequest objects is to get request form parameters
The HttpServletRequest object can also get raw byte data for the parameters in the request
The biggest difference between redirection and request dispatch is that redirection is done by the client browser, and request dispatch is performed on the server side
HttpServletRequest can bind an object to a request by name and access and delete it.

2016.5.9 (use servlet to process HTTP requests)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.