原始数据:
package com.view; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Loin */ @WebServlet("/Loin") public class LoinServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoinServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //浏览器uft-8展示 response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); //返回一个界面 // out.println("你好"); out.println("<h1>用户登录</h1>"); out.println("<form action='/user/ControlServlet' method='post'>"); out.println("id:<input type='text' name='id'></br>"); out.println("密 码:<input type='password' name='password'></br>"); out.println("<input type='submit' value='登录'></br>"); out.println("</form>"); String errInof=(String)request.getAttribute("err"); if(errInof!=null){ out.println("<font color='red'>"+errInof+"</font>"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request,response); } }
分页技术:
分页代码;
ManagerUser.java
package com.view; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ManagerUser */ @WebServlet("/ManagerUser") public class ManagerUser extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ManagerUser() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out=response.getWriter(); out.print("<h1>管理用户</h1>欢迎登陆<a href='/user/ControlServlet'>返回主界面</a> <a href='/user/ControlServlet'>安全退出</a>"); //从数据库中取出数据,并显示 Connection conn = null; PreparedStatement ps = null; Statement stmt = null; ResultSet rs = null; //定义分页所需要的变量 int pageNow=4;//当前页 int pageSize=3;//每页3条记录 int pageCount=1; int rowCount=1; //接收用户的pageNow String spageNow=request.getParameter("pageNow"); if(spageNow!=null){ pageNow=Integer.parseInt(spageNow); } try {// 加载驱动 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("建立连接失败"); } try {// 创建连接对象 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/user", "root", "123456"); stmt = conn.createStatement();// 创建sql操作对象 //统计有多少行 ps=conn.prepareStatement("select count(*) from user"); rs=ps.executeQuery(); rs.next(); rowCount=rs.getInt(1); //算出有多少页 pageCount=rowCount%pageSize==0? rowCount/pageSize:rowCount/pageSize+1; ps = conn.prepareStatement("select * from user where id<="+pageSize*pageNow+" and id>="+(pageSize*(pageNow-1)+1)); // 赋值 rs = ps.executeQuery(); out.print("<table border=1px bordercolor=green cellspacing=0 width=500px>");//美观 out.print("<tr><th>id</th><th>用户名</th><th>email</th><th>级别</th></tr>"); while(rs.next()) { out.println("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td></tr>"); } out.print("</table>"); //显示分页 for(int i=1;i<=pageCount;i++){ out.print("<a href='/user/ManagerUser?pageNow="+i+"'><" +i+"></a> "); } } catch (SQLException e) { e.printStackTrace(); System.out.println("建立连接失败"); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }