有关List泛型注意点
在学习Javaweb的时候在servlet层我们常常会用到List泛型作为数据的存储。有时候我们想取其中的值用于if(){}else{}
下面展示一些 java类和servlet代码
。
// java类
public class userinfo {
private int id; //ID
private String username; //用户名
private String password; //密码
private int role; //角色:0:管理员 1:普通用户
private int sex; //性别,0:女 1:男
private String note; //备注
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//....以下setting和getting方法省略
}
// javaweb中的servlet层java代码只重写一部分dopost方法
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取输入表单的值省略
//...
//login.roleselect是在service写的方法,把表单数据传入给users
List<userinfo> users = login.roleSelect(username, password);
//想要获取users中的usersname password 等需要先list.get(int index).getting();方法获取
System.out.println(users.get(0).getRole());
}