JSON in the previous articleArticleIt has been described in detail that JSON is easy to understand and fast to transmit. And can be well integrated with JavaScript.
Without adding jar, JSP paging can be well completed.
The following describes paging instances:
Effect: JSP + Servlet Technology
First: Compile a JavaBean user. Java
Package bean; public class user {private int ID; private string name; private string password; private int age; public user () {super ();} public user (int id, string name, string password, int age) {super (); this. id = ID; this. name = Name; this. password = password; this. age = age;} public int GETID () {return ID;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} public int getage () {return age;} public void setage (INT age) {This. age = age ;}@ overridepublic string tostring () {// {'id': 1, 'name': 'hangsan ', 'Password': '123 ', 'age': 1} return "{'id':" + ID + ", 'name': '" + name + "', 'Password ': '"+ password +"', 'age': "+ age + "}";}}
Note that the tostring method is changed.
Then: Let's write its control layer and Dao layer.
To simplifyCodeFor easy value, the database is temporarily written as a set
As you can see,
1. The Service receives the request. The current page is displayed (the page)
2. Create a string, add the user from the database in turn, and encapsulate all data
[{},{},{}]
3. Return this string to the request page.
Package servlet; import Java. io. ioexception; import Java. io. printwriter; import Java. util. using list; import Java. util. list; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import bean. user; public class paging extends httpservlet {public static final int per_page = 3; @ overrideprotected void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {// number of records displayed on the page of the paging data source on the current page int page = integer. parseint (request. getparameter ("page"); // page = 1 I = 0 // page = 2 3int length = 0; // record the number of stringbuffer sb = new stringbuffer (); sb. append ("["); // [{}, {}, {}] string message = NULL; If (page >=1 & page <= 3) {for (INT I = (page-1) * per_page; I <dB. list. size () & length <per_page; I ++) {user u = dB. list. get (I); sb. append (U. tostring () + ","); length ++;} If (length> 0) {message = sb. substring (0, sb. length ()-1) + "]";} else {message = sb. tostring () + "]" ;}} else {response. setcontenttype ("text/html; charset = UTF-8"); response. getwriter (). println ("mess"); return;} response. setcontenttype ("text/html; charset = UTF-8"); response. getwriter (). println (Message) ;}} class dB {public static list <user> List = new external list <user> (); static {list. add (new user (1, "zhangsan", "Zs", 34); list. add (new user (2, "Lisi", "ls", 34); list. add (new user (3, "A", "H", 34); list. add (new user (4, "B", "D", 34); list. add (new user (5, "C", "g", 34); list. add (new user (6, "D", "A", 34); list. add (new user (7, "E", "D", 34); list. add (new user (8, "F", "E", 34); list. add (new user (9, "g", "A", 34 ));}}
After:JSP, submit the page asynchronously through Ajax, and then get the corresponding string
VaR MGS = XMLHttpRequest. responsetext; var persons = MGs. evaljson ();
Parse JSON data and add it to the displayed area
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
In addition, you need to have these two JS files.
<SCRIPT type = "text/JavaScript" src = "JS/prototype1.6.js"> </SCRIPT> <SCRIPT type = "text/JavaScript" src = "JS/jquery-1.4.4.js"> </ SCRIPT>