Design Web page entry information with your own definition of server data reception

Source: Internet
Author: User

Requirements: Design a Web page for inputting username and login password. And the data is passed to the server and displayed.

1, Foreword: The webpage submits the get and the post two kind of way.

(1) For the Get submission method, take the example in this article as an example. The complete information received by the server is:

</pre><pre name= "code" class= "java" ><span style= "FONT-SIZE:14PX;" >get/?

Username=admin&password=admin http/1.1 (Request header and request body together) accept (indicates acceptable type): Application/x-shockwave-flash, Image/gif , Image/jpeg, Image/pjpeg, Image/pjpeg, Application/x-ms-application, APPLICATION/X-MS-XBAP, application/ Vnd.ms-xpsdocument, Application/xaml+xml, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, application/ MSWord, */*ACCEPT-LANGUAGE:ZH-CN (means received for Chinese) user-agent:mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; trident/4.0;. NET CLR 2.0.50727;. NET CLR 3.0.4506.2152;. NET CLR 3.5.30729; INFOPATH.2) accept-encoding (receive code): gzip (compression program), deflatehost:192.168.3.100:10000 (IP and port number of server) Connection: Keep-alive (stay Connected) </span>

   The real data we send (username=admin&password=666) is called the request body, and the other is called the request header, and the ability to find the Get method is to place the request body inside the request header. Because the request header and the request body are like the title and content of the article. The title must not have too much data, but the size of the content data is not limited.

So the request header data capacity is very small. Generally there is only 8k, so it is assumed that the request body is placed inside the request header. Can not be used to transfer large data such as pictures, audio, video, and placed outside the request header, there is no limit to the size of the data.

  so it is possible to derive a Get mode submission feature: The data is limited, but is submitted first because it is placed in the request header. So the submission speed is very fast this and the network transmission of the UDP protocol (please click here) features very similar.

 Another drawback of Get mode submission is: the content that is submitted is displayed in the Address bar. such as:


(2) for the Post submission method, for example in this article, the complete information received by the server is:

<span style= "FONT-SIZE:14PX;" >post/http/1.1 (I was asking for a head start) Accept:application/x-shockwave-flash, Image/gif, Image/jpeg, Image/pjpeg, Image/pjpeg, Application/x-ms-application, APPLICATION/X-MS-XBAP, Application/vnd.ms-xpsdocument, Application/xaml+xml, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */*accept-language:zh-cnuser-agent: mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; trident/4.0;. NET CLR 2.0.50727;. NET CLR 3.0.4506.2152;. NET CLR 3.5.30729; infopath.2) Content-type:application/x-www-form-urlencodedaccept-encoding:gzip, DeflateHost: 192.168.3.100:10000content-length:29connection:keep-alivecache-control:no-cache (I am the request head end) username=admin& Password=admin (I am the request body) </span>
can be seen, therequest header and the request body is separate, in the interval of two blank lines (two "/r/n"). This will be an important basis for the subsequent server to extract the content of the request body). There is no limit to the size of the data in the request body. So the post submission method is characterized by:

Features: the transfer data size is unlimited, but the transmission speed is slow. And no address bar exposes user information defects.

2. Design Web page

<span style= "FONT-SIZE:14PX;" ><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

--></span>

  to achieve the Web page effect:


3. Define the server yourself (if the username I entered is admin.) Password for 666)

<span style= "FONT-SIZE:14PX;" >public class Loginserver {public static void main (string[] args) throws IOException {//Create ServerSocket object, port set to 88888 8ServerSocket ss = new ServerSocket (888888);//Listener Client connection Socket s = ss.accept (); SYSTEM.OUT.PRINTLN ("Connection succeeded"); Assume that an object is successfully connected to the server. Output information hint//Get input stream InputStream is = S.getinputstream (); byte[] bys = new byte[1024]; Received with a character array, the size is set to 1024 bytes int len = Is.read (bys); Defines Len as the length of each actual read of the content string data = new String (bys, 0, Len); Because it is submitted by post, the data content obtained here is the full data state of the Post submission server in header 1//due to post submission, the request body is separated by two consecutive \ r \ n and the request header, and the data we need is the request body.

So we get the request body content step is: int index = DATA.INDEXOF ("\r\n\r\n"); <span style= "font-family:arial, Helvetica, Sans-serif;" > First gets the index position </span>string newdata = data.substring (index+4) twice in a row. Get the request body. At this time NewData actual content is: <span style= "font-family:arial, Helvetica, Sans-serif;" >username=admin&password=666</span>//the request body decomposition string username = GetParameter ("username", newdata); Call <span style= "font-family:arial, Helvetica, Sans-serif;" The >getparameter method gets the username content in NewData </span>string password = getparameter ("password", NewData); <span style= "font-family:arial, Helvetica, Sans-serif;" > Call </span><span style= "font-family:arial, Helvetica, Sans-serif;" >getparameter method gets the password content in NewData </span>string email = getparameter ("email", newdata); <span style= "font-family:arial, Helvetica, Sans-serif;" > Call </span><span style= "font-family:arial, Helvetica, Sans-serif;" The >getparameter method gets the email content in the NewData </span>//gets the output stream. Return information in a Web page printwriter PW = new PrintWriter (S.getoutputstream (), true); Get the Print Stream object Pw.println ("User login successful <br/>"); Pw.println ("User name: <font color=red>" +username+ "</font><br/>") in the Web page for successful login Display user name Pw.println ("Password is:" +password+ "<br/>"); Display user Registration password pw.println ("Email is:" +email+ "<br/>"); Display user Registration email pw.close (); Closes the print stream object because the server needs to be turned on all day. So do not close the socket object}private static string GetParameter (string key, String newdata) {//define the GetParameter method yourself. Used to get the object in the request body string[] Datas = Newdata.split ("&"); The data is divided into two parts by & and deposited into the datas array, datas[0]= (username=admin). Datas[1]= (password=666) for (String Data:datas) {//Enhanced for Traversal datas array//username=adminstring[] Keyandvalue = data.split ("= "); Divide data by "=" if (keyandvalue[0].equals (key)) {//Match return keyandvalue[1] According to the number of incoming parameters;}} return null; If none is returned null}}</span>

Summary: Web Data submissions are mainly available in Get and post two ways. Each has its own pros and cons. Get main features for fast speed. Small capacity, post main features are slow, large capacity. Other than that. Before data transfer, be careful to ensure that the server is turned on and in the listening state first.

Design Web page entry information with your own definition of server data reception

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.