Java Web 应用程序 - 将 scriptlet 重构为最佳实践方法

标签 java web-applications jsp servlets

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

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

jsp 具有 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);
    }
}
%>

最佳答案

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

关于Java Web 应用程序 - 将 scriptlet 重构为最佳实践方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4402413/

相关文章:

java - 页面刷新时"Address already in use"

java - struts2插件如何工作

javascript - load() - 不加载

Java - 我的商业软件可以免费嵌入哪个商业智能 (BI) 平台?

java - 包更改后在tomcat中发布失败

Java - 如何在没有对象引用的情况下使用方法

java - 是否有用于 java 的 Backgroundworker(例如在 .Net 中)

java - 使用 OO 原则重构过程方法

java集合用作 map 和检索数据

java - 通过 JSP 将变量传递给 Java?