0

Below is my web.xml

<servlet>
    <servlet-name>DispatcherName</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Is there any way that I can get the servlet-name "DispatcherName" in my application controller?

I want this to access the controller objects from the XMLWebApplicationContext & to do that I need the RequestDispatcher Name. Till now this is what I've tried:

webApplicationContext=WebApplicationContextUtils.getWebApplicationContext(GetServletContextWebListner.getServletContext());     
XmlWebApplicationContext xmlWebApplicationContext = (XmlWebApplicationContext)GetServletContextWebListner.getServletContext().getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT."+webApplicationContext.getApplicationName().replace("/", ""));

and tried this too

@WebListener
public class GetServletContextWebListner implements ServletContextListener {
private static ServletContext servletContext;

public static ServletContext getServletContext() {
    return servletContext;
}

@Override
public void contextInitialized(ServletContextEvent sce) {
    servletContext = sce.getServletContext();
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
    servletContext = null;
}    
}

and

   (XmlWebApplicationContext)GetServletContextWebListner.getServletContext().getServletContextName()

Since I'm not able to get the servlet name, I'm using the getApplicationName() but this may vary with the servlet name.

  • What did you try so far? – Kurt Van den Branden Dec 02 '16 at 10:49
  • @KurtVandenBranden added in question – Yadvendra Rathore Dec 02 '16 at 10:58
  • Don't... You should be using dependency injection in the first place, second the `WebApplicationContextUtils` will only give you the root context and not the one from the `DispatcherServlet`. – M. Deinum Dec 02 '16 at 11:02
  • @M.Deinum Could you please help how to get the XMLWebApplicationContext object? or how to get the Dispatcher Servlet name – Yadvendra Rathore Dec 02 '16 at 11:10
  • Why do you need the `ApplicationContext`? Generally when doing things like that you are doing things in the wrong way. – M. Deinum Dec 02 '16 at 13:14
  • @M.Deinum Yes I do understand that, but there is a requirement to update some value in all the beans. I have tried ApplicationContext & WebApplicationContext but none of those provided the object of controllers(only service and DAO were available). But on trying XmlWebApplicationContext the controllers object was found but my approach is not good, I'm looking for a better approach – Yadvendra Rathore Dec 02 '16 at 14:15
  • Update based on what? Generally updating values on a singleton in a running application is a bad idea. – M. Deinum Dec 02 '16 at 14:25
  • @M.Deinum Actually there are properties set in context, Our client is using Properties but not from property files but database, uses @ Value annotation. They have requirement to update properties at runtime but without restarting the server & since during runtime property values in context doesn't get updated I have to update each object by retrieving it from context. – Yadvendra Rathore Dec 02 '16 at 16:05
  • @M.Deinum also if there's any way to update the context properties directly then it would be the best option. – Yadvendra Rathore Dec 02 '16 at 16:06

1 Answers1

1

in you controller, you may try :

request.getServletContext().getServletContextName()

Or

RequestContextUtils.getWebApplicationContext(request).getDisplayName()
Mohamed Nabli
  • 1,629
  • 3
  • 17
  • 24