requestcontext.java

来自「AJAX的一个好东西哦,国人的一个AJAX的实现不错的东西」· Java 代码 · 共 136 行

JAVA
136
字号
/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * $Id$
 */
package net.buffalo.request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

/**
 * Request Context Object
 * 
 * @author michael
 * @version 1.2
 */
public class RequestContext {

	private HttpSession session;

	private HttpServletRequest request;

	private HttpServletResponse response;
	private ServletContext context;
	
	private Map cookieMap;

	public RequestContext(ServletContext context, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		this.request = request;
		this.response = response;
		this.context = context;
	}

	public void addCookie(String name, String value) {
		addCookie(new Cookie(name, value));
	}

	public void addCookie(Cookie cookie) {
		response.addCookie(cookie);

		if (cookieMap == null)
			readCookieMap();

		cookieMap.put(cookie.getName(), cookie);
	}

	public Cookie getCookie(String name) {
		if (cookieMap == null)
			readCookieMap();

		return (Cookie) cookieMap.get(name);
	}

	private void readCookieMap() {
		cookieMap = new HashMap();

		Cookie[] cookies = request.getCookies();

		if (cookies != null)
			for (int i = 0; i < cookies.length; i++)
				cookieMap.put(cookies[i].getName(), cookies[i]);
	}

	public String getCookieValue(String name) {
		Cookie cookie;

		cookie = getCookie(name);

		if (cookie == null)
			return null;

		return cookie.getValue();
	}

	public String getParameter(String name) {
		return request.getParameter(name);
	}

	public Object getAttribute(String name) {
		return request.getAttribute(name);
	}

	public String[] getParameters(String name) {

		return request.getParameterValues(name);
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public HttpSession getSession() {
		if (session == null)
			session = request.getSession(false);

		return session;
	}
	
	public ServletContext getContext() {
		return context;
	}

	public HttpSession createSession() {
		if (session == null) {

			session = request.getSession(true);
		}

		return session;
	}
}

⌨️ 快捷键说明

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