Struts2访问servlet分享

雾,没有山河那么壮观,没有雨那么缠绵。它总是那么轻盈,那么淡雅,给人一种美的享受。陽光被层层叠叠的树叶过滤,漏到他身上变成了淡淡的圆圆的轻轻摇曳的光晕。

一:访问或添加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;

}

}

到此这篇关于Struts2访问servlet分享就介绍到这了。最可怕的敌人,就是没有坚定的信念。更多相关Struts2访问servlet分享内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

标签: servlet