This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Pass data to JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Pass data to JSP page" Watch "Pass data to JSP page" New topic
Author

Pass data to JSP page

Blaine Swensen
Greenhorn

Joined: Jul 05, 2008
Posts: 6
I found this topic (javaranch) and this topic (SDN) while learning how to pass my sql result (which was changed to an ArrayList, so I can close the connection) to a jsp page.

Currently I have a servlet method that queries mysql. The jsp page calls the method as such:


Am I approaching this correctly? If not can you provide me with some sites to read?

I also found MVC example or tutorial, but I still need advice for outputting a mysql result to a jsp page.
[ July 05, 2008: Message edited by: ranchaz ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

"ranchaz", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

No. All code having to do with SQL should be as far removed from the UI as possible, and not be anywhere near a JSP page.

Your page controller should call methods in your model classes to obtain the results. As you read in the other topics, the results should not be still ensconced in the JDBC resultset, but copied to other collection types.
Blaine Swensen
Greenhorn

Joined: Jul 05, 2008
Posts: 6
Originally posted by Bear Bibeault:
No. All code having to do with SQL should be as far removed from the UI as possible, and not be anywhere near a JSP page.


So where can I find information on the proper placement of an SQL connection?
I would greatly appreciate an example diagram, war file, or description.

Originally posted by Bear Bibeault:
Your page controller should call methods in your model classes to obtain the results. As you read in the other topics, the results should not be still ensconced in the JDBC resultset, but copied to other collection types.


How I understand the mvc process currently is..
A client makes a request to a servlet >
The servlet (Page/Task Controller) operates on the request and gathers the necessary data to output >
The servlet sends that data (via unknown means-Still Learning) to jsp >
Server sends JSP response with the data the servlet sent to it.

I've been reading Scriptless JSP Pages: The Front Man but I am not familiar with what a java bean is, how it differs from a servlet, or how my servlet would interact with it, as well as how my servlet should interact with the jsp page.

I have, however understood that I should copy the resultset so I may close the connection. I use an ArrayList of Maps for rows & columns.
[ July 05, 2008: Message edited by: Blaine Swensen ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Originally posted by Blaine Swensen:
The servlet (Page/Task Controller) operates on the request and gathers the necessary data to output
Yes. but it also delegates to the model layer to perform operations on the model. All JDBC should be ensconced in the model layer.

The servlet sends that data (via unknown means-Still Learning) to jsp
The servlet usually sets scoped variables into request or session scope via the setAttribute() methods.

Server sends JSP response with the data the servlet sent to it.
Not quite. The JSP is executed to create the plain HTML response that is sent to the browser. JSP is a server-side concept.

but I am not familiar with what a java bean is, how it differs from a servlet
Beans are an important Java concept that you need to have under your belt before diving off into JSP and servlets. Essentially a bean is a Java class that follows certain conventions that make them easy to deal with in reflective fashion. Primarily they contain properties that can be set or accessed using methods whose signature follow the JavaBean standard.

I have, however understood that I should copy the resultset so I may close the connection. I use an ArrayList of Maps for rows & columns.
Good start!
Blaine Swensen
Greenhorn

Joined: Jul 05, 2008
Posts: 6
I've reviewed a couple sample applications SimpleTable and SimpleMVC and I see how java beans work and how the MVC interacts. I would like more information on the usage of java beans. Do you have any good information on the topic? I feel like Trail: JavaBeans(TM) is lacking in content related to web applications. Maybe I am not looking in the right places.

Thank you very much for your help so far!
[ July 06, 2008: Message edited by: Blaine Swensen ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41125
    
  45
Originally posted by Blaine Swensen:
I would like more information on the usage of java beans. Do you have any good information on the topic?


Check out the chapter "Using JavaBeans Components in JSP Documents here.


Ping & DNS - my free Android networking tools app
Blaine Swensen
Greenhorn

Joined: Jul 05, 2008
Posts: 6
Okay, now that I see how they are used and I read the chapter it makes perfect sense. Now that I understand bean basics I want to use beans!
I understand how to retrieve data of which I know the structure. I would have an accessor for each column.
I don't understand how I would retrieve a data set that relates to another data set.
Using Cattle Drive - Servlets - Videos as an example, I would know the title, star, etc., but I would want to also add a list of supporting actors of an unknown quantity, and have a dynamic type/genre list.
Using the Videos (now JDBC) as an example, I would imagine having a table of videos as such...

and also a table of supporting actors...

and a table of genres...


I would expect my JDBC model layer to retrieve the video, look for `SuppActors` of Video #, and then get the `TYPE` of video from `Genres`. I know what queries I would use, but to output that, is beyond me at this point.
[ July 06, 2008: Message edited by: Blaine Swensen ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Now you are into the realm of object modeling.
Blaine Swensen
Greenhorn

Joined: Jul 05, 2008
Posts: 6
There doesn't seem to be much on the topic. I found this but it seems short for what we are talking about.

Thanks for the help! You've really pointed me in the right direction!
[ July 07, 2008: Message edited by: Blaine Swensen ]
Blaine Swensen
Greenhorn

Joined: Jul 05, 2008
Posts: 6
Does anyone know of any additional information about model objects?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

What you've posted translates to beans fairly easily.

Obviously, you'd have a Video bean, with discrete members to represent scalar values like title and description.

And a List element to represent Genre (perhaps as a list of Strings, or of Genre instances), and a List of Actor instances for supporting actors.

(Personally, I'd have made made "star" a reference into the Actors table. I'd also have a Type table that translates the magic numbers to human-readable strings.)

Here's a rough first pass (assumes foreign ref for star):
[ July 07, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

You might want to read up on ORM (or gander at the ORM forum here) even if you don't plan to use an ORM tool such as Hibernate. There's a lot of information on Relational-->Object modeling to be had there.
[ July 07, 2008: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass data to JSP page
 
Similar Threads
Many ResultSet. How to do layout???
the jsp thing
Passing an Array to a Javascript function
Unable to see jsp output in eclipse
JavaRanch SimpleTable