@WebServlet with init parameters from xml
Asked Answered
M

3

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?

Mcmurry answered 26/9, 2013 at 8:35 Comment(2)
Are you asking if changes made to web.xml after startup of the web app will be visible to the servlet?Microelement
John O: nope, read q again.Mcmurry
P
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".

Papacy answered 26/9, 2013 at 9:6 Comment(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 isEnvious
E
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.

Envious answered 23/5, 2019 at 2:9 Comment(0)
J
-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.

Jesse answered 26/9, 2013 at 8:42 Comment(2)
Can be SOME_STRING dynamically loaded? ie. not final?Mcmurry
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.Jesse

© 2022 - 2024 — McMap. All rights reserved.