【问题标题】:Java web application - refactor include scriptlets into a best practice approachJava Web 应用程序 - 将 scriptlet 重构为最佳实践方法
【发布时间】:2010-12-09 19:47:09
【问题描述】:

我目前在所有 jsp 文件的顶部包含以下内容:

<%@ include file="inc/inc_cookie_login.jsp"%>
<%@ include file="inc/inc_protect_page.jsp"%>
<%@ include file="inc/inc_log_access.jsp"%>

jsps 有分别check for cookie and set a user object in the session if cookie exists, prevents access to the jsp unless a session has been set, write to a text file the User IP, name, page accessed, etc.,@的scriptlet。

上面的 scriptlet 方法运行良好,但现在我设置了更好的服务器并且可以利用 web.xml 文件,我一直在重构我的应用程序以实现最佳实践。以上是在尖叫FIXME!我应该调查侦听器、过滤器吗?还是我目前的方法足够?

=== inc_cookie_login.jsp ====

<%@ page import="model.STKUser"%>
<%@ page import="model.STKUserCookie"%>
<%@ page import="data.STKUserDAO"%>

<%
if ( request.getSession().getAttribute("STKUserSession") == null) {
    STKUserCookie userCookie = new STKUserCookie(request);
    String userBadge = userCookie.getUserID();
    STKUserDAO userDAO = new STKUserDAO();
    STKUser user = userDAO.getUser(userBadge);
    if (user != null) {
        user.setIpAddress(request.getRemoteAddr());
        userDAO.updateLoginCount(user);
        request.getSession().setMaxInactiveInterval(36000); //set to 10 hours
        request.getSession().setAttribute("STKUserSession", user);
    }
}
%>

【问题讨论】:

    标签: java web-applications jsp servlets


    【解决方案1】:

    这看起来不错,可以用过滤器代替。创建过滤器类并使用 web.xml 中的模式引用它。除非所有其他选项都已合理用尽,否则不应使用 Scriptlet。

    【讨论】:

    • 监听新的 HTTP 会话创建不是 cookie 登录的更好选择吗?在每次访问页面时继续检查 STKUserSession 是否为空似乎有点过头了。每个访客应该做一次,对吧?
    • 如果您可以保证会话不为空时它会存在,那么每个访问者应该只有一次。根据实现,您最终可能会得到一个不包含 STKUserSession 的有效会话。但总的来说,是的,我同意新会话的听众可能会更好。
    猜你喜欢
    • 2011-02-18
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多