⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 actioncontext.java

📁 一个简单的java邮件系统源码
💻 JAVA
字号:
package com.easyjf.web;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 
 * <p>
 * Title:Servlet上下文件处理类
 * </p>
 * <p>
 * Description: 通过使用ThreadLocal变量,实现用户当前访问的Servlet上下文环境访问!<br>
 * 通过使用ActionContext,在用户的Action类可以访问servlet相关资源.如要访问session对象,直接使用ActionContext.getContext().getSession()即可!
 * </p>
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * <p>
 * Company: www.easyjf.com
 * </p>
 * 
 * @author 蔡世友
 * @version 1.0
 */
public class ActionContext {
	public static final String HTTP_REQUEST = "request";

	public static final String HTTP_RESPONSE = "response";

	public static final String HTTP_SESSION = "session";

	public static final String HTTP_PARAMETERS = "parameters";

	static ThreadLocal actionContext = new ActionContextThreadLocal();

	Map context;

	public ActionContext(Map context) {
		this.context = context;
	}

	/**
	 * Sets the action context for the current thread.
	 * 
	 * @param context
	 *            the action context.
	 */
	public static void setContext(ActionContext context) {
		actionContext.set(context);
		
	}

	/**
	 * Returns the ActionContext specific to the current thread.
	 * 
	 * @return the ActionContext for the current thread.
	 */
	public static ActionContext getContext() {
		ActionContext context = (ActionContext) actionContext.get();
		if (context == null) {
			context = new ActionContext(new HashMap());
			setContext(context);
		}
		return context;
	}

	public void setContextMap(Map contextMap) {
		getContext().context = contextMap;
	}

	public Map getContextMap() {
		return context;
	}

	public HttpServletRequest getRequest() {
		HttpServletRequest ret = null;
		if (context.containsKey(HTTP_REQUEST))
			ret = (HttpServletRequest) context.get(HTTP_REQUEST);		
		return ret;
	}

	public HttpServletResponse getResponse() {
		HttpServletResponse ret = null;
		if (context.containsKey(HTTP_RESPONSE))
			ret = (HttpServletResponse) context.get(HTTP_RESPONSE);
		return ret;
	}

	public HttpSession getSession() {
		HttpSession ret = null;
		if (context.containsKey(HTTP_SESSION))
			ret = (HttpSession) context.get(HTTP_SESSION);
		else {
			HttpServletRequest request = getRequest();
			if (request != null)
				ret = request.getSession();
		}
		return ret;
	}

	public Map getParameters() {
		Map ret = null;
		if (context.containsKey(HTTP_PARAMETERS))
			ret = (Map) context.get(HTTP_PARAMETERS);
		else {
			HttpServletRequest request = getRequest();
			if (request != null)
				ret = request.getParameterMap();
		}
		return ret;
	}

	public Object get(Object key) {
		return context.get(key);
	}

	public void put(Object key, Object value) {
		context.put(key, value);
	}

	private static class ActionContextThreadLocal extends ThreadLocal {
		protected synchronized Object initialValue() {
			return new ActionContext(new HashMap());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -