In a jsp page ,I am presenting the customer with drop down lists to select creditcardtype,expiration month,expiration year of credit card.
I am looking at ways the necessary strings for this can be put ,other than hardcoding them in html.
thanks
mark
<tr>
<td>
<select id="creditCardType" title="select card type" name="creditCardType">
<option value="M0">MasterCard</option>
<option value="D0">Discover</option>
<option value="J0">JCB</option>
<option value="I0">Diners Club</option>
<option value="A0">American Express</option>
<option value="V0">Visa</option>
<option value="V">Amazon.com Visa</option>
<option value="G21">Amazon.com Store Card</option>
</select>
</td>
</tr>
<tr>
<td>Expiration Date</td>
<td>
<select id="cardexpiryMonth" name="cardexpiryMonth">
<option value="01" selected="selected">01</option>
<option value="02" >02</option>
<option value="03" >03</option>
<option value="04" >04</option>
<option value="05" >05</option>
<option value="06" >06</option>
<option value="07" >07</option>
<option value="08" >08</option>
<option value="09" >09</option>
<option value="10" >10</option>
<option value="11" >11</option>
<option value="12" >12</option>
</select>
</td>
<td>
<select id="cardexpiryYear" name="cardexpiryYear">
<option value="2011" >2011</option>
<option value="2012" selected="selected">2012</option>
<option value="2013" >2013</option>
<option value="2014" >2014</option>
<option value="2015" >2015</option>
<option value="2016" >2016</option>
<option value="2017" >2017</option>
<option value="2018" >2018</option>
<option value="2019" >2019</option>
<option value="2020" >2020</option>
<option value="2021" >2021</option>
<option value="2022" >2022</option>
<option value="2023" >2023</option>
<option value="2024" >2024</option>
<option value="2025" >2025</option>
<option value="2026" >2026</option>
<option value="2027" >2027</option>
<option value="2028" >2028</option>
<option value="2029" >2029</option>
<option value="2030" >2030</option>
<option value="2031" >2031</option>
</select>
</td>
</tr>
If it are application wide constants, just put them in the application scope during application startup. The application scope is represented by the object being an attribute of
ServletContext. See also How do servlets work? Instantiation, sessions, shared variables and multithreadingCDI available?
If CDI happens to be available in your environment (i.e. you’re running a normal JEE server such as WildFly, Payara, TomEE, etc), then just use an
@ApplicationScopedbean instead of aServletContextListener.(note that I used
LinkedHashMapas it maintains insertion order in contrary toHashMap)This way it’s available as
${data.creditCardTypes}by EL in any JSP. You can then use JSTL<c:forEach>to iterate over it. It also supports iterating over aMapand each iteration will give aMap.Entryback which in turn hasgetKey()andgetValue()methods which are accessible in EL as well.No CDI available?
If CDI is not available (i.e. you’re not running a normal JEE server, such as Tomcat, Jetty, Undertow, etc, and you don’t want to install CDI for some reason), then you can use the
init()method of an arbitrary servlet or, better, aServletContextListener.This way it’s also available as
${data.creditCardTypes}by EL in any JSP.