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);
}
}