If you are using spring you can do the following:
public static HttpServletRequest getCurrentHttpRequest(){
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof ServletRequestAttributes) {
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
return request;
}
logger.debug("Not called in the context of an HTTP request");
return null;
}
Related Contents:
- Autowiring in servlet
- Spring + Hibernate manually creating transactions, PROPAGATION_REQUIRED fails. BUG?
- Why does Spring MVC respond with a 404 and report “No mapping found for HTTP request with URI […] in DispatcherServlet”?
- Using JSF as view technology of Spring MVC
- What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?
- Spring @Autowire on Properties vs Constructor
- Spring Boot Remove Whitelabel Error Page
- Spring Boot – Loading Initial Data
- inject bean reference into a Quartz job in Spring?
- How to avoid the “Circular view path” exception with Spring MVC test
- Spring Boot War deployed to Tomcat
- Spring Boot – How to get the running port
- How to use 2 or more databases with spring?
- Spring 3 MVC resources and tag
- Disable all Database related auto configuration in Spring Boot
- How does Spring annotation @Autowired work?
- Using RestTemplate in Spring. Exception- Not enough variables available to expand
- How Spring Security Filter Chain works
- Spring XML file configuration hierarchy help/explanation
- Spring Scheduled Task running in clustered environment
- How to pass an additional parameter with spring security login page
- Spring Boot CORS filter – CORS preflight channel did not succeed
- Using Spring MVC Test to unit test multipart POST request
- How do I prevent people from doing XSS in Spring MVC?
- @Service are constructed twice
- How can I override Spring Boot application.properties programmatically?
- How to do conditional auto-wiring in Spring?
- How can we configure the internal Jackson mapper when using RestTemplate?
- Hibernate configuring multiple datasources and multiple session factories
- Environment Specific application.properties file in Spring Boot application [closed]
- How can I have list of all users logged in (via spring security) my web application
- Can SpringMVC be configured to process all requests, but exclude static content directories?
- Understanding Spring Cloud Eureka Server self preservation and renew threshold
- Where should “@Transactional” be placed Service Layer or DAO
- Encoded slash (%2F) with Spring RequestMapping path param gives HTTP 400
- How to use multiple login pages one for admin and the other one for user
- Are Spring objects thread safe?
- Spring Data JPA – Multiple EnableJpaRepositories
- Spring 5 WebClient using ssl
- Spring MVC @RestController and redirect
- Spring can you autowire inside an abstract class?
- The request sent by the client was syntactically incorrect.-Spring MVC + JDBC Template
- Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar?
- Neither BindingResult nor plain target object for bean name available as request attr [duplicate]
- Access to Session using Spring JPA and Hibernate in order to enable filters
- spring integration + cron + quartz in cluster?
- Spring Boot and how to configure connection details to MongoDB?
- Quartz: Cron expression that will never execute
- Injection of autowired dependencies failed;
- spring – hibernate 5 naming strategy configuration