• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Java Bean in JSP to show database record

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mike Jenkins
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But of course!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic