1

Let's say you write a Deployment Descriptor with some context parameters and you want one of them to be an object,unfortunatelly I read that you cand only add string values to the tag.

Is there a way to do this?I understood an example when it came to a database because you can put the look-up name.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   ....>
 <context-param>
   <param-name>foo</param-name>
   <param-value>new foo()</param-value>
 <servlet>
  <servlet-name>BeerParamTests</servlet-name>
  <servlet-class>TestInitParams</servlet-class>
  <init-param>
   <param-name>adminEmail</param-name>
   <param-value> likewecare@wikewdfs</param-value>
  </init-param>
 </servlet>
<servlet-mapping>
 <servlet-name>BeerParamTests</servlet-name>
 <url-pattern>/Tester.do</url-pattern>
</servlet-mapping>

tudoricc
  • 709
  • 1
  • 12
  • 31

2 Answers2

1
<context-param>
   <param-name>foo</param-name>
   <param-value>new foo()</param-value>
</context-param>

This will just store new foo() as String.

param-name and param-value is based as key-value pair.

key to retrive the String stored as value no use of this kind of value as it will only return String

String foo=getServletContext().getInitParameter("foo");//result : new foo()

Let's say you write a Deployment Descriptor with some context parameters and you want one of them to be an object

No. You can't store Object to the Deployment descriptor.Only String parameters are allowed.

akash
  • 22,664
  • 11
  • 59
  • 87
1

<context-param> <param-name>foo</param-name> <param-value>com.edu.Foo</param-value> </context-param>

In you java code, try the below

Class.forName(getServletContext().getInitParameter("foo")).newInstance();

but you will get only the empty object without any values assigned to the properties. will it work for you?

whoami
  • 1,517
  • 1
  • 21
  • 29
  • Yes it does,though I can not call a certain method from that class you can still do something with that class – tudoricc Jul 14 '14 at 12:54