關鍵字: struts2 httpservletrequest httpsessionjava
在沒有使用Struts2以前,都習慣使用HttpServletRequest和HttpSession對象來操做相關參數,下面介紹一下在Struts2中訪問隱藏的HttpServletRequest和HttpSession的兩種方法:apache
1.非Ioc方式session
這種方式主要是利用了com.opensymphony.xwork2.ActionContext類以及ora.apache.struts2.ServletActionContext類,具體的方法以下所示。app
得到request對象:ide
1).HttpServletRequest request = ServletActionContext.getRequest ();this
2).ActionContext ct= ActionContext.getContext();spa
HttpServletRequest request=code
(HttpServletRequest)ct.get(ServletActionContext.HTTP_REQUEST);orm
得到session對象:對象
在Struts2中底層的session都被封裝成了Map類型,咱們稱之爲SessionMap,而日常咱們所說的session則是指HttpSession對象,具體的得到方法以下所示。
1).Map session=ActionContext.getSession();
2).Map session=(Map)ActionContext.getContext().get(ActionContext.SESSION);
獲得這個SessionMap以後咱們就能夠對session進行讀寫了,若是咱們想獲得原始的HttpSession能夠首先獲得HttpServletRequest對象,而後經過request.getSession()來取得原始的HttpSession對象。通常狀況下SessionMap已經能夠完成全部的工做,咱們沒必要再去強行使用底層的session了。
2.IoC方式
這種方式相對來講變化就比較少了,下面給出代碼你們本身看吧:
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; publicclass IoCServlet extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware { private String message; private Map att; private HttpServletRequest request; private HttpServletResponse response; public String getMessage() { return message; } publicvoid setSession(Map att) { this.att = att; } publicvoid setServletRequest(HttpServletRequest request) { this.request = request; } publicvoid setServletResponse(HttpServletResponse response) { this.response = response; } @Override public String execute() { att.put("msg", "Hello World from Session!"); HttpSession session = request.getSession(); StringBuffer sb =new StringBuffer("Message from request: "); sb.append(request.getParameter("msg")); sb.append("<br>Response Buffer Size: "); sb.append(response.getBufferSize()); sb.append("<br>Session ID: "); sb.append(session.getId()); message = sb.toString(); return SUCCESS; } }
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; publicclass IoCServlet extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware { private String message; private Map att; private HttpServletRequest request; private HttpServletResponse response; public String getMessage() { return message; } publicvoid setSession(Map att) { this.att = att; } publicvoid setServletRequest(HttpServletRequest request) { this.request = request; } publicvoid setServletResponse(HttpServletResponse response) { this.response = response; } @Override public String execute() { att.put("msg", "Hello World from Session!"); HttpSession session = request.getSession(); StringBuffer sb =new StringBuffer("Message from request: "); sb.append(request.getParameter("msg")); sb.append("<br>Response Buffer Size: "); sb.append(response.getBufferSize()); sb.append("<br>Session ID: "); sb.append(session.getId()); message = sb.toString(); return SUCCESS; } }