Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL SQL Tags

 
Justin Howard
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is it bad practice to add JSTL sql tags in the jsp?

If yes, then what is the alternative?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even the JSTL spec says to only use them for prototyping.

A properly designed MVC architecture is the best practice.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1; the best place to do that is in a place designed for it--servlet, action, whatever back-end technology you're using.

The idea is to separate presentation from business logic/etc.
 
Justin Howard
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I have two cases.

1. I have some of the form elements like checkboxes, radio buttons etc in the database, cause it changes very frequently and I sometimes get
requests to add another checbox to the list.
Is it best practice to use SQL jstl to display these checkboxes in the jsp page, or should I separate out this too.
Should I put this in a servlet too?

2. I have to query the database for the results to be display and then apply access logic. If I put it in the servlet how can I pass it to the jsp page ?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. It is never best practice to use the JSTL SQL tags. It is always best practice to never perform any database access outside the model layer.

2. All database access should take place in the model layer. Not even your servlet should have anything to do with a database. The model layer returns the result of any querying in normal, everyday Java collections or other constructs. resultsets should never rear their heads anywhere near the UI.
 
Justin Howard
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

But one more question. Suppose I have like a huge resultset that I need to display in the JSP page.
I obtain the results in the model layer. How can I pass it to the view?
Is it good to store it in the request?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. I will say it one more time. result sets should never get anywhere near the UI. The UI shouldn't even be able to smell them down-wind.

Grab the data from the resultset and store it in normal Java collections for passing out of the model layer.

Moreover, anything that knows about result sets should not know anything about requests or other UI artifacts. Keep your layers separate.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find this article helpful.
 
Justin Howard
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

Yes, I understood that.
Sorry, I wasn't clear.

I meant if have a resultset , I populate each record into an object.
Then put all objects in a an arraylist in the logic layer.

Do I put it in the request to send it to the jsp?
Will it be slow if I have a really huge collection for display?

Thanks




 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Essentially, your page controller servlet (see article) will ask the model layer for the data, and it will put it in a request scoped variable to carry to the JSP page which will be forwarded to.

With regards to size, how big are we talking here? You should never be sending sizable data sets to the JSP -- what's the user going to do when confronted with thousands of records?

Rather, give the user the ability to filter the results, and when the data set is still large, use paging techniques (also covered in the JSP FAQ) to limit the amount of data you inundate the user with.
 
Justin Howard
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I found the article very helpful. It is well written and importantly in a fun way.

Where can I find like a list of all the articles written in javaranch, so I could lookup for best practices in those articles.

Thanks.



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic