(JavaEE) Implementasi Java Bean dengan JSP 2

Setelah saya membaca beberapa kutipan dari oracle dan beberapa website mas eko, java Bean sangat banyak digunakan pada pemograman java yang berbasis PBO, kenapa kita harus menggunakan java bean? karena dengan menggunakan konsep bean kita dengan mudah membuat entitas yang akan kita panggil.

Edit File HTML seperti dibawah ini untuk menampung data inputan kita

 <%-- 
    Document   : index
    Created on : Jan 29, 2014, 3:27:48 PM
    Author     : Jarod United
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="post" action="SaveName.jsp">
            Nama : <input type="text" name="username" size="20"> <br />
            Email : <input type="text" name="email" size="20"> <br />
            Usia : <input type="text" name="age" size="20"> <br />
            <p> <input type="submit">
        </form>
        </body>
</html>

Create File JSP dengan nama NextPage.jsp

<%-- 
    Document   : NextPage
    Created on : Jan 29, 2014, 3:36:03 PM
    Author     : Jarod United
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<jsp:useBean id="user" class="user.UserData" scope="session" />
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
      Nama Anda =  <%= user.getUsername() %> <br />
      Email Anda = <%= user.getEmai()%> <br />
      Usia Anda =  <%= user.getAge()%> <br />
        
    </body>
</html>
 

Create File JSP dengan nama SaveName.jsp

 <%-- 
    Document   : SaveName
    Created on : Jan 29, 2014, 3:34:22 PM
    Author     : Jarod United
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<jsp:useBean id="user" class="user.UserData" scope="session" />
<jsp:setProperty name="user" property="*" />
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Data Save Name</h1>
        <a href="NextPage.jsp">  Lanjutkan </a>
    </body>
</html>

Create Java Class Dengan nama UserData.java

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package user;

/**
 *
 * @author Jarod United
 */
public class UserData {
    String username;
    String email;
    int age;
    
    public void setUsername(String value){
        this.username = value;
    }
    public void setEmail(String value){
        this.email = value;
    }
    public void setAge(int value){
        this.age = value;
    }
    
    public String getUsername(){
        return username;
    }
    public String getEmai(){
        return email;
    }
    public int getAge(){
        return age;
    }
}

Sehingga File pada Explorer Seperti ini

30-01-2014 8-12-44

jalankan Dan Hasil output seperti dibawah ini

29-01-2014 15-42-37

29-01-2014 15-42-47

29-01-2014 15-42-54

Advertisements

Published by

jarcode

hello I am jarcode

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