13

Given: a Java EE 5 web app that has a web.xml that has a snippet like

<context-param>
    <description>c</description>
    <param-name>a</param-name>
    <param-value>b</param-value>
</context-param>

What would I need to do to move that context-param specification into an annotation based strategy.

vkraemer
  • 9,864
  • 2
  • 30
  • 44
  • I'm not sure if you can do it using annotations. The Servlet Context parameters are container-wide. Therefore I don't even know where you should put them (it doesn't make much sense to put them next to any particular Servlet). A `ServletContextInitializer` would seem to be the most adequate, but I don't think it's possible. – Piotr Nowicki Nov 01 '11 at 17:20

4 Answers4

9

You can find all javax.servlet annotations in the javax.servlet.annotation package summary:

  • @HandlesTypes This annotation is used to declare the class types that a ServletContainerInitializer can handle.
  • @HttpConstraint This annotation is used within the ServletSecurity annotation to represent the security constraints to be applied to all HTTP protocol methods for which a corresponding HttpMethodConstraint element does NOT occur within the ServletSecurity annotation.
  • @HttpMethodConstraint This annotation is used within the ServletSecurity annotation to represent security constraints on specific HTTP protocol messages.
  • @MultipartConfig Annotation that may be specified on a Servlet class, indicating that instances of the Servlet expect requests that conform to the multipart/form-data MIME type.
  • @ServletSecurity This annotation is used on a Servlet implementation class to specify security constraints to be enforced by a Servlet container on HTTP protocol messages.
  • @WebFilter Annotation used to declare a servlet Filter.
  • @WebInitParam This annotation is used on a Servlet or Filter implementation class to specify an initialization parameter.
  • @WebListener This annotation is used to declare a WebListener.
  • @WebServlet Annotation used to declare a servlet.

You see, there's nothing like a @WebContextParam. Which makes also less or more sense; on what kind of class would/could you set it?

Some Servlet based frameworks which rely on context parameters, such as JSF, also allows for setting some of them by JNDI. You might want to look into that instead. Or if it concerns homegrown code, then I'd look if @WebInitParam isn't a more feasible option for you.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'd think something like `@WebContextParam` would be useful on a servlet as a way of injecting a context parameter into multiple artifacts (contrasted with a `@WebInitParam` for a single servlet/filter). – Dave Newton Nov 02 '11 at 02:47
  • I am going to accept this answer. It is an answer to what I asked... but not what I really wanted to ask. I am going to open a new question to ask 'what I wanted to ask'... – vkraemer Dec 03 '11 at 03:41
5

If you are using Tomcat, you can use the Parameter tag in context.xml, and it will work identical as a context-param put in web.xml. So you can use @WebInitParam to catch a context variable.

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Context_Parameters

Abdul Rahman
  • 2,097
  • 4
  • 28
  • 36
  • Not entirely sure if this answers the question. I think the question might be about how to define the paramater in an annotation strategy, not necessarily access it. (but I could be wrong, so not down voting) – Lawrence Oct 19 '12 at 21:58
1

Well I think we just can't hardcode context-param using "Annotation" because there is a logical reason behind this : i am sure you guys knows annotations is always hardcoded in the Servlet and Servlet never loaded by the container in the memory for serving the client request until its first request is made by client (read Servlet Life Cycle ).

So what happen if we want to get the values out of "context-param" , which is hard-coded using Annotation in some other Servlet ? And the Servlet wrapped with context-param annotation is still not loaded in the memory thus we can't get the object for context :)

I think now you guys can easily guess why we can't use Annotation in case of context-param because we need to hardcode that Annotation with any specific servlet and we can't do that .......

Kr. Gaurav
  • 11
  • 2
1

One can specify the servlet content listener met data by using @WebServletContextListener. For instance,

 @WebServletContextListener 

public class TestServletContextListener implements javax.servlet.ServletContextListener { 
    public void contextInitialized(ServletContextEvent sce) { 

    } 

    public void contextDestroyed(ServletContextEvent sce) { 
        .... 
    } 
}
Preetham
  • 33
  • 6