package entity;
import java.io.Serializable;
public class Emp implements Serializable{
//使用封装类型
private Integer empno;
private String ename;
private String job;
private Double sal;
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
}
---------------------------------------------------------------------------------------------------------------------------
package Dao;
import java.util.ArrayList;
import java.util.List;
import entity.Emp;
public class EmpDao {
/*
* 模拟查询员工*/
public List<Emp> findAll(){
List<Emp> list=new ArrayList<Emp>();
Emp e1=new Emp();
e1.setEmpno(1);
e1.setEname("唐僧");
e1.setJob("领导");
e1.setSal(10000.0);
list.add(e1);
Emp e2=new Emp();
e2.setEmpno(2);
e2.setEname("孙悟空");
e2.setJob("大师兄");
e2.setSal(9000.0);
list.add(e2);
Emp e3=new Emp();
e3.setEmpno(3);
e3.setEname("八戒");
e3.setJob("二师兄");
e3.setSal(5000.0);
list.add(e3);
return list;
}
}
---------------------------------------------------------------------------------------------------------
package web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Dao.EmpDao;
import entity.Emp;
public class FindEmpServlet extends HttpServlet {
@Override
protected void service(
HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//处理业务,查询全部员工
EmpDao dao=new EmpDao();
List<Emp> list=dao.findAll();
//输出响应信息(根据员工拼table)
res.setContentType("text/html;charset=UTF-8");
PrintWriter out= res.getWriter();
out.println("<table border='1px' width='40%' callspacing='0'>");
out.println(" <tr>");
out.println(" <th>编号</th>");
out.println(" <th>姓名</th>");
out.println(" <th>职位</th>");
out.println(" <th>工资</th>");
out.println(" </tr>");
for(Emp e:list) {
out.println("<tr>");
out.println(" <td>"+e.getEmpno()+"</td>");
out.println(" <td>"+e.getEname()+"</td>");
out.println(" <td>"+e.getJob()+"</td>");
out.println(" <td>"+e.getSal()+"</td>");
out.println("</tr>");
}
out.println("</table>");
out.close();
}
}