Answer the question
In order to leave comments, you need to log in
How to make HttpServletRequest available from another thread?
You need access to the current address where the web application is located.
HttpServletRequest is used for this, but due to the fact that access from another thread is required, difficulties arise.
Found information that you need to add a RequestContextListener
Added a bean to the application configs
@Bean
@ConditionalOnMissingBean(RequestContextListener.class)
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
@Override
protected void registerContextLoaderListener(ServletContext servletContext) {
super.registerContextLoaderListener(servletContext);
servletContext.addListener(new RequestContextListener());//To change body of generated methods, choose Tools | Templates.
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(new RequestContextListener());
super.onStartup(servletContext);
}
Answer the question
In order to leave comments, you need to log in
Not really understood - do you need the context path of your application or the full URL?
If the first, then you can, for example, make such a listener:
@WebListener
public class MyApplicationLifeCicleListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
// cохраняем куда-нибудь путь
System.out.println(event.getServletContext().getContextPath());
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
}
@WebListener
public class MyRequestListener extends RequestContextListener {
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
super.requestInitialized(requestEvent);
if (requestEvent.getServletRequest() instanceof HttpServletRequest) {
// cохраняем путь
String url = ((HttpServletRequest) requestEvent.getServletRequest()).getRequestURL().toString();
System.out.println(url);
}
}
}
I read here and realized that you are just doing some kind of garbage.
Want to do an asynchronous send?
Make a service and mark the method with the async annotation. Spring will destroy everything.
There are a million examples https://spring.io/guides/gs/async-method/
What's stopping you from using HttpServletRequest in your native thread?
@WebListener
public class MyRequestListener extends RequestContextListener {
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
super.requestInitialized(requestEvent);
if (requestEvent.getServletRequest() instanceof HttpServletRequest) {
// cохраняем путь
String url = ((HttpServletRequest) requestEvent.getServletRequest()).getRequestURL().toString();
System.out.println(url);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question