Question

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?

Était-ce utile?

La solution

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".

Autres conseils

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.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top