JavaEE servlet setAttribute 的用途
JavaEE servlet setAttribute what for
希望大家能帮我理解servlet方法doGet中的case。
例如,我们有一个用户class,名字和姓氏,当然getters和setters都设置好了。附加信息位于代码的注释中。问题是,在这种情况下 SetAttribute 的目的是什么?我应该使用它吗?打开和关闭时我看不出有什么区别。预先感谢您的及时回复。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//here I initiate servlet to create a session yes?
HttpSession session = request.getSession(true);
// here im getting atribiute which is goinna be null now:
User user = (User) session.getAttribute("user");
//now in case of null i want to create object which can have null atributes like name and lastname:
if(user == null) {
user = createUser(request);
//here is my problem, i dont know what is it for? I already get atrributes, why would i want to set object again? to what?
session.setAttribute("user", user);
}
returnInfo(response,user);
}
protected User createUser(HttpServletRequest request) {
User user = new User();
String name = request.getParameter("name");
String lastname = request.getParameter("lastname");
user.setName(name);
user.setSurname(lastname);
return user;
}
protected void returnInfo(HttpServletResponse response,User user) throws IOException {
PrintWriter write = response.getWriter();
write.println("<html>");
write.println("<body>");
write.println("<div>");
if(user.getName() == null && user.getSurname() == null) {
write.println("no person was added");
}
else{
write.println("person is" + " " + user.getName() + " " + user.getSurname());
}
write.println("</div>");
write.println("</body>");
write.println("</html>");
}
您刚刚创建了一个新用户。它必须添加到会话中,否则您的用户属性将始终为空。
希望大家能帮我理解servlet方法doGet中的case。
例如,我们有一个用户class,名字和姓氏,当然getters和setters都设置好了。附加信息位于代码的注释中。问题是,在这种情况下 SetAttribute 的目的是什么?我应该使用它吗?打开和关闭时我看不出有什么区别。预先感谢您的及时回复。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//here I initiate servlet to create a session yes?
HttpSession session = request.getSession(true);
// here im getting atribiute which is goinna be null now:
User user = (User) session.getAttribute("user");
//now in case of null i want to create object which can have null atributes like name and lastname:
if(user == null) {
user = createUser(request);
//here is my problem, i dont know what is it for? I already get atrributes, why would i want to set object again? to what?
session.setAttribute("user", user);
}
returnInfo(response,user);
}
protected User createUser(HttpServletRequest request) {
User user = new User();
String name = request.getParameter("name");
String lastname = request.getParameter("lastname");
user.setName(name);
user.setSurname(lastname);
return user;
}
protected void returnInfo(HttpServletResponse response,User user) throws IOException {
PrintWriter write = response.getWriter();
write.println("<html>");
write.println("<body>");
write.println("<div>");
if(user.getName() == null && user.getSurname() == null) {
write.println("no person was added");
}
else{
write.println("person is" + " " + user.getName() + " " + user.getSurname());
}
write.println("</div>");
write.println("</body>");
write.println("</html>");
}
您刚刚创建了一个新用户。它必须添加到会话中,否则您的用户属性将始终为空。