简体   繁体   中英

JSP: drop down list 2 depends on drop down list 1

I am having difficulties while dealing with two linked drop down lists which the drop down list 1 will fetch the values from the DB and based on the user's selection, it will fetch the concerned records in drop down list 2.

I tried to do that in my jsp with that code, but it did not work and many people advised to use javascript. In fact, I don't know much more abot JS, so can you please help me

<select size="1" name="shop_category"><option value="NONE">  
<%  
             try  
             {  
                            ResultSet rs=null;  
                            Statement st1=null;  
                            String query = "select Category_name, category_id from shop_category_lkup";  
                            st1 = conn1.createStatement();  
                            rs = st1.executeQuery(query);  
                            while(rs.next())                  
            {  
       String sz_Selected="";  
             if (rs.getString("category_id").equals(shop_category))  
             {  
               sz_Selected = "selected";  
             }  
%>  
            <option value="<%=rs.getString("category_id")%>" <%=sz_Selected%>>  
    <%=rs.getString("category_name")%></option>  
<%  
                            }  
            }  
            catch (Exception e) {  
  e.printStackTrace();  
}  
%>  
</select>  

<select size="1" name="rent_category"><option value="NONE">  
<%  
             try  
             {  
                            ResultSet rs=null;  
                            Statement st1=null;  
                            String query = "select r.Category_name, r.category_id from rent_category_lkup r, shop_categpry_lkup s where r.category_id=s.category_id";  
                            st1 = conn1.createStatement();  
                            rs = st1.executeQuery(query);  
                            while(rs.next())                  
            {  
       String sz_Selected="";  
             if (rs.getString("category_id").equals(rent_category))  
             {  
               sz_Selected = "selected";  
             }  
%>  
            <option value="<%=rs.getString("category_id")%>" <%=sz_Selected%>>  
    <%=rs.getString("category_name")%></option>  
<%  
                            }  
            }  
            catch (Exception e) {  
  e.printStackTrace();  
}  
%>  
</select>

You're making a fundamental mistake of assuming that the Java code present in the scriptlets gets executed at the client end!

take a look at the lifecycle of a JSP . After that you'll be in a much better position to understand why your code doesn't work.
thereafter, you should try looking into some Cascading Dropdown examples using AJAX.

if all that doesn't help - post in again and it'll be much easier to guide you through.

I am not in a mood to post an extended answer since @anirvan already perfectly sums it up in two words which I cannot outbeat: you're making a fundamental mistake .

To the point: Java/JSP runs at the webserver, generates bunch of HTML/CSS/JS and sends it over network from websserver to webbrowser. The webbrowser (eg MSIE, Firefox, etc) retrieves and understands alone HTML/CSS/JS and starts to display/apply/run it. If Java/JSP has done its task right, you should not see any line of it when doing rightclick > View Source in webbrowser. The only way to let code in webbrowser (JavaScript) and code in webserver (Java/JSP) to communicate with each other is to let JavaScript send HTTP requests and Java/JSP respond on it.

Sending HTTP requests in JavaScript can be done in several ways:

  1. Submit a form: document.getElementById('formId').submit() .
  2. Change the window location: window.location = 'http://www.google.com'; .
  3. Fire an Ajaxical request: new XMLHttpRequest() and so on.

Here's a bunch of "must read" links to learn how the one and other fits in each other and how one and other should be used:

Hmm, this answer is after all a bit more extended than I meant it to be... Anyway, hope that it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM