aspose file tools*
The moose likes JSP and the fly likes Using Java Bean in JSP to show database record Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using Java Bean in JSP to show database record" Watch "Using Java Bean in JSP to show database record" New topic
Author

Using Java Bean in JSP to show database record

Mike Jenkins
Ranch Hand

Joined: Jul 23, 2006
Posts: 57
I have links in my Tomcat container that if someone clicks on a specific link it should go to a Record page (Show.jsp) with all the values associated with a record from a database.
For example if someone clicks on a specific link like this:
<a href="Show.jsp?lastname='Jones'">LinkExample</a>
it would take you to a jsp with database info for someone named Jones and show you his info:
Lastname = Jones
Firstname = Mike
City = San Diego

I would like to do this using a Java helper class and bean so I dont have any database connection or Java code in my JSP.

I created Java class file that has Database connection that works with a Java bean. I just dont know how to get the Show.jsp to work with the Java Bean and Java helper class file.

Here is what I have for Show.jsp and this is the part I have been working on the longest but cant get it to work because it doesnt seem to work with the database:

My Java Helper class that compiles and connects to database:


My Java Bean that compiles here:

[ April 22, 2007: Message edited by: Mike Jenkins ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Before doing anything else, please let us know what container and version of JSP that you are using. The code you posted is doing things using old-fashioned mechanisms and patterns.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Jenkins
Ranch Hand

Joined: Jul 23, 2006
Posts: 57
Originally posted by Bear Bibeault:
Before doing anything else, please let us know what container and version of JSP that you are using. The code you posted is doing things using old-fashioned mechanisms and patterns.


Thanks,

I am using Tomcat 4.1.27 with JDK 1.4 and I dont have JSTL due to restrictions in my environment for downloading software.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

OK, that explains much.

After my obligatory urging for you to convince the powers-that-be to update to a non-prehistoric version of Tomcat, I'll ask my next question.

Any reason you are sticking to Model 1 vs a Model 2 pattern?
Mike Jenkins
Ranch Hand

Joined: Jul 23, 2006
Posts: 57
I didnt think I needed Model 2 for this process.

Can you give me guidance on the way I am doing it or guidance on adding a Servlet to my attempt for more modern Model 2 way?

Either way I appreciate any suggestions to make this work.

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

I think that model 2 is appropriate for any web app that does any backend processing.

Firstly, you might want to read this article.

What i'd do is to have the original link invoke a servlet. The servlet would delegate the fetching of the user data to a UI-agnostic class in the model (what I think you termed the helper class). The model would return the user information, in the form of a bean-formatted Data Transfer Object (what you are thinking of as the bean) to the servlet. The servlet would tack the bean as a scoped variable onto the request and forward to the JSP.

All of the above is as appropriate for the JSP 1.2 environment as for a more modern setup.

On the JSP is where the main difference lies. In a JSP 2.0 setup, you'd use JSTL and EL. Under JSP 1.2 without JSTL, you'll need to use scriplets and sciptlet expressions.

The <jsp:useBean> action would be used to "hook up" the JSP to the bean that the servlet placed in request scope. The setProperty action you have on your original page is wrong -- it's used to populate a bean from request parameters. Remove it.

You could use the getProperty actions to access the bean, but you could also just use scriptlet expressions.
Mike Jenkins
Ranch Hand

Joined: Jul 23, 2006
Posts: 57
Thanks for your help especially on a Sunday night!

I will now take your guidance and apply it and if any problems I hope I can ask for assistance on here again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

But of course!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Java Bean in JSP to show database record