Suppose I am having three dropdownlist controls named dd1
, dd2
and dd3
. The value of each dropdownlist comes from database. dd3
's value depends upon value of dd2
and dd2
's value depends on value of dd1
. Can anyone tell me how do I call servlet for this problem?
相关问题
- dropdown <select> for list.js
- There is no ViewData item with the key 'taskTy
- IE dropdown z-index bug
- #{facesContext} EL expression not resolved at runt
- jsp caching tag library
相关文章
- jsp里面的画布功能,为什么会出错?求大佬找下问题
- JSP String formatting Truncate
- java.lang.NoClassDefFoundError: javax/servlet/http
- Forward request from servlet to jsp
- Intercept @RequestHeader exception for missing hea
- Django: Replacement for the default ManyToMany Wid
- Comparing string and boolean in Expression languag
- ASP.Net MVC 3: optgroup support in Html.DropDownLi
Judging by your question, you're really not using a web framework but using servlets to render html.
I'll be nice and say that you're about a decade behind the times :), people use JSPs (and a web framework like struts) for this sort of thing. However, having said that, here goes:
I'll say it again, just use a web-framework, or atleast plain old jsp to do this.
There are basically three ways to achieve this:
Submit form to a servlet during the onchange event of the 1st dropdown (you can use Javascript for this), let the servlet get the selected item of the 1st dropdown as request parameter, let it obtain the associated values of the 2nd dropdown from the database as a
Map<String, String>
, let it store them in the request scope. Finally let JSP/JSTL display the values in the 2nd dropdown. You can use JSTL (just drop jstl-1.2.jar in/WEB-INF/lib
)c:forEach
tag for this. You can prepopulate the 1st list in thedoGet()
method of theServlet
associated with the JSP page.Once caveat is however that this will submit the entire form and cause a "flash of content" which may be bad for User Experience. You'll also need to retain the other fields in the same form based on the request parameters. You'll also need to determine in the servlet whether the request is to update a dropdown (child dropdown value is null) or to submit the actual form.
Print all possible values of the 2nd and 3rd dropdown out as a Javascript object and make use of a Javascript function to fill the 2nd dropdown based on the selected item of the 1st dropdown during the onchange event of the 1st dropdown. No form submit and no server cycle is needed here.
One caveat is however that this may become unnecessarily lengthy and expensive when you have a lot of items. Imagine that you have 3 steps of each 100 possible items, that would mean 100 * 100 * 100 = 1,000,000 items in JS objects. The HTML page would grow over 1MB in length.
Make use of XMLHttpRequest in Javascript to fire an asynchronous request to a servlet during the onchange event of the 1st dropdown, let the servlet get the selected item of the 1st dropdown as request parameter, let it obtain the associated values of the 2nd dropdown from the database, return it back as XML or JSON string. Finally let Javascript display the values in the 2nd dropdown through the HTML DOM tree (the Ajax way, as suggested before). The best way for this would be using jQuery.
..where the
Servlet
behind/json/options
can look like this:Here,
Gson
is Google Gson which eases converting fullworthy Java objects to JSON and vice versa. See also How to use Servlets and Ajax?You may need multiple servlets for this.
Servlet 1: Load the values for the first drop down list from the database. On the JSP page construct the drop down list. On the user selecting a value submit to servlet two.
Servlet 2: retrieve the value from the first list and perform your database search for the values of the second list. Construct the second list. When the user selects the second value submit it to servlet 3.
Servlet 3: retrieve the value selected in the second drop down and perform the database search to get values for the last drop down.
You may want to consider AJAX to make the populating of the lists appear seamless to the users. jQuery has some very nice plugins for making this quite easy if you are willing to do that.
You can write JavaScript that submits the form in the onchange event. Again, If you use an existing library like jQuery it will be 10 times simpler.
That was an awesome simple solution. I like how small the JQuery code is and really appreciate the link to the GSON API. All the examples made this an easy implementation.
Had one issue on building the JSON server URL with the reference to the parent SELECT ( e.g.
$(this).val()
) [needed to specify the:selected
attribute]. I've modified the script a little to include the suggested updates. Thanks for the initial code.