The Remarkable Everyday

Archive for the ‘JSTL’ Category

Simple JNDI Accessing MySQL Datasource

Posted by leejeok on May 27, 2008

Base on the previous post, I have used the struts-config.xml to define the datasource to access to the MySQL database. The datasource class is code within the Action class (controller layer) and map to the struts-config.xml.


In order to improve the programming pratice, the datasource can be created via JNDI and the java code should be sit within the model layer. As a result, business logic will separate from the controller layer and making the Action class without knowing any relation with business class at all.


I am using Apache Tomcat 5.x and so, I will define the datasource resource via JNDI within the context.xml.


<Context path=”/bookstore”>

<ResourceLink global=”jdbc/bookstoreDB” name=”jdbc/bookstoreDB” type=”javax.sql.DataSource”/>



The java code to make the database connection accessing the JNDI would be look like this:


    Connection cn(){

        DataSource ds = null;


        try {

            Context ctx = new InitialContext();

            if ( ctx == null ) {

                throw new RuntimeException(“JNDI Context could not be found.”);


            ds = (DataSource)ctx.lookup(“java:comp/env/jdbc/bookstorDB”);


            if ( ds == null ) {

                throw new RuntimeException(“DataSource could not be found.”);


            return ds.getConnection();

            // Handle any JNDI errors

        } catch (NamingException ne) {

            throw new RuntimeException(“A JNDI error occured. ” + ne.getMessage());


        }catch(Exception e){

            System.err.println(“Boom – No Context”);


        return null;



Alternative, you can use JSP Standard Tag Library (JSTL) without embedding any Java code. I am using JSTL SQL tag here and may look like this:


<%@ taglib prefix=”sql” uri=”http://java.sun.com/jsp/jstl/sql&#8221; %>

<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core&#8221; %>


<sql:setDataSource dataSource=”jdbc/bookstoreDB” />


<sql:query var=”qryBooks” >

    SELECT bookID, bookTitle, bookAuthor, bookPublisher FROM book




    <c:forEach var=”row” items=”${qryBooks.rows}”>










Posted in JDBC, JSTL, MySQL, Struts | 1 Comment »

Ajax and JSTL

Posted by leejeok on August 7, 2007

Simple Web Application using AJAX and JSTL

Description: By having the clickable link, result will be publish on the same site without reloading the site. Result was retrieving from the database.

Technology Coverage: AJAX , JSTL, and MYSQL

Tools: NetBeans 5.5, MySQL 4.1 and MySQL Administrator

View code here

1. Program the index.jsp

– Ajax script will be code here.

– Here are the code:


<script language=”Javascript” type=”text/javascript”>

function createRequestObject() {
    var tmpXmlHttpObject;
//depending on what the browser supports,
//use the right way to create the XMLHttpRequest object
    if (window.XMLHttpRequest) {
        // Mozilla, Safari would use this method …
        tmpXmlHttpObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // IE would use this method …
        tmpXmlHttpObject = new ActiveXObject(“Microsoft.XMLHTTP”);
    return tmpXmlHttpObject;

//call the above function to create the XMLHttpRequest object
var http = createRequestObject();

function makeGetRequest(wordId) {
//make a connection to the server …
//specifying that you intend to make a GET request
    //to the server. Specifiy the page name and the URL parameters to send
    http.open(‘get’, ‘view.jsp?id=’ + wordId);
    //assign a handler for the response
    http.onreadystatechange = processResponse;
    //actually send the request to the server

function processResponse() {
    //check if the response has been received from the server
    if(http.readyState == 4){
        //read and assign the response from the server
        var response = http.responseText;
        //do additional parsing of the response, if needed
        //in this case simply assign the response
//to the contents of the <div> on the page.
        document.getElementById(‘description’).innerHTML = response;
        //If the server returned an error message like a 404 error,
//that message would be shown within the div tag!!.
        //So it may be worth doing some basic error before
//setting the contents of the <div>


<h1>Have you heard these books before?</h1>
Professional Apache Tomcat 6  <a href=”javascript:makeGetRequest(1)” mce_href=”javascript:makeGetRequest(1)”>More</a><br>
Beginning SharePoint 2007 Administration: Windows SharePoint Services 3.0 and Microsoft Office  <a href=”javascript:makeGetRequest(2)” mce_href=”javascript:makeGetRequest(2)”>More</a><br>
Expert Access 2007 Programming <a href=”javascript:makeGetRequest(3)” mce_href=”javascript:makeGetRequest(3)”>More</a><br>

<div id=”description”></div>

2. Program the view.jsp

– JSTL script will be code here. 

– Display the actual result which retrieving data from mysql database.

– Here are the code:

<%@ taglib prefix=”sql” uri=”http://java.sun.com/jsp/jstl/sql” %>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>

<sql:setDataSource dataSource=”jdbc/bookstoreDB” />

<sql:query var=”qryItems” >
    SELECT title, author, publisher, description
    FROM bookdetail
    WHERE id = ‘${param.id}’

<c:forEach var=”row” items=”${qryItems.rows}”>
    Title: <c:out value=”${row.title}” /><br>
    Author: <c:out value=”${row.author}” /><br>
    Publisher: <c:out value=”${row.publisher}” /><br><br>
    Description: <c:out value=”${row.description}” /><br>

3. Result


Posted in AJAX, J2EE, JSTL | Leave a Comment »