I was wondering if developer in this forum that uses Java as their platform for developing web application or n-tier web application, is familiar with JSTL or JSF. What is JSTL or JSF?
The JavaServer Pages Standard Tag Library (JSTL), is a component of the Java EE Web application development platform. It extends the JSP specification by adding a tag library of JSP tags for common tasks, such as XML data processing, conditional execution, loops and internationalization. JSTL was developed under the Java Community Process (JCP) as JSR 52. As of 2006, the current version of JSTL is 1.1.
JSTL provides an effective way to embed logic within a JSP page without using embedded Java code directly. The use of a standardized tag set, rather than breaking in and out of Java code leads to more maintainable code and enables separation of concerns between the development of the application code and user interface.
While JSF is JavaServer Faces (JSF) is a Java-based Web application framework that simplifies the development of user interfaces for Java EE applications. Out of the box, JSF uses JavaServer Pages for its display technology, but JSF can also accommodate other display technologies, such as XUL, for example.
To use these technologies in the development process of a web application (Forms) we simple create a backing bean or JavaBeans. JavaBean is a reusable component that can be used in any Java application development environment. JavaBeans are dropped into an application container, such as a form, and can perform functions ranging from a simple animation to complex calculations.
These beans can connect to a datasource using either DAO (Data Access Object) or a JNDI (Java Naming and Directory Interface)
The Java Naming and Directory InterfaceTM (JNDI) is a standard extension to the Java platform, providing Java-enabled applications with a unified interface to multiple naming and directory services in the enterprise.A datasource is a logical name to a physical connection to a database using mapping technology found in JNDI. Many java web container have the configuration section to allow this process i.e Tomcat, BEA, IBM Webspere, Sun AppServ, WebLogic, JBoss etc. Look at a typical DAO connection to a database
/*
* StoreAccesDAO.java
*
* Created on 10 March 2006, 02:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ejb.dao;
/**
*
* @author Administrator
*/
public interface StoreAccesDAO {
public String loginUser(String username, String passwd);
}
*
* StoreAccessDAOImp.java
*
* Created on 10 March 2006, 02:59
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ejb.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.InitialContext;
import javax.sql.DataSource;
/**
*
* @author Administrator
*/
public class StoreAccessDAOImp implements StoreAccesDAO{
DataSource jdbcFactory;
/** Creates a new instance of StoreAccessDAOImp */
public StoreAccessDAOImp() {
InitialContext cxt =null;
if(jdbcFactory == null ){
try{
jdbcFactory = (DataSource) cxt.lookup("java:comp/env/jdbc/mysql");
}catch(Exception e){
e.getMessage();
}//End catch
}//End if
}//End StoreAccesDAOImp
public String loginUser(String username, String passwd){
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String userId = null;
boolean result;
String query ="select from storeaccess where username=? and password=? ";
try{
con = jdbcFactory.getConnection();
stmt = con.prepareStatement(query);
stmt.setString (1, username);
stmt.setString(2, passwd);
rs = stmt.executeQuery ();
result = rs.next();
if(result){
userId = rs.getString("userid");
}//End if
}catch(Exception e){
e.getMessage();
}//End catch
finally{
try{
con.close();
stmt.close();
rs.close();
}catch(Exception e){
e.getMessage();
}
}
return userId;
}//End loginUser
}
Now in the web tier, we can call this backing bean like this
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library, action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%@taglib uri="
http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="
http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="
http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="bundle.Messages" var="Message"/>
<jsp:useBean scope="request" id="demo" class="ejb.dao.StoreAccessDAOImp"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Digitemsystems Online Banking</title>
</head>
<body>
<h1>Online Banking</h1>
<f:view>
<h:form id="form1">
<h:outputText value="#{Message.username}" />
<h:inputText id="username" value="#{demo.username}" required="true">
<f:validateLength minimum="2" maximum="10"/>
</h:inputText>
<h:outputText value="#{Message.password}" />
<h:inputText id="passwd" value="#{demo.password}" required="true">
<f:validateLength minimum="2" maximum="10"/>
</h:inputText>
<h:commandButton id="submit" action="" value="login"/>
</h:form>
</f:view>
</body>
</html>
It is very rewording to program Business application in this form. It is modular and provide someone without knowledge of java to work in the front-end system without knowing what is happening in the beans side.
Loose coupling is the key to any software development. If the back-end function changes for any reason the front tier do not necessary need to change as it is relational Mapping using JNDI and other front-end framework like JSF and JSTL.