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. %>