Java Beans, tag libraries and JSPs - what and why.
Java Programming - training course!
- just completed. We went a long way from the first steps in Java on Monday through to Servlets and Java Server Pages (JSPs) on Friday - and yet in many ways, our course goes full circle. For, through Java Server Pages, you can keep the look and feel of a web site separated from the business logic, and produce meaningful pages that contain - in the HTML - remarkablly little code if any. This excellent approach (sometimes known as MVC / Model, View, Controller) allows you to have your web page appearence looked after by those members of your team who are good graphic artists, and to have your actions looked after by other members of the team - the good analyst / programmers. You have a good interface between the two, you can change the way the thing works without the appearance, and the appearance without changing how it works. And you can have the page updated easily without having to employ a multi-skilled person - a graphic artist who's a programmer too (and who is, perhaps, only mediocre at one of those skills).
To show you what I mean ... from Friday, [here]
is a link to a JSP page. You'll find that it has a grand total of two complete Java statements, and two other references to Java variables. The rest is all HTML.
A further example - [here]
adds in a bit more code - but the complexity of doing the MySQL database lookup and presenting the results back has all been put into the net.wellho.Book class which is the responsibility of the programmer
We can get rid of even that small amount of Java source code by providing the graphic artist with a tag library
- in the third example in this series, [here]
, all the Java has gone to be replaced by tags which are very similar to HTML tags, but are replaced by results on the web server
before being sent out to the site visitor's browser / client
As well as the standard tag library, there are othe libraries available such as Spring and Struts. Those are beyond the scope of a course that takes you from "what is Java" in just a week - but at the end of the week it's good to be given the roadmap ahead and know where you may be headed.
• Above here - the graphic artist
• Below here - the programmer
The examples above apply a look and feel to a database lookup that needs to be codes. And the API (Application programmer interface) that the tag library uses is a Java Bean. What that means is that the program logic is going to be in a Java class, which must have a constructor that takes no parameters. Information (properties) are put in using methods with names like setIsbn
where the word "set" is used to identify this as a method which supplies data to th eclass, and getAuthor
where the word "get" identifies the method as one to extract data.
The net.wellho.Book class is such a bean - and that means that the tag libraries (which require that such a convention has been followed, thus turning it into a rule as far as they are concerned) can make use of it. You can find the source of the class [here]
and you'll note that - like all good classes - it has its own main
test harness so that you can check it out independently of the JSP / tag library / web level. (written 2010-11-13)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesA508 - Web Application Deployment - Jakarta and StrutsJ811 - Java - JDBC - Relational Database Access 
MySQL and Java - connectivity past, present, and future thoughts - (2010-07-09) 
Bean Classes in Java and Java Database Connections - (2009-05-02) 
MySQL - an FAQ - (2005-12-03)J812 - Java Beans 
No subject or title? - (2009-05-03) 
Java beans and classes, .war and .jar files - (2007-02-10)J907 - JSP - JavaServer Pages 
Servlet v JSP (Java Server Page). What is the difference? - (2013-02-06) 
Using JSPs, Tag Libraries, Java Beans, Tomcat in one short example - (2009-12-04) 
Servlet life cycle, and Java Servlet variable scope - (2009-05-16) 
What is a JSP tag library? - (2009-05-01) 
A very easy JSP (Java Server Page) - (2009-05-01) 
Java (JSP and Servlet examples) live on our server - (2008-02-23) 
The HTML++ Metalanguage - (2007-01-22) 
Last day of Java - (2004-10-08)
Some other Articles
Views of Melksham - November 2010Computer Graphics is fun - even if Java Applets are Old HatString handling - from first steps to practical examplesWhat is a universal superclass? Java / Perl / Python / Other OO languagesJava Beans, tag libraries and JSPs - what and why.After Course Resources - do we publish sample answers. Example from Java Exceptions module.Changing a Servlet - more that just editing and compilingGathering information - logging - with log4j. First steps.Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in PerlJava - basic rules for arithmetic, variables and conversion