Android (Java) Learning Note 209: Submitting data to a server with GET requests

Source: Internet
Author: User

1.GET Request:

The path of the group-spelling URL, after which the submitted data is assembled into the URL, is submitted to the server.

cons : (1) security (2) Limited length can not exceed 4K (HTTP protocol limit), ie browser limit to 1K

Pros : Code is easy to write

2. We first access the server scene under the computer simulation

(1) Create a new Dynamic Web project using Eclipse, as follows:

(2) then write a servlet program (running on the server), named " Loginservlet ", as follows:

The code reads as follows:

1  PackageCom.himi.web;2 3 Importjava.io.IOException;4 Importjavax.servlet.ServletException;5 ImportJavax.servlet.annotation.WebServlet;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten /** One * Servlet Implementation class Loginservlet A  */ -@WebServlet ("/loginservlet") -  Public classLoginservletextendsHttpServlet { the     Private Static Final LongSerialversionuid = 1L; -  -     /** -      * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response) +      */ -     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { +String QQ = Request.getparameter ("QQ")); AString Password = request.getparameter ("Password"); atSystem.out.println ("QQ:" +QQ); -System.out.println ("Password:" +password); -           -          //simulate the server operation, query the database, look at QQ and password is correct. Response.getoutputstream () gets an output stream that writes data to the browser (cue data) -          if("10086". Equals (QQ) && "123456". Equals (password)) { -Response.getoutputstream (). Write ("Login Success". GetBytes ()); in}Else { -Response.getoutputstream (). Write ("Login Failed". GetBytes ()); to          } +           -     } the  *}

Just now we said GET request method, put the submitted data group to the URL path to submit to the server, so here Request.getparameter ();

(3) Next write a JSP dynamic page (different from the HTML static page), first we say the JSP and HTML differences, as follows:

JSP and HTML differences:

The HTML page is a static page, which is written by the user in advance and placed on the server, sent by the Web server to the client; The JSP page is a dynamic page with a JSP container that executes the Java code portion of the page and then generates the page in real time.

• The definition of an HTML page is a static page that can be run directly, and the JSP page is a dynamic page that needs to be converted into a servlet when it runs.

HTML can be opened directly, JSP can only be published to Tomact and other servers to open

• Their headers are different, this is the JSP header "<%@ page language=" java "import=" java.util.* "pageencoding=" GBK "%>" in the header of the table has encoded format and poured into the package and so on.

• It is well-differentiated to use <%%> in JSP to write Java code, while HTML does not have <%%>.

Write a login.jsp code, under the WebContent folder :

The login.jsp code is as follows:

1 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">2 <HTML>3 <Head>4 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"5 pageencoding="Utf-8"%>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">7 <title>???????</title>8 </Head>9 <Body>Ten      One     <formAction= "Loginservlet"Method= "Get"> APlease enter QQ account:<inputtype= "text"name= "QQ"> <BR/> -Please enter the QQ password:<inputtype= "Password"name= "Password"> <BR/> -     <inputtype= "Submit"value= "Login"> the     </form> - </Body> - </HTML>

Right-click the login.jsp file , run as and run on server , and run the following results:

(4) Open 360 browser capture package to analyze the detailed process of HTTP communication:

• Open 360 Browser, address bar input:http://localhost:8080/web/login.jsp, the results are as follows:

• Enter a QQ account and QQ password, enter the QQ account :10000, enter the QQ password :123123, as follows:

The detailed fetch packet is as follows:

The data length of the Response mentioned above is (the number of characters), the following Response data content :

• Enter the correct QQ account and QQ password as follows:

The same crawled packets are:

The length of the data mentioned above is Response (the number of characters), the following Response data content :

Android (Java) Learning Note 209: Submitting data to a server with GET requests

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.