Populating Drop-Down menu in JSP from XML

In the web.xml file I have made this entry under <web-app>:

<context-param>

<param-name>Option1</param-name>

<param-value>India</param-value>

</context-param>

<context-param>

<param-name>Option2</param-name>

<param-value>Global</param-value>

</context-param>

In the JSP I have written this to retreive the values in the dropdown box:

<%

String myValue1= application.getInitParameter("Option1");

String myValue2= application.getInitParameter("Option2");

%>

<option value="aaa" selected> <%=myValue1%> </option>

<option value="bbb" > <%=myValue2%> </option>

The drop-down is getting populated with "null".

Instead of application I have tried with config object also as well as ServletContext, but in all the cases "null" is getting retreived.

What I'm missing?

[934 byte] By [DhurjatiCa] at [2007-11-27 11:39:15]
# 1

I am not sure from which context you are getting the value

But you need to get the value from servlet context

like

private ServletContext servletContext;

servletContext.getInitParameter('"option1")

~Aman

aman_mahajana at 2007-7-29 17:25:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I have tried using servletContext.getInitParameter('"option1") It is fetching null

DhurjatiCa at 2007-7-29 17:25:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

this code is working for me and get output

web.xml

**********

<context-param>

<param-name>datasourcename</param-name>

<param-value>fred</param-value>

</context-param>

callingfrom.jsp

****************

<%

String ds = (String)application.getInitParameter("datasourcename");

out.println (ds);

%>

output is : fred

drvijayy2k2a at 2007-7-29 17:25:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

even i am getting the value in my program and there is no separate setting i have done

aman_mahajana at 2007-7-29 17:25:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

But Im not getting the values in the drop-down. So there might be something missing in the

<option value="aaa" selected> <%=myValue1%> </option>

<option value="bbb" > <%=myValue2%> </option>

tag.

Is anything needs to be done here?Or Im doing it correctly?

DhurjatiCa at 2007-7-29 17:25:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Well if you are getting null values for the above code it is for sure tht you dont any value associated with that context-Init-parameter.

a better practise would be that check all the listed parameters first if they exist then go about else where there could some issues with the way you are configuring your web.xml

<%

for (Enumeration e = application.getInitParameterNames(); e.hasMoreElements();) {

out.println(e.nextElement());

}

%>

check whether the listed parameter which you specified exists in the printed or not.

RahulSharnaa at 2007-7-29 17:25:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

I guess, the problem lies somewhere else.

There is a.jsp.In a.jsp we have

<%@ include file="./b.jspf" %>

In b.jspf we have:

<%@ include file="./c.jspf" %>

Now the drop down is in c.jspf. So will it be able to read from web.xml the way I have mentioned?

DhurjatiCa at 2007-7-29 17:25:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...