This discussion is archived
12 Replies Latest reply: Feb 8, 2013 5:50 AM by Murray9654 RSS

how to display a dynamic combobox?

Murray9654 Newbie
Currently Being Moderated
we had a requirement to display a combobox which is filled with some values on the webpage when somebody checks the checkbox which is present on the webpage.

The approach we have taken is ajax. how we did this is, when somebody checks the checkbox on the webpage we are initiating the ajax call requesting a servlet. this servlet pulls the data from the database, places the data in the request and forwards the request to a jsp. In that jsp i am writing simple html combobox and populating the values in the combobox from the request using JSTL and this jsp will be sent as response to ajax call. The ajax gets the html as responce which contains a combobox and i am just placing that html ( the complete response) in a div which is next to checkbox.

In this approach there is lot of pain. if i have to display 10 html components dynamically based on some condition then i have to have 10 jsp's one for each component. I always feel this is a wrong way of doing things. what is the best approach to deal with the dynamic stuff on webpages.
  • 1. Re: how to display a dynamic combobox?
    Kayaman Guru
    Currently Being Moderated
    Muralidhar wrote:
    In this approach there is lot of pain.
    Hah, there sure is.
    if i have to display 10 html components dynamically based on some condition then i have to have 10 jsp's one for each component. I always feel this is a wrong way of doing things. what is the best approach to deal with the dynamic stuff on webpages.
    A simple way would be to show use CSS to hide the unneeded elements and show them with Javascript as necessary. Then you'd have 1 page and you wouldn't even need to do Ajax.
  • 2. Re: how to display a dynamic combobox?
    Murray9654 Newbie
    Currently Being Moderated
    if i have to display 10 html components dynamically based on some condition then i have to have 10 jsp's one for each component. I always feel this is a wrong way of doing things. what is the best approach to deal with the dynamic stuff on webpages.
    A simple way would be to show use CSS to hide the unneeded elements and show them with Javascript as necessary. Then you'd have 1 page and you wouldn't even need to do Ajax.
    There is a problem in this. I have to fill the combobox with some values coming from the database. In some other cases i have to check that it meets some conditions on the server that mean in the business logic. If it meets the conditions then only i have to display some html form components to the user. so in both these cases what you suggested will not work. here there might be experts who have intensively worked on web app's so kindly suggest the best solution.
  • 3. Re: how to display a dynamic combobox?
    Kayaman Guru
    Currently Being Moderated
    Muralidhar wrote:
    here there might be experts who have intensively worked on web app's so kindly suggest the best solution.
    Are you implying I haven't "intensively worked on web app's"?

    I was suggesting an easy solution for you, because you didn't give enough information. And it's still a bit unclear what you're trying to do (besides outsourcing).

    If you want the first combobox choice to affect the values in the second combobox (and so on), then you do need the ajax call.
    I don't understand however why you would need a separate JSP page for each combobox. In fact, why would JSP be involved in the ajax calls in any way? That's not right.
  • 4. Re: how to display a dynamic combobox?
    r035198x Pro
    Currently Being Moderated
    Your problem here is mixing data with presentation. Your ajax calls must not return HTML. They must return data only as either JSON or XML (JSON is almost always better).
    Conditionally displaying the layout is then easily done using javascript because javascript can parse either JSON or XML.
  • 5. Re: how to display a dynamic combobox?
    Murray9654 Newbie
    Currently Being Moderated
    Kayaman wrote:
    Muralidhar wrote:
    here there might be experts who have intensively worked on web app's so kindly suggest the best solution.
    Are you implying I haven't "intensively worked on web app's"?
    I am very sorry. I don't mean that.
    If you want the first combobox choice to affect the values in the second combobox (and so on), then you do need the ajax call.
    I don't understand however why you would need a separate JSP page for each combobox. In fact, why would JSP be involved in the ajax calls in any way? That's not right.
    ok it should be json or xml that i should return and parse them using javascript.

    ok i know i have done something terrible but i will tell u why i used jsp. I have to present a combobox popped up with some values so i used to fetch values from the database, store them in the list, store the list in the request, forward request to jsp and in jsp write the select tag and below that just iterate the list stored in the request using jstl , i mean take the values from the list and put them as value in option tag and loop option tag until i finish the list. so this is how i did and when i get the response on the client as html, i used to insert that complete html in a div.

    Please don't laugh.
  • 6. Re: how to display a dynamic combobox?
    Kayaman Guru
    Currently Being Moderated
    Muralidhar wrote:
    ok it should be json or xml that i should return and parse them using javascript.
    That would be the smart thing to do yes.
    ok i know i have done something terrible but i will tell u why i used jsp. I have to present a combobox popped up with some values so i used to fetch values from the database, store them in the list, store the list in the request, forward request to jsp and in jsp write the select tag and below that just iterate the list stored in the request using jstl , i mean take the values from the list and put them as value in option tag and loop option tag until i finish the list. so this is how i did and when i get the response on the client as html, i used to insert that complete html in a div.
    Well that's not too far off, it's just not smart to get HTML directly back. What I don't understand is why you need 10s of JSPs for that? You can easily do this with a servlet/jsp combination and handle all the ajax calls.
    Please don't laugh.
    We're not here to laugh.
  • 7. Re: how to display a dynamic combobox?
    Murray9654 Newbie
    Currently Being Moderated
    ok it should be json or xml that i should return and parse them using javascript.
    That would be the smart thing to do yes.
    Ok. one last question would be if not smart, is there any other ways to do. I just want to know any other possible ways. Just to transfer the data i see only json or xml.
    >
    Well that's not too far off, it's just not smart to get HTML directly back. What I don't understand is why you need 10s of JSPs for that? You can easily do this with a servlet/jsp combination and handle all the ajax calls.
    If json or xml why do i need jsp? if xml, i will build the xml using xml api's and will set the response type to xml and will insert the xml in response body using output stream. if it is json then i will set the response content type to "text" and will set the json as response body. In both the cases i don't need a jsp.
  • 8. Re: how to display a dynamic combobox?
    r035198x Pro
    Currently Being Moderated
    Someone has to present the data to the user at HTML, that's where JSP comes in.

    The other option is to stop reinventing the wheel and use modern frameworks that already have AJAX capabilities like JSF 2
  • 9. Re: how to display a dynamic combobox?
    Murray9654 Newbie
    Currently Being Moderated
    Someone has to present the data to the user at HTML, that's where JSP comes in.
    No it is not true. I think you mean to say the response from the server should be as html. in my condition i don't need to send html. i simply need to send data where in data will be parsed on client to build a dynamic html component. so i don't need jsp. If i have to send the presentation logic then only my response need to be html.
    The other option is to stop reinventing the wheel and use modern frameworks that already have AJAX capabilities like JSF 2
    trying to understand the core is not a bad idea.
  • 10. Re: how to display a dynamic combobox?
    r035198x Pro
    Currently Being Moderated
    Muralidhar wrote:
    Someone has to present the data to the user at HTML, that's where JSP comes in.
    No it is not true. I think you mean to say the response from the server should be as html. in my condition i don't need to send html. i simply need to send data where in data will be parsed on client to build a dynamic html component. so i don't need jsp. If i have to send the presentation logic then only my response need to be html.
    You are now just going around in circles.
    The advice we have repeatedly given you is that send data only from the AJAX call, no one has asked you to send jsp from the ajax call.
    If you are the one building the html from the data as well then use JSP to display that HTML, so you do need JSP if you are the one consuming the data. If all you are providing are data services for other people to build HTML with then you don't need servlets and JSPs at all and you have posted in the forum.
  • 11. Re: how to display a dynamic combobox?
    Murray9654 Newbie
    Currently Being Moderated
    You are now just going around in circles.
    The advice we have repeatedly given you is that send data only from the AJAX call, no one has asked you to send jsp from the ajax call.
    If you are the one building the html from the data as well then use JSP to display that HTML, so you do need JSP if you are the one consuming the data. If all you are providing are data services for other people to build HTML with then you don't need servlets and JSPs at all and you have posted in the forum.
    I understand what you are saying. thank u for that. i am not providing data services. I have told you that i have to display a dynamic combox on the currently viewing page so i am saying that my server responds with json or xml and on the client side i will use the values to display a dynamic combobox so i said in this case i don't need jsp because i am pulling values only for filling a combobox.

    here as i am not building the html from the data, jsp is not required. If i have to build the view with the data for sure i will use a jsp.
  • 12. Re: how to display a dynamic combobox?
    Murray9654 Newbie
    Currently Being Moderated
    Ok after thinking for a while i understood what you are saying, i mean about servlet and jsp combination. ya we can easily do it with them. now i also don't remember why i have written multiple jsp's. Anyway thank you very much.

    The last question left was about other possible ways. any reply for this will help me a lot.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points