国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

Struts2訪問servlet分享

2020-07-27 21:38:57
字體:
供稿:網(wǎng)友

一:訪問或添加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;

       }

}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 和顺县| 涞水县| 岢岚县| 永胜县| 体育| 恩平市| 钟山县| 韶关市| 怀仁县| 泰顺县| 京山县| 门头沟区| 北宁市| 大渡口区| 丹凤县| 新竹县| 东城区| 疏附县| 富顺县| 琼结县| 建瓯市| 鄂托克前旗| 郴州市| 高唐县| 花莲县| 江达县| 临漳县| 常州市| 百色市| 铜川市| 中方县| 孙吴县| 阳山县| 宕昌县| 股票| 绥阳县| 大同县| 禹城市| 哈密市| 磐石市| 杭州市|