色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

Struts2訪問servlet分享

一:訪問或添加request/session/application屬性
 
復(fù)制代碼 代碼如下:
public String scope() throws Exception{

   ActionContext ctx = ActionContext.getContext();

   ctx.getApplication().put("app", "應(yīng)用范圍");//往ServletContext里放入app

   ctx.getSession().put("ses", "session范圍");//往session里放入ses

   ctx.put("req", "request范圍");//往request里放入req

   return "scope";

}

 

JSP:
復(fù)制代碼 代碼如下:
 <body>

    ${applicationScope.app} <br>

    ${sessionScope.ses}<br>

    ${requestScope.req}<br>

 </body>

 

二:獲取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse對(duì)象
 

方法一,通過ServletActionContext.類直接獲取:
復(fù)制代碼 代碼如下:
public String rsa() throws Exception{

       HttpServletRequest request = ServletActionContext.getRequest();

       ServletContext servletContext = ServletActionContext.getServletContext();

       request.getSession()

       HttpServletResponse response = ServletActionContext.getResponse();

       return "scope";

}

方法二,實(shí)現(xiàn)指定接口,由struts框架運(yùn)行時(shí)注入:
復(fù)制代碼 代碼如下:
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;

       }

}

jsp技術(shù)Struts2訪問servlet分享,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 淮阳县| 雅江县| 金乡县| 潼关县| 清水河县| 宜黄县| 汉寿县| 青神县| 吐鲁番市| 武宣县| 正蓝旗| 四子王旗| 庆安县| 龙岩市| 望江县| 乐亭县| 乐平市| 宝山区| 泰顺县| 雅江县| 镇江市| 渝北区| 兴仁县| 巴东县| 原平市| 且末县| 苏尼特左旗| 嘉鱼县| 翁牛特旗| 七台河市| 兴城市| 花莲市| 黔西| 土默特右旗| 长宁区| 大邑县| 明水县| 洪江市| 郯城县| 荃湾区| 大洼县|