Use EF to develop a web application (3): query string (see figure)

Source: Internet
Author: User

Question:There is no precedent for developing Web applications with "easy language. Flying" (EF. However, because the EF Local Development Kit (efndk) has been released, it is not difficult to use C/C ++ to develop an EF class library to support EF in developing web applications. Of course, we can imagine that there are many difficulties to solve. This series of articles is a record of my exploration process and may not be of much value to outsiders. If some netizens are optimistic about it, please wait rationally. Author: liigo. For more information, see http://blog.csdn.net/liigo /. Online messages.

 

Use EF to develop a web application (3): query string

 

Today, I will explain how to use the FastCGI class library (FastCGI. efn) in "easy language. Flying" to read the query string in the URL address.

What is query string? Question mark (?) in the URL (?) The subsequent text is query string. The following url is used as an example:

Http: // localhost: 8080/querystring. efcgi?X = ABC & Y = EF & Z = % E4 % B8 % ad % E5 % 9B % BD

The highlighted section in bold with a yellow background (x = ABC & Y = EF & Z = % E4 % B8 % ad % E5 % 9B % BD) is the query string. If you compare a FastCGI program to a function call, the query string is equivalent to the function parameter. In this example, the FastCGI program querystring. efcgi receives three parameters. The value of parameter X is ABC, and the value of Parameter Y is EF, the value of Parameter Z is % E4 % B8 % ad % E5 % 9B % BD (the decoded string is "China ).

To increase your impression, let's look at several query string instances. The URLs generated by Google, Baidu, and Yahoo are as follows:

Http://www.google.cn/search?Hl = ZH-CN & Q = liigo & meta = & AQ = f

Http://www.baidu.com?Ie = gb2312 & BS = FastCGI. efn & sr = & Z = & CL = 3 & F = 8 & WD = FastCGI. efn & Ct = 0

Http://www.yahoo.cn?P = % E6 % 98% 93% E8 % af % ad % E8 % A8 % 80.% E9 % A3 % 9e % E6 % 89% AC + EF + & V = web & pid = HP 

Query string is widely used in today's networks and can be seen almost everywhere.

 

In the EF class library FastCGI. efn, Reading query strings mainly involves the following methods:

Attribute text fcgi. QUERY_STRING(); // Obtain the QUERY_STRING decoded by the URL

Fctext GI. QUERY_STRING(Text name); // get the parameter value of the specified name in QUERY_STRING, which has been URL decoded

Text [] fcgi. Query_string_names(); // Get the parameter name array in QUERY_STRING

Attribute text fcgi. Query_string_undecoded(); // Returns the original QUERY_STRING without URL Decoding.

The property method QUERY_STRING () has the same name as the CGI Environment Variable with the same name. The returned value is also the content of the environment variable (but it has been URL decoded ). (Other CGI Environment variables will be introduced in future articles .)

The preceding (first) URL is used as an example to call the method fcgi without parameters. QUERY_STRING () returns "x = ABC & Y = EF & Z = China". Note that the word "China" has been decoded. In contrast to this method, fcgi. query_string_undecoded (), which returns the query string before URL Decoding, that is, "x = ABC & Y = EF & Z = % E4 % B8 % ad % E5 % 9B % BD ". Call the fcgi method with text parameters. QUERY_STRING (Text name) returns the value of the specified parameter, such as fcgi. QUERY_STRING ("X") returns "ABC", fcgi. QUERY_STRING ("Y") will return "Ef", fcgi. QUERY_STRING ("Z") returns "China" (URL-decoded ). Call method fcgi. query_string_names () returns an array of parameter names, {"X", "Y", "Z"}. The order of the members of the array is the same as that of the query string.

Note: According to CGI specifications, the environment variable QUERY_STRING stores the "original, undecoded" query string, which must be decoded by the programmer after being read. In my ef class library, the QUERY_STRING () method directly returns the "url decoded" query string. This is also a user-friendly processing that fully takes into account the actual needs of programmers. If you really need a "undecoded" query string, you can call the query_string_undecoded () method to obtain it without losing functionality and flexibility. This is different from the common cgi/FastCGI program.

It is also worth noting that the reading, decoding, and parsing of query strings has been specially optimized and the results are cached to maximize the execution efficiency, which will be detailed in the next article. URL Decoding and URL encoding will also be introduced in future articles.

See the actual operation (Online demonstration) below ):

The source code of the above dynamic web page "easy language. Flying" (EF) is as follows:

Introduce FastCGI;

Public class startup class
{
Public static startup ()
{
Int COUNT = 0;
Fcgi = new fcgi;
While (fcgi. Accept ()> = 0)
{
Text T = S. replace all ("$ (title)", "query string ");
T = T. Replace ("$ (Params)", get_params (fcgi ));
T = T. Replace ("$ (INDEX)", Count. To text ());
Fcgi. Output (T. To utf8 ());
Count ++;
}
}

Static string get_params (fcgi)
{
String S;

S + = "<p> QUERY_STRING:" + fcgi. QUERY_STRING + "</P> ";
S + = "<p> query_string_undecoded:" + fcgi. query_string_undecoded + "</P> ";
S + = "<p> query_string_names:" + fcgi. query_string_names (). To text () + "</P> ";

S + = "

Traversal loop (fcgi. query_string_names (), text name)
{
S + = "<p>" + name + ":" + fcgi. QUERY_STRING (name) + "</P> ";
}

Return S;
}

Constant Text S = ["Content-Type: text/html

<HTML> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> $ (title) </title>
</Head>
<Body>
<H1> $ (title) <HR> <P> $ (Params) </P>
<HR> <P> by liigo, index: $ (INDEX) </P>
</Body>
</Html>
"];
}

 

Next Article Content preview: cache query string.

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.