【发布时间】:2025-12-28 17:40:17
【问题描述】:
我有一个用于登录过程的servlet类,如果用户名或密码不正确我想为请求设置两个属性,一个属性是布尔authentication_error,第二个是字符串authentication_error_message。
如果authentication_error 为真,我想做一个条件语句来打印authentication_error_message。
req.setAttribute("authentication_error", false);
if(!query.list().isEmpty()){
List<?> list = query.list();
Admin student = (Admin)list.get(0);
req.setAttribute("username", student.getUsername());
}
else{
req.setAttribute("authentication_error_message", "username or password is incorrect!");
req.setAttribute("authentication_error", true);
}
req.getRequestDispatcher("/views/login.jsp").forward(req, res);
如何在jsp中写这个语句?
注意:最好不要使用 jstl。
【问题讨论】:
-
为什么不使用 JSTL ?它比混合 Java、HTML 和 JS 好得多
-
你可以转发到两个不同的jsps,因为有不同的属性,否则jstl
<c:if test="...">仍然是最紧凑的方式。 -
@Dici 因为我已经知道如何使用 jstl 了,所以我只是要求另一种方法,谢谢!
-
@Mohammad 但没有更好的替代方案,仅供您了解