Struts2访问servlet分享_JSP教程

编辑Tag赚U币

推荐:javaweb之web入门基础
javaweb之web入门基础,需要的朋友可以参考一下

一:访问或添加request/session/application属性

复制代码 代码如下:www.mb5u.com

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:

复制代码 代码如下:www.mb5u.com

<body>

${applicationScope.app} <br>

${sessionScope.ses}<br>

${requestScope.req}<br>

</body>


二:获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象

方法一,通过ServletActionContext.类直接获取:

复制代码 代码如下:www.mb5u.com

public String rsa() throws Exception{

HttpServletRequest request = ServletActionContext.getRequest();

ServletContext servletContext = ServletActionContext.getServletContext();

request.getSession()

HttpServletResponse response = ServletActionContext.getResponse();

return "scope";

}


方法二,实现指定接口,由struts框架运行时注入:
复制代码 代码如下:www.mb5u.com

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;

}

}

分享:ssh生成随机数字验证码操作步骤
随机数字验证码生成方法很多,接下来将介绍下jsp中时如何实现的,感兴趣的朋友可以参考下,希望本文对你有所帮助

来源:模板无忧//所属分类:JSP教程/更新时间:2013-04-22
相关JSP教程