Example of server-side paging logic Implementation Using JSP and JSON

Source: Internet
Author: User

The data on the server is stored in the JSON file, a comment list comment_list.json

{"comment_table"[{"userName": "zhangsan", "commentTime": "2012-5-26 12:00:00 UTC", "comment": "hello, wo shi zhansan"}{"userName": "zhangsan", "commentTime": "2012-5-26 12:00:00 UTC", "comment": "hello, wo shi zhansan"}{"userName": "zhangsan", "commentTime": "2012-5-26 12:00:00 UTC", "comment": "hello, wo shi zhansan"}{"userName": "zhangsan", "commentTime": "2012-5-26 12:00:00 UTC", "comment": "hello, wo shi zhansan"}{"userName": "zhangsan", "commentTime": "2012-5-26 12:00:00 UTC", "comment": "hello, wo shi zhansan"}{"userName": "zhangsan", "commentTime": "2012-5-26 12:00:00 UTC", "comment": "hello, wo shi zhansan"}{"userName": "zhangsan", "commentTime": "2012-5-26 12:00:00 UTC", "comment": "hello, wo shi zhansan"}{"userName": "zhangsan", "commentTime": "2012-5-26 12:00:00 UTC", "comment": "hello, wo shi zhansan"}{"userName": "zhangsan", "commentTime": "2012-5-26 12:00:00 UTC", "comment": "hello, wo shi zhansan"}]}

Using forms to submit data to JSP pages does not use the popular Ajax technology.

<Form method = "Post" Action = "resbrowse_2.jsp"> <Div align = "Left" style = "padding-left: 10px; margin-top: 10px "> <input id =" comment_text "name =" comment_text "type =" text "maxlength =" 100 "style =" width: 580px; Height: 40px; Background-color: # fffbf0 "/> </div> <Div align =" right "style =" width: 595px; margin-top: 5px "> <! -- Note that the submit label should be used here, otherwise data will not be submitted --> <input type = "Submit" name = "comment_button" onclick = "addcomment () "value =" comment "style =" width: 80px; Height: 25px "/> </div> <! -- Use a hidden tag to submit the "current page number" to the server --> <input type = "hidden" name = "page_index"/> <! -- Use a hidden tag to submit "size per page" to the server --> <input type = "hidden" name = "page_size"/> </form>

Now let's take a look at how the JSP logic on the server processes the data submitted by the customer?

Introduce the JSON package. You can find the JSON Processing Package in the previous article.

Http://blog.csdn.net/wuzh1230/article/details/6613033)

<%@ page import="import org.json.*" %>

<! -- Two global Java objects are defined to store the comment list and resource list. Restarting the server will result in loss --> <%! Jsonarray cmttable = new jsonarray (); %> <%! Jsonarray restable = new jsonarray (); %> <% // obtain the string strcmnt = request. getparameter ("comment_text"); int npageindex = integer. parseint (request. getparameter ("page_index"); int npagesize = integer. parseint (request. getparameter ("page_size"); // Through the above two methods, we get the three parameters submitted by the client through the form // 1, the user's previous comment // 2, user's current page // 3, the number of comments displayed on each page // first, in this example, I used a jsonobect memory global object to store the comments submitted by the user first appended to the JSON file in the comment list. // The method of reading data from the component is not introduced here. // ---- the disadvantage of this is that after Tomcat is restarted, user data will be lost. // Second, read the JSON and parse it into the Java response list. // Finally, retrieve a section in the list based on the current page number and size of each page and return it to the browser. %>

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.