Click to See Complete Forum and Search --> : State Country Lookup in database


PJM
August 6th, 2004, 04:58 PM
Table: State
Col1: StateCode
Col2: StateName

Currently, a new class is being created, new( StateHM), where a hashmap is created after a sql call to the db with key being StateCode. A new class for every jsp page, action class, and servlet that uses StateCode(select entity HTML, etc.). This is bad.

How should a lookup table be accessed?
1. db called once
2. hashmap(s) need to be created once. Not sure exactly, since I may have the StateName and need to get StateCode and vica-versa.
3. static functions?
4. servletcontext vs class?
It will be accessed in both servlets and jsps. So I will need code to generate HTML select enities, as well as some method to use in html:select tags

Just curious, best practice.

cjard
August 7th, 2004, 07:43 AM
you need a two way association in the hashmap:


call your sql

for each row in the resultset you must
hm.put(statecode, statename)
hm.put(statename, statecode)
this is the only way you can get your "vice versa" functionality out

store the hashmap in tghe servlet context, retrieve and use it as appropriate



note: that the servlet context is often not well exposed any more. i think people were using it for the wrong reasons, and hence access to it seeems to be overly difficult. (i've clasically had some difficulty in obtaining it to place an object into.) check the documentation of your container