aspose file tools*
The moose likes JSP and the fly likes JSP page getting values from DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP page getting values from DB" Watch "JSP page getting values from DB" New topic
Author

JSP page getting values from DB

Venny Ra
Greenhorn

Joined: Nov 10, 2011
Posts: 5
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

Joined: Jan 10, 2002
Posts: 61424
    
  67

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.


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Venny Ra
    Greenhorn

    Joined: Nov 10, 2011
    Posts: 5
    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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18657
        
        8

    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

    Joined: Nov 10, 2011
    Posts: 5
    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

    Joined: Jan 10, 2002
    Posts: 61424
        
      67

    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

    Joined: Jul 06, 2005
    Posts: 1018
    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

    Joined: Jan 10, 2002
    Posts: 61424
        
      67

    Wanna bet it's a misuse of an instance variable?
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18657
        
        8

    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

    Joined: Sep 18, 2011
    Posts: 24
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JSP page getting values from DB