JSON implementation JSP pagination example introduction (with effect chart) _jsp programming

Source: Internet
Author: User
Tags stringbuffer
JSON has been described in detail in the previous article, and JSON is straightforward and fast. And it blends well with JavaScript.
The JSP paging problem can be done well without the need to add a jar.
The following is a detailed description of the paging instance:

The effect is as shown in the picture, using Jsp+servlet technology
First: Write a JavaBean User.java
Copy Code code as follows:

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;
}
@Override
Public String toString () {
{' id ': 1, ' name ': ' Zhangsan ', ' password ': ' 123 ', ' Age ': 1}
Return ' {' id ': ' +id+ ', ' name ': ' +name+ ', ' Password ': ' +password+ ', ' age ': ' +age+ '} ';
}

}

The thing to note here is how the ToString method changes.
Then: Let's write its control layer and DAO layer
To simplify the code and make it easier to value, the database is tentatively written as a collection
can see that
1.service receive request requests get the page to display the current page (as page page)
2. Create a string, add the user from database db in sequence, and encapsulate all data
Copy Code code as follows:

[{},{},{}]

3. Return this string to the request page
Copy Code code as follows:

Package servlet;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.util.LinkedList;
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;
@Override
protected void Service (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
The paging data source currently gets the number of pages of the record how many pieces per page
int page = Integer.parseint (request.getparameter ("page"));
page = 1 i = 0
page = 2 3
int length = 0;//How many entries are currently taken
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 ("disruptive");
Return
}
Response.setcontenttype ("Text/html;charset=utf-8");
Response.getwriter (). println (message);
}
}
Class db{
public static list<user> List = new linkedlist<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 fill in, through the Ajax asynchronous submission page, and then get the corresponding string
Copy Code code as follows:

var mgs = Xmlhttprequest.responsetext;
var persons = Mgs.evaljson ();

Parse and add JSON data to the displayed area
Copy Code code as follows:

<%@ 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" >
<base href= "<%=basePath%>" >
<title>my JSP ' regist.jsp ' starting page</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<script type= "Text/javascript" src= "Js/prototype1.6.js" ></script>
<script type= "Text/javascript" src= "Js/jquery-1.4.4.js" ></script>
<script type= "Text/javascript" >
var paging = 0;
//
function page (p) {
/*if (p = = ' Next ' && paging < 3) {
paging + +;
}else if (p = = ' last ' && paging > 1) {
Paging--;
}else{
Alert (' Error ');
}*/
if (p = = ' Next ' && paging < 3) {
paging + +;
if (Paging > 1) {
$ (": Button:eq (0)"). Removeattr (' disabled ');
}
if (paging = = 3) {
$ (": Button:eq (1)"). attr (' disabled ', ' disabled ');
}
}else if (p = = ' last ' && paging > 1) {
Paging--;
$ (": Button:eq (1)"). Removeattr (' disabled ');
if (paging = = 1) {
$ (": Button:eq (0)"). attr (' disabled ', ' disabled ');
}
}
Createxmlhttprequest ();
Xmlhttprequest.onreadystatechange=back;
var url = encodeURI ("/json_page/paging?page=" +paging);
Xmlhttprequest.open ("Get", url,true);
Xmlhttprequest.send (NULL);
}
Let's say the name is XMLHttpRequest.
function Createxmlhttprequest () {
if (window. ActiveXObject) {
XMLHttpRequest = new ActiveXObject ("Microsoft.XMLHTTP");
}else{
XMLHttpRequest = new XMLHttpRequest ();
}
}
callback function
function back () {
if (xmlhttprequest.readystate = = 4 && xmlhttprequest.status = 200) {
var mgs = Xmlhttprequest.responsetext;
var persons = Mgs.evaljson ();
Alert (MGS);
$ ("table"). empty ();
$ ("table"). Append ($ ("<tr><td>id</td><td> username </td><td> password &LT;/TD&GT;&LT;TD >age</td></tr> "));
for (var i = 0; i < persons.length;i++) {
var person = persons[i];
var $tr = $ ("<tr><td>" +person.id+ "</td><td>" +person.name+ "</td><td>" + Person.password+ "</td><td>" +person.age+ "</td></tr>");
$ ("table"). Append ($TR);
}
}
}
</script>
<body>
<input type= "button" disabled= "Disabled" value= "page" onclick= "page (' last '); /><input type= "button" value= "Next" onclick = "page (' Next ');" />
<table>
<tr><td>id</td><td> username </td><td> Password </td><td>age</td></ Tr>
</table>
</body>

In addition: To have these two JS OH
Copy Code code as follows:

<script type= "Text/javascript" src= "Js/prototype1.6.js" ></script>
<script type= "Text/javascript" src= "Js/jquery-1.4.4.js" ></script>
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.