Ajax basics-Chapter 5 (5)

Source: Internet
Author: User

Get and post 

The get method places the value as a name/value pair in the request URL for transmission. There is a question mark after the URL, followed by the name/value pair. The post method is used to send parameters to the server, which is basically the same as the get method. The main difference is that the POST method sends parameters in the Request body, while the get method appends the parameters to the URL and sends them. The amount of data sent using get is fixed, which usually varies with the browser, while the POST method can send any amount of data.

The form element of HTML allows you to set the method attribute of the form element to get or post. The form Element automatically encodes the data of the input element based on the method attribute.

SetRequestHeader:

This method must be called after the open method. The enctype attribute of the form element specifies the encoding type used when the form data is submitted to the server, the default value is "application/X-WWW-form-urlencoded ".

Example:

Getandpostexample.html:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> sending request data using get and post </title>

<SCRIPT type = "text/JavaScript">
VaR XMLHTTP;

Function createxmlhttprequest (){
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}

Function createquerystring (){
VaR firstname = Document. getelementbyid ("firstname"). value;
VaR middlename = Document. getelementbyid ("middlename"). value;
VaR Birthday = Document. getelementbyid ("Birthday"). value;

VaR querystring = "firstname =" + firstname + "& middlename =" + middlename
+ "& Birthday =" + birthday;

Return querystring;
}

Function dorequestusingget (){
Createxmlhttprequest ();

VaR querystring = "getandpostexample? ";
Querystring = querystring + createquerystring ()
+ "& Timestamp =" + new date (). gettime ();
XMLHTTP. onreadystatechange = handlestatechange;
XMLHTTP. Open ("get", querystring, true );
XMLHTTP. Send (null );
}

Function dorequestusingpost (){
Createxmlhttprequest ();

VaR url = "getandpostexample? Timestamp = "+ new date (). gettime ();
VaR querystring = createquerystring ();

XMLHTTP. Open ("Post", URL, true );
XMLHTTP. onreadystatechange = handlestatechange;
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XMLHTTP. Send (querystring );
}

Function handlestatechange (){
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 | XMLHTTP. Status = 0 ){
Parseresults ();
}
}
}

Function parseresults (){
VaR responsediv = Document. getelementbyid ("serverresponse ");
If (responsediv. haschildnodes ()){
Responsediv. removechild (responsediv. childnodes [0]);
}

VaR responsetext = Document. createtextnode (XMLHTTP. responsetext );
Responsediv. appendchild (responsetext );
}

</SCRIPT>
</Head>

<Body>
<H1> enter your first name, middle name, and birthday:
<Table>
<Tbody>
<Tr>
<TD> First name: </TD>
<TD> <input type = "text" id = "firstname"/>
</Tr>
<Tr>
<TD> middle name: </TD>
<TD> <input type = "text" id = "middlename"/>
</Tr>
<Tr>
<TD> birthday: </TD>
<TD> <input type = "text" id = "Birthday"/>
</Tr>
</Tbody>

</Table>

<Form action = "#">
<Input type = "button" value = "SEND Parameters Using get" onclick = "dorequestusingget ();"/>

<Br/>
<Input type = "button" value = "SEND Parameters Using Post" onclick = "dorequestusingpost ();"/>
</Form>

<Br/>
<H2> Server Response: </H2>

<Div id = "serverresponse"> </div>

</Body>
</Html>

Note: add the timestamp to the URL to ensure the uniqueness of the URL.

Java Servlet can be used to process the server processing method of the program.

Java Servlet must define doget and dopost. Each method is called Based on get and post.

Import java. Io .*;
Import java.net .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

Public class getandpostexample extends httpservlet {

Protected void processrequest (httpservletrequest request,
Httpservletresponse response, string method)
Throws servletexception, ioexception {

// Set content type of the response to text/XML
Response. setcontenttype ("text/XML ");

// Get the user's input
String firstname = request. getparameter ("firstname ");
String middlename = request. getparameter ("middlename ");
String Birthday = request. getparameter ("Birthday ");

// Create the response text
String responsetext = "hello" + firstname + "" + middlename
+ ". Your birthday is" + birthday + "."
+ "[Method:" + method + "]";

// Write the response back to the browser
Printwriter out = response. getwriter ();
Out. println (responsetext );

// Close the writer
Out. Close ();
}

Protected void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// Process the request in method processrequest
Processrequest (request, response, "get ");
}

Protected void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// Process the request in method processrequest
Processrequest (request, response, "Post ");
}
}

The related classes in the javax. servlet software package are servletrequest and servletresponse, while those in the javax. servlet. Http software package are httpservletrequest and httpservletresponse. Servlet communicates with the server through these objects and finally communicates with the client.

Note: The Service () method is the core of servlet. Every time a customer requests an httpservlet object, the Service () method of this object will be called, and a "request" (servletrequest) object and a "response" (servletresponse) will be passed to this method) object as a parameter. The Service () method already exists in httpservlet. The default service function is to call the do function corresponding to the HTTP request method. Therefore, it is not necessary to overwrite the Service () method. You only need to overwrite the corresponding do method.

Each servlet service method is introduced.

1. doget (): Call the server resource and return it to the client as a response. doget () calls display the data that is being transferred to the servlet in the URL force, which may cause some problems in the system security, such as user login to the city, the user name and password in the form must be sent to the server. The doget () call will display the user name and password in the browser URL.
2. dopost (): it is used to transmit client data to the server. It can be used to send data to the server in a hidden way. Post is suitable for sending large amounts of data.
3. doput (): The call is similar to post. It allows the client to store real files on the server, not just to transmit data.
4. dodelete (): it allows the client to delete files or web pages on the server. It is rarely used.
5. dohead (): it is used to process the head call of the client and returns a response. when the client only needs to wait for the response header, it sends a header request. in this case, the client usually cares about the response length and the MIME type of the response.
6. dooptions (): it is used to process the options call of the client. Through this call, the client can obtain the methods supported by this servlet. If the servlet overwrites the dopost () method, it will return:
Allow: Post, Trace, options, head
Generally, this method does not need to be overwritten.

For details about httpservlet, see Java basics.

Related Article

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.