问题
In web.xml I have two initial parameters name Premier and Regular. To show you the code this is it.
<servlet>
<servlet-name>StockServelt</servlet-name>
<servlet-class>test.StockServelt</servlet-class>
<init-param>
<param-name>Premier</param-name>
<param-value>/Premier</param-value>
</init-param>
<init-param>
<param-name>Regular</param-name>
<param-value>/Regular</param-value>
</init-param>
</servlet>
I have Create two constants (named PREMIER AND REGULAR) and I want to retrieve the configuration intial parameters name "Regular" and "Premier".
This is my code for that. I am not sure I am doing this part right or not
final String PREMIER = getServletContext().getInitParameter("Premier");
final String REGULAR = getServletContext().getInitParameter("Regular");
Here I print both to the console make sure that I retrieved correctly. But I am getting a null for both
System.out.println("This is the value for Premier: " + PREMIER + " "
+ "and this is the value for Regular: " + REGULAR);
Can someone please help me out her what I am doing wrong.
回答1:
If you are doing all this in a JSF request (which I don't understand if you are) you can get the init-parameters with
<context-param>
<param-name>Premier</param-name>
<param-value>/Premier</param-value>
</context-param>
(note: not inside the servlet-tag)
and
final String PREMIER = FacesContext.getCurrentInstance().getExternalContext().getInitParameter("Premier");
This is just a comment as I don't know if it solves anything and/or there are other problems.
来源:https://stackoverflow.com/questions/42036330/how-to-obtain-servletinit-param-inside-a-servlet-getservletcontext-getini