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;
import com.www.util.PageUtils;
public class ServletStudent extends HttpServlet {
private StudentService service = new StudentService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
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 * = request.getParameter("*");
String cpage = request.getParameter("cpage");
if(cpage==null){
cpage="1";
}
Integer pageSize = 2;
Integer count = service.getStudentCount(*);
PageUtils pu = new PageUtils(Integer.valueOf(cpage),pageSize,count);
List<Student> stuList = service.getStudentList(pu,*);
request.setAttribute("*", *);
request.setAttribute("pu", pu);
request.setAttribute("stuList", stuList);
request.getRequestDispatcher("list.jsp").forward(request, response);
}
}
package com.www.service;
import java.util.List;
import com.www.dao.StudentDao;
import com.www.entity.Student;
import com.www.util.PageUtils;
public class StudentService {
private StudentDao dao = new StudentDao();
public Integer getStudentCount(String *) {
// TODO Auto-generated method stub
return dao.getStudentCount(*);
}
public List<Student> getStudentList(PageUtils pu, String *) {
// TODO Auto-generated method stub
return dao.getStudentList(pu,*);
}
}
package com.www.dao;
import java.util.List;
import com.www.entity.Student;
import com.www.util.JdbcUtils;
import com.www.util.PageUtils;
public class StudentDao {
public Integer getStudentCount(String *) {
// TODO Auto-generated method stub
String sql = "select count(*) from t_student s left join t_dept c on s.cid = c.cid";
if(* != null){
sql += " where name like '%"+*+"%' ";
}
return JdbcUtils.getListCount(sql);
}
public List<Student> getStudentList(PageUtils pu, String *) {
// TODO Auto-generated method stub
String sql = "select s.*,c.dname from t_student s left join t_dept c on s.cid=c.cid ";
if(* != null){
sql += " where name like '%"+*+"%' ";
}
sql += " limit "+pu.getStartIndex()+","+pu.getPageSize();
return JdbcUtils.getList(Student.class, sql);
}
}
当你凝视深渊时,深渊也在凝视着你。
《善恶的彼岸》
——尼采