Accessing selected dropdown items using Java

﹥>﹥吖頭↗ 提交于 2019-12-25 07:06:40

问题


I have a dropdown which consist the language names. I am setting the value and displaying name of the dropdown by using a hashmap.

 <form action="TextTranslation" method="post" class="form" role="form" >
        <div class="row">
            <div  id = "imageView" class="col-lg-8 center-block ">

         <div class="btn-group"> 
         <select name="country">

            <% 

          Map<String,String> langCode = x.getCountryList();
           for( Object key :langCode.keySet() )
           {%>


           <option value="<%=(String)key%>"><%=langCode.get(key) %> </option>
              <% 

              System.out.println((String)key);
           }

           String name =  request.getParameter("country");
           request.setAttribute("code", name);

            %>



        </select>
    </div>
        <input type="submit" class= "btn btn-image" value="Translate">

                <a href="#search" class="btn btn-default bg-light-gray">Search Text</a>

            </div>
        </div>
          </form>

Values are passed correctly to dropbox as it print all the values in console. the set attribute is accessed in the particular servlet. But it gives a null value. Do you have any idea?Thank you in advance

UPDATED

<select name="country"> 
    <% 
       Map<String,String> langCode = x.getCountryList(); 
        for( Object key :langCode.keySet() ) 
          {%>
               <option value="<%=(String)key%>"><%=langCode.get(key) %> /option> 
               <% System.out.println((String)key); 
           } 
         String name = request.getParameter("country"); 
    %> 
</select> 

<input type="hidden" name="code" value = <%= name%>/> .

In the servlet I used,

 request.getParameter("code");

回答1:


update your jsp likewise,

<form...>
...
<input type="hidden" name="code" value = <%= name%>/> 
....
</form>

then get it from your servlet likewise,

 request.getParameter("code"); // will return value of code

NOTE :

Remove from your jsp-code if above solution you gonna implement then,

 request.setAttribute("code", name);


来源:https://stackoverflow.com/questions/36445715/accessing-selected-dropdown-items-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!