Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP page getting values from DB

 
Venny Ra
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I need help regarding a jsp page that m designing. The jsp page creates an appoitnment for the patient. Here is the flow:


patientProfile.jsp --------------> createAppointment.jsp ----------------> appointmentServlet --------------------->Store the appointment info in DB


now when the control goes to createAppointment.jsp, there is a drop down box inside the form which must be automatically filled with doctors list from DB. Once the form is filled, servlet is called which then talks to DAO for storing value in DB.

But how do I populate those docotr list in the drop down box ?


Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not by putting Java code in the JSP. That's a bad practice that's been discredited for over 10 years.

Two approaches that I'd consider:
  • Give the JSP a page controller (all my JSPs have page controllers) that fetches the values from the DB by delegating to model classes that perform the actual DB access. If you don't know what a page controller is, please read through this article.
  • Write a custom tag that will delegate to the model classes to fetch the info and produce the list of options.
  •  
    Venny Ra
    Greenhorn
    Posts: 15
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:Not by putting Java code in the JSP. That's a bad practice that's been discredited for over 10 years.

    Two approaches that I'd consider:
  • Give the JSP a page controller (all my JSPs have page controllers) that fetches the values from the DB by delegating to model classes that perform the actual DB access. If you don't know what a page controller is, please read through this article.
  • Write a custom tag that will delegate to the model classes to fetch the info and produce the list of options.


  • Hi,

    First of all thanks for the reply.. I am using a page controller i.e a servlet which fetches data from DB and put it into the request request. But on the jsp page I have to use a lil java code to retrive the values out of arrayList that was stored in request object.

    Here is my code for jsp:



    <%
    User user1 = (User) request.getSession().getAttribute("visitUser");

    %>

    <select name="DoctorsList" style="width: 390px; height: 20px;" class="auto-style3">

    <% for (int i=0; i < user1.getUserList().size() ; i ++ ) {%>

    <option> <%= user1.getUserList().get(i).getFullName() %></option>
    <%}%>

    </select></td>

    now the problem is. evrytime I refresh the page, it populates the arraylist with same values again. and show the same names in drop down. How do I control that ???

     
    Paul Clapham
    Sheriff
    Pie
    Posts: 20758
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would expect to see the same contents every time I refresh a page. If by "refresh" you mean "clicking the browser's Refresh button" or "pressing the Refresh command key", that is. Why do you expect to see something different?
     
    Venny Ra
    Greenhorn
    Posts: 15
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:I would expect to see the same contents every time I refresh a page. If by "refresh" you mean "clicking the browser's Refresh button" or "pressing the Refresh command key", that is. Why do you expect to see something different?



    oops sorry. I wasnot clear, .. problem is when i click refresh,, it would show me same names twice. So If i refreshed 2 times, the page gets loaded with 3 same names in drop down. How do I control that ?


    Evrytime i click refresh, it goes to controller to fetch the same values again. and put it in arrayList.

    DO I make sense ?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64629
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You should be using the JSTL and EL on the JSP page, not Java code!

    As to why you're getting multiples, we can't say where your logic error is without more info.
     
    Stefan Evans
    Bartender
    Pie
    Posts: 1670
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That loop is easily translated into JSTL/EL



    With regards to names coming up each time, I would take a look at your User.getUserList method.
    I am guessing every time it runs it just adds the list onto the already existing one.

     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64629
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wanna bet it's a misuse of an instance variable?
     
    Paul Clapham
    Sheriff
    Pie
    Posts: 20758
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Venny Ra wrote:Evrytime i click refresh, it goes to controller to fetch the same values again. and put it in arrayList.

    DO I make sense ?


    Absolutely that makes sense. Did you expect to see something different? Why?
     
    Anuj Batra
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi your solution is in the code you mentioned.
    Consider you have 1 doctor name in the DB
    the first time you go to the page the value gets populated from DB.

    but the second time you are just adding the option in dropdown (kinda appending) so it is getting loaded twice.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic