C
C
Chik-2015-06-25 14:11:55
Java
Chik-, 2015-06-25 14:11:55

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();
  }

but nothing has changed, still the same error about unreachability from another thread.
also tried to add the given Listner using the methods below, but also no result.
@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);
  }

How can I fix it or are there any alternative solutions for getting the address where the project is located?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Timur, 2015-06-25
@timych

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) {
  }
}

If the second, then as an option such a crutch - we make a request listener
@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);
    }
  }
}

And in the first and second cases, you will either have to create a static variable (you can directly in the listener) to record the url. Or a singleton class that will return the desired value.
But this is of course a crutch, because in order to get the URL in the second case, for example, you need to wait for at least one request. And the url will change depending on the resource.
What's stopping you from using HttpServletRequest in your native thread?
And why don't you know your URL? (You can register it in some config)
Do you have a REST service or servlets?

N
Nikolai Pavlov, 2015-06-26
@gurinderu

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/

C
Chik-, 2015-06-25
tweet @Timrus161

What's stopping you from using HttpServletRequest in your native thread?

we hang up the annotation for sending letters. it sends in another thread.
this is not very convenient when moving and during dev / release builds
rest
@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);
    }
  }
}

do you need to specify anything else?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question