开发者

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%>&开发者_Python百科gt;  
    <%=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 (e.g. 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:

  • Beginning and intermediate JSP/Servlet tutorials
  • Java/JSP and JavaScript, how to communicate with each other?
  • DAO tutorial, getting data from DB using basic JDBC the right way
  • How to avoid Java code in JSP files?
  • Populating dependent dropdown lists with JSP/Servlet

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜