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

前端 未结 2 1422
予麋鹿
予麋鹿 2020-12-21 15:57

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 wi

相关标签:
2条回答
  • 2020-12-21 16:30

    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 讨论(0)
  • 2020-12-21 16:34

    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.

    0 讨论(0)
提交回复
热议问题