struts2中如何獲取Session,HttpServletRequest,HttpServletResponse
1,How do we get access to the session?
You can obtain the session attributes by asking the ActionContext or implementing SessionAware. Implementing SessionAware is preferred.
Map attibutes = ActionContext.getContext().getSession();
2,How can we access the HttpServletRequest
You can obtain the request by asking the ActionContext or implementing ServletRequestAware. Implementing ServletRequestAware is preferred.
HttpServletRequest request = ServletActionContext.getRequest();
3,How can we access the HttpServletResponse
You can obtain the request by asking the ActionContext or implementing ServletResponseAware. Implementing ServletResponseAware is preferred.
The response is available on the ActionContext instance, which is made available via ThreadLocal.
HttpServletResponse response = ServletActionContext.getResponse();
You can obtain the session attributes by asking the ActionContext or implementing SessionAware. Implementing SessionAware is preferred.
Ask the ActionContext

Implement SessionAware
The session attributes are available on the ActionContext instance, which is made available via ThreadLocal. _Preferred_
- Ensure that servlet-config Interceptor is included in the Action's stack.
The default stack already includes servlet-config.
- Edit the Action so that it implements the SessionAware interface.
- The SessionAware interface expects a setSession method. You may wish to include a companion getSession method.
- At runtime, call getSession to obtain a Map representing the session attributes.
- Any changes made to the session Map are reflected in the actual HttpSessionRequest. You may insert and remove session attributes as needed.
-
Map parameters = this.getSession();
2,How can we access the HttpServletRequest
You can obtain the request by asking the ActionContext or implementing ServletRequestAware. Implementing ServletRequestAware is preferred.
Ask the ActionContext
The request is available on the ActionContext instance, which is made available via ThreadLocal.
Implement ServletRequestAware
Preferred
- Ensure that servlet-config Interceptor is included in the Action's stack.
The default stack already includes servlet-config.
- Edit the Action so that it implements the ServletRequestAware interface.
- The ServletRequestAware interface expects a setServletRequest method. You may wish to include a companion getServletRequest method.
- At runtime, call getServletRequest to obtain a reference to the request object.
3,How can we access the HttpServletResponse
You can obtain the request by asking the ActionContext or implementing ServletResponseAware. Implementing ServletResponseAware is preferred.
Ask the ActionContext
The response is available on the ActionContext instance, which is made available via ThreadLocal.

Implement ServletResponseAware
Preferred
- Ensure that servlet-config Interceptor is included in the Action's stack.
The default stack already includes servlet-config.
- Edit the Action so that it implements the ServletResponseAware interface.
- The ServletResponseAware interface expects a setServletResponse method. You may wish to include a companion getServletResponse method.
- At runtime, call getServletResponse to obtain a reference to the response object.
posted on 2009-03-29 16:40 草原上的駱駝 閱讀(3771) 評論(0) 編輯 收藏 所屬分類: JAVA框架