wood burning stoves 2.0*
The moose likes JSP and the fly likes JSTL SQL Tags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL SQL Tags " Watch "JSTL SQL Tags " New topic
Author

JSTL SQL Tags

Justin Howard
Ranch Hand

Joined: Feb 19, 2009
Posts: 162
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

Joined: Jan 10, 2002
Posts: 60077
    
  65

Even the JSTL spec says to only use them for prototyping.

A properly designed MVC architecture is the best practice.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

+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

Joined: Feb 19, 2009
Posts: 162
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

Joined: Jan 10, 2002
Posts: 60077
    
  65

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

Joined: Feb 19, 2009
Posts: 162
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

Joined: Jan 10, 2002
Posts: 60077
    
  65

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

Joined: Jan 10, 2002
Posts: 60077
    
  65

You might find this article helpful.
Justin Howard
Ranch Hand

Joined: Feb 19, 2009
Posts: 162
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

Joined: Jan 10, 2002
Posts: 60077
    
  65

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

Joined: Feb 19, 2009
Posts: 162
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

Joined: Jan 10, 2002
Posts: 60077
    
  65

The JavaRanch Journal main page is here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL SQL Tags
 
Similar Threads
SCWCD Beta
JSTL Pagination and Deletion
Java Beans in Jsp
Generating RSS from JSP
javax.el.PropertyNotFoundException: Property 'empname' not found on type java.lang.String