前言
Servlet API就是HttpServletRequest(Request)、HttpSession(Session)、ServletContext(Application)
通过ActionContext类访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ActionContext ctx = ActionContext.getContext();
Map<String, Object> application = ctx.getApplication(); application.put("key", value); Object value = application.get("key"); ctx.setApplication(map);
Map<String, Object> session= ctx.getSession(); application.put("key", value); Object value = application.get("key"); ctx.setSession(map);
ctx.put("key", value); Object value = ctx.get("key"); Map<String, Object> request = ctx.getParameters();
|
实现XxxAware接口
ServletContextAware
:直接访问ServletContext(application)
实例
ServletRequestAware
:直接访问HttpServletRequest(request)
实例
ServletResponseAware
:直接访问HttpServletResponse(response)
实例
通过ServletActionContext类访问
1 2 3 4
| PageContext pageContext = ServletActionContext.getPageContext(); HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); ServletContext application = ServletActionContext.getServletContext();
|