JavaRanch » Java Forums »
Java »
JSP
| Author |
how to connect to database and display query vqlus using proper java/jsp
|
khamhielle gabriel
Greenhorn
Joined: Aug 15, 2011
Posts: 16
|
|
hello there,
dont know if i post this question in the right place but can anyone help me with this:
i am new to jsp and with months of coding i found out that i am doing it the WRONG WAY
and since i dont want to continue doing like that i decided to fix it up.
i have a jsp page here where in everything was just all mixed up (eg : the design and functions)
what i want to do now is to do it properly and that is to
separate the design into the connections and functionalities.
but i dont really know how to start with it.
the class thing i supposed? (please correct me if im wrong)
looking forward for your help and advices. thank you so much. >.<
God Bless you
|
 |
Bear Bibeault
Author and opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50693
|
|
I recommend novices to JSP read the following articles: The Secret Life of JSPsThe Front Man
The first will make sure that you understand what JSP is, and how it operates.
The second will answer your question regarding proper web application structure.
And above all, remember to keep Java code out of the JSP.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
khamhielle gabriel
Greenhorn
Joined: Aug 15, 2011
Posts: 16
|
|
hello Bear Bibeault, :)
i really appreciate the articles you have shown me.
and it did help me to understand some things about jsp .
and its great.
but the thing is, i still dont know how to get started with
the question i ask a while ago.
how to connect to database and display query values using proper java/jsp?
and now i was eager more to learn how .
super thanks.
God Bless you.
|
 |
Tarun Bolla
Ranch Hand
Joined: Jun 20, 2011
Posts: 85
|
|
Hi khamhielle,
You need to separate the presentation from business logic.. Here is a simple how
You need a servlet (say DisplayController.java)
You need a JSP (MyView.jsp)
Then..through your web browser.. you should call the servlet (assuming you have configured the servlet in web.xml)
In the servlet, you will retrieve the contents from database and put the contents in a list or map or any data structure of your choice. Then you need to get that variable ready to be accessible by UI. That means you should send the variable by adding it at as an attribute to the request.
Then pass on the view to JSP. You can do that by using the RequestDispatcher
In JSP you should use your scriptlets/JSTL to just iterate over the results variable that is retreived from attributes which is a list or or map and display them
Hope its clear.. Can ask if its not!
|
 |
 |
|
|
subject: how to connect to database and display query vqlus using proper java/jsp
|
|
|
|