leejeok

The Remarkable Everyday

Archive for the ‘J2EE’ Category

JSP AND JAVABEANS – PASSING PARAMETER

Posted by leejeok on August 11, 2007

Description: Simple demo of javabean to receive and pass back parameter to JSP page. The objective here is to assemble all html codes to be place within the JSP page. Any processing of business logic would be code within the JAVABEANS. As a result, the HTML developer will focus any changes of the presentation / interface and allowing the JAVA developer to focus on the business logic to be define within the JAVABEANS.

Best Practice:

  1. JavaBean class must not have argument constructor.
  2. Provide the properties to customize the bean.
  3. Define the getter method to retrieve the property. No parametes and must return an object of the type of the property.
  4. Define the setter method to modify the property. Take a single parameter and return a void.

Technology Coverage: JSP and JAVABEANS

Sample of the code of index.jsp:

<jsp:useBean id=”_detailbean” class=”org.detailbean” scope=”session”/>

   <%
   String name = request.getParameter(“name”);
   if(name!= null)
       _detailbean.setName(name);
   %>
<html>
    <head>
        <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
        <title>JSP Page</title>
    </head>
    <body>
        <form name=myform method=post>
            Param Value: <input type=”text” name=”name” value=”” />
            <input type=”submit” value=”Submit” />
            <br>
            Your parameter: <%=_detailbean.getName()%>
        </form>
    </body>
</html>

Sample of the code of detailbean.java:

package org;
public class detailbean implements java.io.Serializable {
    private String name = “”;
   
    /** Creates a new instance of checkbean */
    public detailbean() {
    }
   
    //define getter
    public String getName() {
        return name;
    }
   
    //define setter
    public void setName(String name) {
        this.name = name;
    }
}

Result

simple_param_result_11082007.jpg

I have been using NetBeans 5.5 to perform the development. Full souce code available Click HERE.

Advertisements

Posted in J2EE, JAVABEANS, JSP | Leave a 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:

<html>
<body>

<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
    http.send(null);
}

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>
    }
}

–>
</script>

<h1>Have you heard these books before?</h1>
<p>
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>
</p>

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


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}’
</sql:query>

<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>
</c:forEach>
 

3. Result

bookstore

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

Three-Tier Architecture

Posted by leejeok on June 13, 2007

Technology Coverage: JSP, JAVA class, JSTL, Connection Pool, JNDI, JDBC, Tomcat, and three-tier architecture.

Required Software
1. NetBeans IDE 5.5
3. MySQL database
 

Planning the Structure
1. Simple web applications often designed using three-tier architecture.
2. Presentation Layer – the user interface.
3. Logic Layer – the functional process logic.
4. Data Layer – the data access and storage are all maintained independently.

4. By popularity, three-tier architecture is best to used when developing a client/server web application. It reduce the complexity of the design from user. Also, increase the flexibility, ease of maintenance, reusability and scalability of design for developer. 

Posted in J2EE | Leave a Comment »