leejeok

The Remarkable Everyday

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: