Java web simple page display, javaweb page display
Digress: This paging display is implemented using the design concept of "presentation layer-control layer-DAO layer-Database". If you need to improve it, you can make a joint learning and progress.
Ideas: You must first provide the paging query method in the DAO object. You can call this method at the control layer to find the data on the specified page. In the presentation layer, the data on the specified page is displayed through EL expressions and JSTL.
Key Points: Two methods: (1) calculate the total number of pages. (2) query data on the specified page.
1. DAO layer-Database
The JDBCUtils class is used to open and close a database. The core code is as follows:
1 import java. SQL. connection; 2 import java. SQL. driverManager; 3 import java. SQL. preparedStatement; 4 import java. SQL. resultSet; 5 import java. SQL. SQLException; 6 7 public class JDBCUtils {8 private Connection conn = null; 9 private PreparedStatement pstmt = null; 10 11 12/** 13 * connect to the database 14 * @ return15 */16 public Connection connect () {17 String user = "root"; 18 String password = "1234 "; 19 String dri VerClass = "com. mysql. jdbc. driver "; 20 String jdbcUrl =" jdbc: mysql: // localhost: 3306/book "; 21 22 try {23 Class. forName (driverClass); 24 conn = DriverManager. getConnection (jdbcUrl, user, password); 25} catch (Exception e) {26 // TODO Auto-generated catch block27 e. printStackTrace (); 28} 29 return conn; 30 31} 32 33/** 34 * close database 35 * @ param conn36 * @ param pstmt37 * @ param resu38 */39 public void Close (Connection conn, PreparedStatement pstmt, ResultSet result) {40 if (conn! = Null) {41 try {42 conn. close (); 43} catch (SQLException e) {44 // TODO Auto-generated catch block45} 46} 47 if (pstmt! = Null) {48 try {49 pstmt. close (); 50} catch (SQLException e) {51 // TODO Auto-generated catch block52 e. printStackTrace (); 53} 54} 55 if (result! = Null) {56 try {57 result. close (); 58} catch (SQLException e) {59 // TODO Auto-generated catch block60 e. printStackTrace (); 61} 62} 63} 64 65}
The getPage () and listUser () methods in the UserDao class are used to calculate the total number of pages and query the data on a specified page respectively. The core code is as follows:
1 import java. SQL. connection; 2 import java. SQL. preparedStatement; 3 import java. SQL. resultSet; 4 import java. SQL. SQLException; 5 import java. util. arrayList; 6 import java. util. list; 7 8 import com. db. JDBCUtils; 9 10 public class UserDao {11/** 12 * calculate the total number of pages 13 * @ return14 */15 public int getPage () {16 int recordCount = 0, t1 = 0, t2 = 0; 17 PreparedStatement pstmt = null; 18 ResultSet result = null; 19 JDBCUtils jdb C = new JDBCUtils (); 20 Connection conn = jdbc. connect (); 21 String SQL = "select count (*) from books"; 22 try {23 pstmt = conn. prepareStatement (SQL); 24 resulttables pstmt.exe cuteQuery (); 25 result. next (); 26 recordCount = result. getInt (1); 27 t1 = recordCount % 5; 28 t2 = recordCount/5; 29} catch (Exception e) {30 // TODO Auto-generated catch block31 e. printStackTrace (); 32} finally {33 jdbc. close (conn, pstmt, result); 34} 35 I F (t1! = 0) {36 t2 = t2 + 1; 37} 38 39 return t2; 40} 41 42/** 43 * query data on a specified page 44 * @ param pageNo45 * @ return46 */47 public List <User> listUser (int pageNo) {48 PreparedStatement pstmt = null; 49 ResultSet result = null; 50 List <User> list = new ArrayList <User> (); 51 int pageSize = 5; 52 int page = (pageNo-1) * 5; 53 JDBCUtils jdbc = new JDBCUtils (); 54 Connection conn = jdbc. connect (); 55 String SQL = "select * from books order by id limit ?,? "; 56 try {57 pstmt = conn. prepareStatement (SQL); 58 pstmt. setInt (1, page); 59 pstmt. setInt (2, pageSize); 60 resultextends pstmt.exe cuteQuery (); 61 while (result. next () {62 User user = new User (); 63 user. setId (result. getInt (1); 64 user. setName (result. getString (2); 65 user. setNumber (result. getString (3); 66 list. add (user); 67 68} 69} catch (Exception e) {70 // TODO Auto-generated catch block71 e. printStackTrace (); 72} finally {73 jdbc. close (conn, pstmt, result); 74} 75 return list; 76} 77 78}
The User class is used to store the queried data. The core code is as follows:
1 public class User { 2 private int id; 3 private String name; 4 private String number; 5 public int getId() { 6 return id; 7 } 8 public void setId(int id) { 9 this.id = id;10 }11 public String getName() {12 return name;13 }14 public void setName(String name) {15 this.name = name;16 }17 public String getNumber() {18 return number;19 }20 public void setNumber(String number) {21 this.number = number;22 }23 }
2. Control Layer
The ListUser class calls the UserDao object to query data and assigns the page to display data. The core code is as follows:
1 import java.io.IOException; 2 import java.io.PrintWriter; 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 11 import com.dao.User;12 import com.dao.UserDao;13 14 public class ListUser extends HttpServlet {15 public ListUser() {16 super();17 }18 19 public void destroy() {20 super.destroy(); // Just puts "destroy" string in log21 // Put your code here22 }23 24 public void doGet(HttpServletRequest request, HttpServletResponse response)25 throws ServletException, IOException {26 27 doPost(request, response);28 }29 30 public void doPost(HttpServletRequest request, HttpServletResponse response)31 throws ServletException, IOException {32 33 34 response.setCharacterEncoding("utf-8");35 int pageNo = 1;36 UserDao userdao=new UserDao();37 List<User> lists=new ArrayList<User>();38 String pageno=request.getParameter("pageNos");39 if(pageno != null){40 pageNo=Integer.parseInt(pageno);41 }42 lists=userdao.listUser(pageNo);43 int recordCount=userdao.getPage();44 request.setAttribute("recordCount", userdao.getPage());45 request.setAttribute("listss", lists);46 request.setAttribute("pageNos", pageNo);47 request.getRequestDispatcher("userlist.jsp").forward(request, response);48 }49 50 public void init() throws ServletException {51 // Put your code here52 }53 54 }
3. Presentation Layer
Output page userlist. jsp. Use EL and JSTL to output the query result. The core code is as follows:
1 <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> 2 <% @ taglib prefix =" c "uri =" http://java.sun.com/jsp/jstl/core "%> 3 <% @ taglib uri =" http://java.sun.com/jsp/jstl/fmt "prefix =" fmt "%> 4 <% @ taglib uri =" http://java.sun.com/jsp/jstl/functions "prefix =" fn "%> 5 <% 6 String path = request. getContextPath (); 7 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + req Uest. getServerPort () + path + "/"; 8%> 9 10 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 11
4.