5月12日 CMS 周六

package com.www.entity;

public class Student {
    private int id;
    private String name;
    private String sex;
    private int age;
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(int id, String name, String sex, int age) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", sex=" + sex
                + ", 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 getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


}
package com.www.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.www.entity.Student;

public class StudentDao {

    public List<Student> stulist(Integer cpage, Integer pageSize) {
        // TODO Auto-generated method stub
        List<Student> stulist = new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql:///cms01","root","root");
            PreparedStatement pst = con.prepareStatement("select * from student limit "+(cpage-1)*pageSize+","+pageSize);
            ResultSet rs = pst.executeQuery();

            while(rs.next()){
                Student s = new Student(rs.getInt("id"),rs.getString("name"),rs.getString("sex"),rs.getInt("age"));
                stulist.add(s);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stulist;
    }

    public Integer count() {
        // TODO Auto-generated method stub
        Integer count = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql:///cms01","root","root");
            PreparedStatement pst = con.prepareStatement("select count(*) from student");
            ResultSet rs = pst.executeQuery();

            if(rs.next()){
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return count;
    }

}
package com.www.service;

import java.util.List;

import com.www.dao.StudentDao;
import com.www.entity.Student;

public class StudentService {
    private StudentDao dao = new StudentDao();

    public List<Student> stulist(Integer cpage, Integer pageSize) {
        // TODO Auto-generated method stub
        return dao.stulist(cpage,pageSize);
    }

    public Integer count() {
        // TODO Auto-generated method stub
        return dao.count();
    }
}
package com.www.servlet;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.www.entity.Student;
import com.www.service.StudentService;

public class StudentServlet extends HttpServlet {
    private StudentService service = new StudentService(); 
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String m = request.getParameter("m");

        if("list".equals(m)){
            list(request,response);
        }
    }
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String cpage = request.getParameter("cpage");
        if(cpage==null){
            cpage="1";
        }
        Integer pageSize = 2;

        Integer count = service.count();
        Integer totalpage = count/pageSize + (count%pageSize==0?0:1);
        System.out.println("totalpage"+totalpage);

        List<Student> stulist = service.stulist(Integer.valueOf(cpage),pageSize);
        request.setAttribute("cpage", cpage);
        request.setAttribute("totalpage", totalpage);
        request.setAttribute("stulist", stulist);
        request.getRequestDispatcher("list.jsp").forward(request,response);

    }
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'list.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  <script type="text/javascript" src="js/jquery-1.8.2.js"></script>
  <link rel="stylesheet" href="css/index_work.css" type="text/css"></link>
    <script type="text/javascript">
        function gopage(cpage){
            location = "student?m=list&cpage="+cpage;

        }
        function jumppage(){
            var cpage = $("#d1").val();
            if(cpage>=1 && cpage<="${totalpage}"){
                gopage(cpage);
            }else{
                alert("输入错误");
            }
        }
    </script>
  </head>

  <body>
    <table>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>sex</th>
            <th>age</th>
        </tr>
        <c:forEach var="s" items="${stulist}">
            <tr>
                <th>${s.id}</th>
                <th>${s.name}</th>
                <th>${s.sex}</th>
                <th>${s.age==1?"新建":s.age==2?"保存":"未保存"}</th>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="21"><input type="button" value="首页" onclick="gopage(1)">
            <input type="button" value="上页" onclick="gopage(${cpage<=1?1:(cpage-1)})">
            <input type="button" value="下页" onclick="gopage(${cpage>=totalpage?totalpage:(cpage+1)})">
            <input type="button" value="尾页" onclick="gopage(${totalpage})">
            <input type="text" style="width: 30px;" id="d1" value="${cpage}">/${totalpage}页
            <input type="button" value="跳" onclick="jumppage()"></th>
        </tr>
    </table>
  </body>
</html>

别不好意思拒绝别人,反正那些好意思为难你的人都不是什么好人。

猜你喜欢

转载自blog.csdn.net/helloworld_1996/article/details/80295550
今日推荐