Ajax: Real-time prompt Function

Source: Internet
Author: User
Remember Google's search. When you enter "North", the search keyword starting with "North" will be immediately pulled out. For example:

Through Ajax technology, we can also easily implement this function. BelowSource codeThis is an example I demonstrated when I attended class y08. I will study it myself!

1. xmlservlet. Java: This is a servlet that is returned in XML format.
Package com. Aptech. servlet;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. util. arraylist;
Import java. util. List;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Public class xmlservlet extends httpservlet {
Private Static list List = new arraylist ();
static {
list. Add ("Peking University ");
list. Add ("Great Northern Wilderness ");
list. Add ("Northern Ireland ");
list. Add ("Nordic ");
list. Add (" ");
list. Add ("red ");
list. Add ("Sun, red ");
list. Add ("red, green, and female ");
list. Add ("Flower pen ");
list. Add ("new ");
}
Public void dopost (httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
response. setcontenttype ("text/XML; character = UTF-8 ");
// disable caching
response. setheader ("Pragma", "No-Cache ");
response. setheader ("cache-control", "No-Cache ");
response. setdateheader ("expires", 0 );

Printwriter out = response. getwriter ();
Stringbuffer sb = new stringbuffer ();
SB. append (" ");
Request. setcharacterencoding ("UTF-8 ");
String key = request. getparameter ("key ");
If (key! = NULL & Key. Trim (). Length ()> 0 ){
For (string K: List ){
If (K. startswith (key )){
SB. append (" ");
SB. append (k );
SB. append (" ");
}
}
}
SB. append (" ");
Out. Print (sb. tostring ());
Out. Flush ();
Out. Close ();
}
}

Configuration:
Xmlservlet Com. Aptech. servlet. xmlservlet Xmlservlet /Xmlservlet

2.google.html: a static page that is used to enter query keywords and display matching information in real time.

Keywords keywords

Keywords keywords
Keywords:
Keywords keywords
Keywords keywords

3.styles.css: Use a style sheet for better results.
. Box {
Border: 1px solid # 0000ff;
Font-size: 14px;
Color: #000000;
Overflow: auto;
}
. Op {
Background-image: url(bk.jpg );
Border: 1px solid # 3300cc;
Width: 100%;
Color: white;
}

4. Results

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.