Introduction to jsp paging instances using json

Source: Internet
Author: User
Json is easy to understand and fast to transmit. It can be well integrated with javascript. without adding a jar, it can well solve the jsp paging problem. Next we will introduce how to implement json in the previous article, 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:

First: Write 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; } @Override public String toString() { //{'id':1,'name':'zhangsan','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 simplify the code and facilitate the 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; @ Override protected 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 3 int 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
 
  
List = new external list
  
   
(); 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 ));}}
  
 

Then, enter the jsp file, submit the page asynchronously through ajax, and obtain 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 + "/"; %>    My JSP 'regist. jsp 'starting page 
  
  
  
  
  
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.