【发布时间】:2014-05-22 08:53:28
【问题描述】:
我目前正在使用 servlet 和 JSP 在 J2EE 中开发一个中级 Web 应用程序。它就像一个内容管理系统。根据我的需要,我的网站的工作方式非常相似,但是对于在 J2EE 中使用 MVC 的最佳实践和不良实践有一些问题 .
用户登录应用代码为:
<c:choose>
<c:when test="${not empty sessionScope.admin}">
<a href="/context/controller?action=add-content"> + Add a Content</a>
</c:when>
<c:otherwise>
<a href="/context/controller?action=log-in"> Admin Login</a>
</c:otherwise>
</c:choose>
控制器 servlet 中的 Java 代码为:
if (userDAO.isUser(request.getParameter("uname"), request.getParameter("upass"))) {
request.getSession().setAttribute("admin", request.getParameter("uname"));
request.getRequestDispatcher("/admin.jsp").forward(request, response);
} else {
request.getSession().setAttribute("admin", "");
request.getRequestDispatcher("/content.jsp").forward(request, response);
}
注销:
<a href="/context/controller?action=log-out">Logout</a>
控制器 servlet 中的 Java 代码为:
if (action != null && action.equals("log-out")) {
HttpSession session = request.getSession(false);
if(session != null){
session.invalidate();
}
request.getRequestDispatcher("index.jsp").forward(
request, response);
}
我想知道上面的登录、注销和会话管理逻辑是否正确?
而且我也无法阻止用户在退出后返回安全页面,我该如何设置?
【问题讨论】:
-
避免返回不是一种选择。所有浏览器都允许这样做,或者用户可以重新输入 URL 或通过搜索历史记录。您必须考虑用户将始终返回或使用历史记录访问直接页面。因此,请在您的控制器中为此做好准备,并将用户发送到正确的位置。用户万岁!
标签: jakarta-ee session jsp