?? actioncontext.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 + -