All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tmsps.ne4springboot.util.WebUtil Maven / Gradle / Ivy

The newest version!
package com.tmsps.ne4springboot.util;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * web层相关工具类
 * 
 * @author 冯晓东 [email protected]
 *
 */
public class WebUtil {

	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return request;
	}

	public static HttpServletResponse getReponse() {
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getResponse();
		return response;
	}

	public static HttpSession getSession() {
		HttpSession sn = WebUtil.getRequest().getSession();
		return sn;
	}

	public static ServletContext getServletContext() {
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		if (webApplicationContext == null) {
			return null;
		}
		ServletContext servletContext = webApplicationContext.getServletContext();
		return servletContext;
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy