案例代码实现1

案例代码实现1

写人实体类

package pojo;

public class User {
private int id;
private String name;
private String gender;
private int age;
private String address;
private String qq;
private String email;

public User(int id, String name, String gender, int age, String address, String qq, String email) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.address = address;
this.qq = qq;
this.email = email;
}

public User() {
}

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 getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getQq() {
return qq;
}

public void setQq(String qq) {
this.qq = qq;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", address='" + address + '\'' +
", qq='" + qq + '\'' +
", email='" + email + '\'' +
'}';
}
}

我们在dao层创建一个查询全部的方法接口


public interface UserDao {
public List<User> fileAll();
}

实现接口

public class UserDaoImpl implements UserDao {
private UserDao dao = new UserDaoImpl();
@Override
public List<User> fileAll() {
return dao.fileAll();
}
}

然后在Servlce创建接口,实现接口

public interface UserServlce {
public List<User> fileAll();
}
public class UserServlceImpl implements UserServlce {
@Override
public List<User> fileAll() {
return null;
}
}

在Web包实现跳转

@WebServlet( value = "/UserListServlet")
public class UserListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 调用UserServlet完成查询
UserServlceImpl servlce = new UserServlceImpl();
List<User> users = servlce.fileAll();
request.setAttribute("user",users);
// 转发
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
}


 

posted @ 2022-08-19 09:27  一位程序袁  阅读(23)  评论(0编辑  收藏  举报