系统上下文信息PROVIDER

<!--系统上下文信息PROVIDER-->

<bean id="contextPvd" class="com.whatycms.common.struts2.ContextPvdImpl" autowire="byName"/>


package com.whatycms.common.struts2;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

public interface ContextPvd {
	/**
	 * 获得系统绝对路径 如:F:\webapps\CmsSys
	 * 
	 * @param path
	 *            可以传入空串
	 * @return
	 */
	public String getAppRealPath(String path);

	/**
	 * 获得应用绝对根路径
	 * 
	 * @return
	 */
	public String getAppRoot();

	/**
	 * 获得系统根路径 如:/CmsSys
	 * 
	 * @return
	 */
	public String getAppCxtPath();

	/**
	 * 获得应用端口号
	 * 
	 * @return
	 */
	public int getServerPort();

	/**
	 * 注销
	 * 
	 * @return
	 */
	public void logout();

	/**
	 * 获得response
	 * 
	 * @return
	 */
	public HttpServletResponse getResponse();

	/**
	 * 从Request的Attribute中获取值
	 * 
	 * @param key
	 * @return
	 */
	public Object getRequestAttr(String key);

	/**
	 * 给Request的Attribute中赋值
	 * 
	 * @param key
	 * @param value
	 */
	public void setRequestAttr(String key, Object value);

	/**
	 * 从SESSION中获得值
	 * 
	 * @param key
	 * @return
	 */
	public Object getSessionAttr(String key);

	/**
	 * 给session赋值
	 * 
	 * @param key
	 * @param value
	 */
	public void setSessionAttr(String key, Object value);

	/**
	 * 移除session中的属性
	 * 
	 * @param key
	 */
	public void removeAttribute(String key);

	public Object getApplicationAttr(String key);

	public void setApplicationAttr(String key, Object value);

	/**
	 * 获得sessionId
	 * 
	 * @param isCreate
	 *            如果session不存在是否创建
	 * @return
	 */
	public String getSessionId(boolean isCreate);

	/**
	 * 获得访问者IP
	 * 
	 * @return
	 */
	public String getRemoteIp();

	/**
	 * 获得访问者PORT
	 * 
	 * @return
	 */
	public int getRemotePort();

	/**
	 * 获得访问者URL
	 * 
	 * @return
	 */
	public String getRequestURL();

	/**
	 * 获得访问者浏览器
	 * 
	 * @return
	 */
	public String getRequestBrowser();

	/**
	 * 获得访问者操作系统
	 * 
	 * @return
	 */
	public String getRequestOs();

	/**
	 * 获得访问者的代理全部信息
	 * 
	 * @return
	 */
	public String getRequestUserAgent();

	/**
	 * 添加cookie
	 * 
	 * @param cookie
	 */
	public void addCookie(Cookie cookie);

	/**
	 * 获取cookie
	 * 
	 * @param name
	 * @return
	 */
	public Cookie getCookie(String name);

	/**
	 * 是否是post请求
	 * 
	 * @return
	 */
	public boolean isMethodPost();
}

package com.whatycms.common.struts2;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

public class ContextPvdImpl implements ContextPvd {
	public String getAppRealPath(String path) {
		return ServletActionContext.getServletContext().getRealPath(path);
	}

	public String getAppRoot() {
		return getAppRealPath("/");
	}

	public String getAppCxtPath() {
		return ServletActionContext.getRequest().getContextPath();
	}

	public int getServerPort() {
		return ServletActionContext.getRequest().getServerPort();
	}

	public void logout() {
		HttpSession session = ServletActionContext.getRequest().getSession(
				false);
		if (session != null) {
			session.invalidate();
		}
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public Object getRequestAttr(String key) {
		return ServletActionContext.getRequest().getAttribute(key);
	}

	public void setRequestAttr(String key, Object value) {
		ServletActionContext.getRequest().setAttribute(key, value);
	}

	public Object getSessionAttr(String key) {
		HttpSession session = ServletActionContext.getRequest().getSession(
				false);
		if (session == null) {
			return null;
		} else {
			return session.getAttribute(key);
		}
	}

	public void setSessionAttr(String key, Object value) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setAttribute(key, value);
	}

	public void removeAttribute(String key) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute(key);
	}

	public Object getApplicationAttr(String key) {
		return ServletActionContext.getServletContext().getAttribute(key);
	}

	public void setApplicationAttr(String key, Object value) {
		ServletActionContext.getServletContext().setAttribute(key, value);
	}

	public String getSessionId(boolean isCreate) {
		HttpSession session = ServletActionContext.getRequest().getSession(
				isCreate);
		if (session == null) {
			return null;
		} else {
			return session.getId();
		}
	}

	public String getRemoteIp() {
		return ServletActionContext.getRequest().getRemoteAddr();
	}

	public int getRemotePort() {
		return ServletActionContext.getRequest().getRemotePort();
	}

	public String getRequestURL() {
		return ServletActionContext.getRequest().getRequestURL().toString();
	}

	public String getRequestBrowser() {
		String userAgent = getRequestUserAgent();
		String[] agents = userAgent.split(";");
		if (agents.length > 1) {
			return agents[1].trim();
		} else {
			return null;
		}
	}

	public String getRequestOs() {
		String userAgent = getRequestUserAgent();
		String[] agents = userAgent.split(";");
		if (agents.length > 2) {
			return agents[2].trim();
		} else {
			return null;
		}
	}

	public String getRequestUserAgent() {
		HttpServletRequest req = ServletActionContext.getRequest();
		String userAgent = req.getHeader("user-agent");
		return userAgent;
	}

	public void addCookie(Cookie cookie) {
		ServletActionContext.getResponse().addCookie(cookie);
	}

	public Cookie getCookie(String name) {
		Cookie[] cookies = ServletActionContext.getRequest().getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				if (c.getName().equals(name)) {
					return c;
				}
			}
		}
		return null;
	}

	public boolean isMethodPost() {
		String method = ServletActionContext.getRequest().getMethod();
		if ("post".equalsIgnoreCase(method)) {
			return true;
		} else {
			return false;
		}
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值