• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dropdown from database

 
swapna jhanjhar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though this is a simple requirement, but i dont know why this is not working. I have a jsp page where i need to show the values from database. I'm using servlet which will be referred to on page submit. On page load of the jsp page i'm calling a javascript function which is submitting the form. In servlet connection is created with database and the values are taken in a resultset.


This structure is working fine in other requirements. Please let me know why this code is not working. I'm getting error in the "while"statement inside the dropdown.
 
swapna jhanjhar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to populate the values in dropdown.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swapna jhanjhar wrote:...I'm getting error in the "while"statement inside the dropdown.

What error you get,any exception details? And you should not write scriptlets inside JSPs these days. Looking at your code, you submit the JSP in the "onload" event of the body, which would submit the form recursively.
 
swapna jhanjhar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha can you show me some other method of getting the same result?
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4324
236
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to submit the form at the form load event? Submitting a form is typically done by the user by pressing a submit button, after the form is filled. If it is submitted at the form-load event, the user may probably not even see the form. On the other hand, for the best practice, these scriptles should be eliminated from your JSP. Use EL, JSTL, and standard tags instead of using Java codes in JSPs. For eliminating the while loop, pass an array or list of colors instead of passing a ResultSet object. Then you can use forEach tag of JSTL.

Devaka
 
swapna jhanjhar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to populate the dropdown when the page loads from DB.
Is there any other way to populate during page load?
 
swapna jhanjhar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swapna jhanjhar wrote:I need to populate the dropdown when the page loads from DB.
Is there any other way to populate during page load?
using servlets and with the same structure.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4324
236
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swapna jhanjhar wrote:Is there any other way to populate during page load?

You don't need to do it through JavaScripts as JavaScripts are running on the client side. The 'dynamic content' of a JSP is generated at the creation of that page at server side. Have a look at the JSP Tutorial, if you are new to JSP.

Devaka
 
swapna jhanjhar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka Cooray wrote:
swapna jhanjhar wrote:Is there any other way to populate during page load?

You don't need to do it through JavaScripts as JavaScripts are running on the client side. The 'dynamic content' of a JSP is generated at the creation of that page at server side. Have a look at the JSP Tutorial, if you are new to JSP.

Devaka

Actually I want to create 3 dropdowns which will be populated on page load and i dont want to write the code in jsp, and want to use servlet.
can anyone please suggest the best way to do so?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64716
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the proper approach:
  • Not only should there be no database access code in the JSPs, there shouldn't be any anywhere in the presentation layer. All DB acccess shold be abstracted away by the model layer.
  • DB constructs like results sets get closed as soon as possible and are never passed outside the model layer.
  • JSPs should never be directly accessed. All but the most trival of JSPs should have a page controller that does all the work needed to gather the data needed to display the page.
  • The page controller places the data in scoped variables on the request and forwards to the JSP.
  • The JSP uses JSTL and EL to create its display. No Java code. No scriptlets. And certainly no DB constructs.

  • You might find these two articles helpful:
  • The Secret Life of JSPs
  • The Front Man
  •  
    Michael Angstadt
    Ranch Hand
    Posts: 277
    Eclipse IDE Java PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Like others have said, there should be no database code in your JSP (there shouldn't be scriptlets either). It shouldn't matter to the JSP where the data comes from.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64716
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rather than an arrays (using unreadable indexes [0] and [1]), I'd use a Map.
     
    Michael Angstadt
    Ranch Hand
    Posts: 277
    Eclipse IDE Java PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:Rather than an arrays (using unreadable indexes [0] and [1]), I'd use a Map.


    Yes, I think that using a Map<String, String> instead of a List<String[]> would definitely make the code much more readable and maintainable. But doesn't a Map add unnecessary overhead in this particular situation? For example, when you add an entry to a HashMap, it has to do things like create a hash of the key, put the value in the proper bucket, etc. In the situation above, we don't need any of that. All we need is a way to store is a list of String pairs that we can iterate through. We never have to retrieve individual values by calling Map.get(), making all the hashing that HashMap does a waste.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64716
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Michael Angstadt wrote:[For example, when you add an entry to a HashMap, it has to do things like create a hash of the key, put the value in the proper bucket, etc.
    Do you really think any of that is more than noise? Really? Do you think that the infinitesimal time that might (might) be saved is worth creating crappy unreadable code?
     
    Michael Angstadt
    Ranch Hand
    Posts: 277
    Eclipse IDE Java PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:
    Michael Angstadt wrote:[For example, when you add an entry to a HashMap, it has to do things like create a hash of the key, put the value in the proper bucket, etc.
    Do you really think any of that is more than noise? Really? Do you think that the infinitesimal time that might (might) be saved is worth creating crappy unreadable code?

    No, it's just annoying is all.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic