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 mohu = request.getParameter("mohu");
String cpage = request.getParameter("cpage");
if(cpage==null){
cpage="1";
}
Integer pageSize = 2;
Integer count = service.getStudentCount(mohu);
PageUtils pu = new PageUtils(Integer.valueOf(cpage),pageSize,count);
List<Student> stuList = service.getStudentList(pu,mohu);
request.setAttribute("mohu", mohu);
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 mohu) {
// TODO Auto-generated method stub
return dao.getStudentCount(mohu);
}
public List<Student> getStudentList(PageUtils pu, String mohu) {
// TODO Auto-generated method stub
return dao.getStudentList(pu,mohu);
}
}
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 mohu) {
// TODO Auto-generated method stub
String sql = "select count(*) from t_student s left join t_dept c on s.cid = c.cid";
if(mohu != null){
sql += " where name like '%"+mohu+"%' ";
}
return JdbcUtils.getListCount(sql);
}
public List<Student> getStudentList(PageUtils pu, String mohu) {
// 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(mohu != null){
sql += " where name like '%"+mohu+"%' ";
}
sql += " limit "+pu.getStartIndex()+","+pu.getPageSize();
return JdbcUtils.getList(Student.class, sql);
}
}
当你凝视深渊时,深渊也在凝视着你。
《善恶的彼岸》
——尼采