6

I am checking new annotations for web servlets but what I do not understand how can you pass initial parameters (init-param) from easily modified location. Yes, I found annotation @WebInitParam, but that means you must write value into code, which i guess misses the point for easy change in web.xml.

So whats deal here? Do not use @WebServlet in such case?

2
  • Are you asking if changes made to web.xml after startup of the web app will be visible to the servlet?
    – John
    Commented Sep 28, 2013 at 14:34
  • John O: nope, read q again.
    – JIV
    Commented Sep 30, 2013 at 7:16

3 Answers 3

9

An interesting use case, and it turns out you can (my configuration: JBoss 7.1.1):

Servlet:

@WebServlet(name="fooServlet", urlPatterns="/foo")
public class FooServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String flip = getInitParameter("flip");
        resp.getWriter().println("<html>" +
            "<head><title>" + getServletName() + "</title></head>" +
            "<body>From " + getServletName() + ": " + flip + "</body>" +
            "</html>"
        );
    }
}

Inside web.xml (note the name is the same, and no <servlet-class> is specified):

<servlet>
    <servlet-name>fooServlet</servlet-name>
    <init-param>
        <param-name>flip</param-name>
        <param-value>flop</param-value>
    </init-param>
</servlet>

The value of flip = getInitParameter("flip") is set to flop, as specified in the web.xml!


By the way, I was wondering if this is a feature of JBoss, or standard. It is standard, see Servlet 3.0 specification, chapter 8.2.1 "Modularity of web.xml".

1
  • I'm confused - isn't @WebServlet supposed to replace the need for web.xml? I'm not sure how valid this use case actually is Commented May 20, 2019 at 7:03
0

The point of annotations is really to allow you to accept parameters more flexibly (even if your parameters will be constant values).

If you need constant values for your parameters, you could define these somewhere, then construct a URL that includes your values in a querystring. You can then use that URL to make an HTTP request to your service. For example, you could construct a URL that looks like this:

[hostname]/my-service/api?myParameter1=myValue1&myParameter2=myValue2

You can then use this URL to make a GET request to your service, which would look like this:

@WebServlet(
        name = "MyServletName",
        description = "MyDescription",
        urlPatterns = {"/my-service/api"},
        initParams={
            @WebInitParam(name="myParameter1", value="Not provided"),
            @WebInitParam(name="myParameter2", value="Not provided")
        }
    )
public class MyServlet extends HttpServlet {


    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException 
    {

      String parameter1 = request.getParameter("myParameter1");   
      String parameter2 = request.getParameter("myParameter1");

...

@WebInitParam is only being used to define default parameter values in case values for those parameters aren't supplied. So, if you have constants somewhere that you use to construct a URL that you then use to make an HTTP request, you can achieve what you're looking for.

-3

Yes you can.

In your constants file

public static String SOME_STRING= "stringVal";

And then import it in servlet

import static something.Constants.SOME_STRING;

@WebInitParam(name=SOME_STRING ,.....)

Now you change in only Constants for all your needs.

2
  • Can be SOME_STRING dynamically loaded? ie. not final?
    – JIV
    Commented Sep 26, 2013 at 11:25
  • I pasted mine.You can assign them by removing final modifier.What I done is I have a configuration xml,I used one servlet when I deploy It runs by specifying load on start up.Inside that there all fieds are getting populated. Commented Sep 26, 2013 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.