How to read web.xml from WebApp

For WebApps, web.xml can be used to store application settings. How can I read this file. My servlets run on GlassFish v2 server.

+3
source share
4 answers

Not sure if I fully understand this question ...

Assuming your servlet is expanding HttpServlet?

HttpServletimplements ServletConfig, so you can find out the parameters of the servlet using:

In web.xml

<servlet>
    <servlet-class>com.acme.Foo</servlet-class>
    <init-param>
        <param-name>my.init.param</param-name>
        <param-value>10</param-value>
    </init-param>
</servlet>

To servlet:

int x = Integer.parseInt(getInitParameter("my.init.param"));

Similarly, you can get global (context-sensitive) parameters using:

<context-param>
    <param-name>my.context.param</param-name>
    <param-value>Hello World</param-value>
</context-param>

To servlet:

String s = getServletContext.getInitParameter("my.context.param");

, , Spring, Spring , -.

+9

init-param:

<init-param> 
    <param-name>InitParam</param-name> 
    <param-value>init param value</param-value> 
</init-param> 

java- ( ):

String initParam = getServletConfig().getInitParameter("InitParam");
+2

Doekman, , web.xml? WebContainer. , , :

context-param -. - context-param, . javax.servlet.ServletContext.getInitParameter() javax.servlet.ServletContext.getInitParameterNames().

, , Java IO. , , , Glassfish, . - System.getProperty( "user.dir" );

, . www.exampledepot.com.

0
source

The choice of container should not be relevant to this issue, since each container must implement a specification for the contents of a servlet , whether it be Tomcat, Glassfish, or one of many others .

0
source

Source: https://habr.com/ru/post/1702812/


All Articles