非IoC方式访问Servlet API的两种方法

使用ActionContext访问
ActionContext来自com.opensymphony.xwork2.ActionContext

具体实现代码段如下:

<span style="font-size:14px;">public class UserAction01 extends ActionSupport {
	
	private String username;
	private String password;
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;	
	
	public String login(){
		//获取对象
		ActionContext ac=ActionContext.getContext();
		request=(Map<String, Object>)ac.get("request");
		session=ac.getSession();
		application=ac.getApplication();
		//保存数据
		request.put("loginAddress", "您本次登录地点是:北京");
		session.put("username",username);
		application.put("online","当前在线人数:XXX");</span>
此种方式获得的request,session,application均为Map集合形式。

——————————————————————————————————————————

使用ServletActionContext访问
ServletActionContext来自org.apache.struts2.ServletActionContext
具体实现代码段如下:

<span style="font-family:Comic Sans MS;font-size:14px;">public class UserAction03 extends ActionSupport {

	private String username;
	private String password;
	private HttpServletRequest request;
	private HttpSession session;
	private ServletContext application;

	public String login() {
		// 获取对象
		request = ServletActionContext.getRequest();
		session = request.getSession();
		application = ServletActionContext.getServletContext();
		// 保存数据
		request.setAttribute("loginAddress", "您本次登录地点是:北京");
		session.setAttribute("username",username);
		application.setAttribute("online","当前在线人数:XXX");</span>



posted on 2015-12-24 17:47  菜鸟Z  阅读(157)  评论(0编辑  收藏  举报

导航