Use JSON and Ajax to create a Web site's tag cloud (Tagcloud)

Source: Internet
Author: User
Tags comments json tojson tostring

Don't say much nonsense, first look at the effect we finally achieved. Source download at the end of the article.

Style1:

Style2:

The tag cloud above realizes the idea as follows:

1. The server side provides information about tag, including tagname,posts, and transmits data using JSON format

In this example, I use the servlet to turn the bean into a JSON string using Json-lib. Of course, the relevant information Tag is only demonstrated here, the real environment may need to be removed from the database to deal with.

The code is as follows:

Import java.io.IOException;
Import java.util.ArrayList;
Import Java.util.Random;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Net.sf.json.JSONSerializer;
public class Tagcloudaction extends HttpServlet {
private static String cache = "";
/** */ /**
*
*/
Private static final Long Serialversionuid =-7031695721764039045L;
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Resp.setheader ("Pragma", "No-cache");
Resp.addheader ("Cache-control", "must-revalidate");
Resp.addheader ("Cache-control", "No-cache");
Resp.addheader ("Cache-control", "No-store");
Resp.setdateheader ("Expires", 0);
Resp.setcontenttype ("Text/xml");
Resp.setcharacterencoding ("UTF-8");
if (Cache.isempty ())
{
cache = Gettagcloudjsonstring ();
}
Resp.getoutputstream (). Write (Cache.getbytes ("UTF-8"));
Resp.flushbuffer ();
}
Private String gettagcloudjsonstring ()
{
Category C = new Category ("Name", "This is comments", 10);
C.setcategoryid (10);
System.out.println (Jsonserializer.tojson (c). ToString ());
System.out.println (Jsonserializer.tojson (c). ToString (2));

ArrayList < Category > categorieslist = new ArrayList < Category > ();
Random r = new Random ();
string[] tags = new string[] {
"JAVA", "Groovy", "Servlet", "ee", "JSP", "J2SE"
, "JSON", "AJAX", "Caiclient", ". NET", "C #", "Perl",
"Python", "Rails", "Ruby", "Boss", "Nokia", "GPhone", "IPhone"
, "Hiphone", "Ericsson", "Semens", "Novels", "Spring", "Summer", "Autumn", "Winter", "Happy Holidays",
"Burn The Bridges", "Rising Antivirus", "Milk powder accident"
, "Olympic Games", "Grails", "Google", "Baidu", "Xiaonei",
"Happy net", "Nets", "Domestic network", "are rubbish", "Saddam", "PK", "NET Pick"
} ;
int len = tags.length-1;
for (int i = 0; i < i + +) {
Category item = new Category (Tags[r.nextint (len)], "This are comments for" + i,r.nextint (100));
Item.setcategoryid (i);
Categorieslist.add (item);
}
System.err.println (Jsonserializer.tojson (categorieslist). toString ());
System.err.println (Jsonserializer.tojson (categorieslist). toString (2));
Return Jsonserializer.tojson (categorieslist). toString (2);
}
}

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.