views:

144

answers:

2

Hello All,

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>
+3  A: 

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 you're 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.

anirvan
Ayup. The distinction between client-side and server-side code tripped me up when I was first learning JSPs, too; once you get it straight in your own mind, you'll save yourself a lot of bother chasing bad solutions down blind alleys. If you're hell-bent on avoiding Javascript, you'll need to have the form submit when the user selects an option from list 1 and recreate the page based on that selection; which is certainly doable but will present you with plenty of implementation headaches. For tweaking the display based on the user's choices, Javascript is the right tool for the job. Learn it.
BlairHippo
Thank you, I wil try my best
maas
A: 

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:

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

BalusC
Thank you, I will try my best
maas