这里我就简单的把代码晒出来下:
ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
ServletConfig config = (ServletConfig)request.getSession().getServletContext().getAttribute("servletConfig");
其中,config里的attribute在servlet中定义,在程序启动的时候就执行,代码如下:
public void init(ServletConfig config) throws ServletException {
super.init(config);
config.getServletContext().setAttribute("servletConfig", config);
}
在web.xml中的配置
<servlet>
<servlet-name>initServlet</servlet-name>
<servlet-class>com.bkstore.action.common.initServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>initServlet</servlet-name>
<url-pattern>/initServlet</url-pattern>
</servlet-mapping>
其中
<load-on-startup>表示的是程序执行的等级.它可以是负数,也可以是1~5中的整数
本文详细解析了Web应用启动时初始化Servlet的过程,包括如何将Servlet配置信息存入Session域,以及在web.xml文件中配置加载顺序的重要性。通过实例展示了如何在启动阶段设置全局配置参数,供后续请求使用。
3295

被折叠的 条评论
为什么被折叠?



