一:訪問或添加request/session/application屬性
復制代碼 代碼如下:
public String scope() throws Exception{
ActionContext ctx = ActionContext.getContext();
ctx.getApplication().put("app", "應用范圍");//往ServletContext裡放入app
ctx.getSession().put("ses", "session范圍");//往session裡放入ses
ctx.put("req", "request范圍");//往request裡放入req
return "scope";
}
JSP:
復制代碼 代碼如下:
<body>
${applicationScope.app} <br>
${sessionScope.ses}<br>
${requestScope.req}<br>
</body>
二:獲取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse對象
方法一,通過ServletActionContext.類直接獲取:
復制代碼 代碼如下:
public String rsa() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
ServletContext servletContext = ServletActionContext.getServletContext();
request.getSession()
HttpServletResponse response = ServletActionContext.getResponse();
return "scope";
}
方法二,實現指定接口,由struts框架運行時注入:
復制代碼 代碼如下:
public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{
private HttpServletRequest request;
private ServletContext servletContext;
private HttpServletResponse response;
public void setServletRequest(HttpServletRequest req) {
this.request=req;
}
public void setServletResponse(HttpServletResponse res) {
this.response=res;
}
public void setServletContext(ServletContext ser) {
this.servletContext=ser;
}
}