struts2中訪問request和session

來源:互聯網
上載者:User

第一種方法:

 

Request:

通過com.opensymphony.xwork2

.ActionContext類以及org.apache.struts

2

.ServletActionContext類

HttpServletRequest

request

= ServletActionContext.getRequest

();

ActionContext context= ActionContext.getContext()

HttpServletRequest

request

=(HttpServletRequest

)ct.get(ServletActionContext.HTTP_REQUEST

);

 

Session:

Map session=ActionContext.getSession();

Map session=(Map)ActionContext.getContext().get(ActionContext.SESSION);

 

註:sturt2中session都被封裝成了Map類型,我們稱之為SessionMap如果我們想得到原始的HttpSession可以首先得到HttpServletRequest

對象,然後通過request

.getSession()來取得原始的HttpSession對象。一般情況下SessionMap已經可以完成所有的工作,我們不必再去碰底層的session了。

 

第二種方法:

 

 

public class TestAction implements ServletRequestAware, ServletResponseAware {<br /> protected HttpServletRequest request;<br /> protected HttpServletResponse response;<br /> protected HttpSession session;<br /> @Override<br /> public void setServletRequest(HttpServletRequest httpServletRequest) {<br /> this.request = httpServletRequest;<br /> this.session = httpServletRequest.getSession();<br /> }<br /> @Override<br /> public void setServletResponse(HttpServletResponse httpServletResponse) {<br /> this.response = httpServletResponse;<br /> }<br />}<br />

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.