Home
Course
Service
Contact
About Us

 

 

 

 

 

 

 

 

 


ตัวอย่างการใช้ Bean และ action ต่างๆ

หลังจากเรียนรู้เรื่องเกี่ยวกับ Bean กันบ้างแล้ว เราจะทดลองเขียน pages ที่ต้องใช้ Bean มาช่วย ก่อนอื่นให้เขียน html ที่จะใช้ติดต่อกับ user ตามตัวอย่าง โดยบันทึกเป็น c:\jswdk\myexam\jsp\guessDice.html

<HTML>

<HEAD>

<TITLE>Tell me what you think</TITLE>

</HEAD>

<BODY>

Guess 1-6 <br>

<form action="dice.jsp" method="post" name="guessForm">

<input type="text" name="inputNumber">

<input type="submit" name="Submit" value="Guess">

</form>

</BODY>

</HTML>

และเขียน jsp ทำหน้าที่ action จาก html ข้างบน โดยบันทึกเป็น c:\jswdk\myexam\jsp\dice.jsp

<jsp:useBean id="diceBean" class="net.energythai.DiceBean" scope="session"/>

<jsp:setProperty name="diceBean" property="inputNumber" param="inputNumber"/>

<% String inputStr = request.getParameter("inputNumber");

int inputNumber = Integer.parseInt(inputStr);

int result = diceBean.getResult();

if (result == -1) { %>

Only, number 1-6 you can pick man!!!<br>

<% } else if (inputNumber == result) { %>

Number: <jsp:getProperty name="diceBean" property="inputNumber"/><br>

<% out.print("Wow you're right!!! Like it huh?</br>");

} else {

out.print("Sorry, I like " + result + " but you picked " + inputNumber + ".<br>");

} %>

Guess 1-6 <br>

<form method="post" name="guessForm">

<input type="text" name="inputNumber">

<input type="submit" name="Submit" VALUE="Guess">

</form>

และสุดท้ายคือ Bean ซึ่งเราต้องเขียน java code ก่อน แล้ว compile ออกมาเป็น DiceBean.class เพื่อให้ใช้งานได้อย่างง่ายๆ เราจะบันทึก javacode เป็น c:\jswdk\myexam\WEB-INF\jsp\beans\net\energythai\DiceBean.java

package net.energythai;

import java.util.Random;

public class DiceBean {

private Random rand;

private int inputNumber = -1;

public DiceBean() {

rand = new Random();

}

public void setInputNumber(int i) {

inputNumber = i;

}

public int getInputNumber() {

return inputNumber;

}

public int getResult() {

if (inputNumber < 0 || inputNumber > 6) {

return -1;

}

// return a number between 1-6

return rand.nextInt(6)+1;

}

}

หลังจากได้เรียนรู้หลักการต่างๆกันพอสมควรแล้ว ต่อไปเราจะลองทำตัวอย่างที่ซับซ้อนขี้น ซี่งอาจต้องใช้ทั้ง jsp และ ฺbean ซี่ง code ทีให้ไว้ตามตัวอย่างข้างล่าง จะยังไม่สมบูรณ์ เพื่อได้ใช้เป็นแบบฝีกหัด หาโอกาสทดลองเขียนเพิ่มเติมด้วยตัวเอง

ตัวอย่างแรกเป็นการจัดการกับ error ในส่วนของerror handler ให้บันทึกเป็น c:\jswdk\myexam\jsp\errorhandling\errhandler.jsp หรือถ้าใช้ tomcat ก็ให้บันทึกไว้ที่ c:\tomcat\webapps\myexam\jsp\errorhandling\errhandler.jsp

<%-- ใส่คำสั่ง: indicate the location of the error handler using the page tag --%>

<html>

<body>

<form method=post action="errhandler.jsp">

What's the coolest programming language in the known universe?<p>

Java<input type=radio name=language value="JAVA" checked>

C++<input type=radio name=language value="CPP">

Visual Basic<input type=radio name=language value="VB">

<p>

<input type=submit>

</form>

<%

if (request.getMethod().equals("POST")) {

if (request.getParameter("language").equals("JAVA")) {

out.println("<hr><font color=red>You got that right!</font>");

} else {

// ใส่คำสั่ง: thow a new exception initializing it with some message

}

}

%>

</body>

</html>

สำหรับส่วนที่เป็นหน้าของ error page ให้บันทึกเป็น c:\jswdk\myexam\jsp\errorhandling\errorpage.jsp หรือถ้าใช้ tomcat ก็ให้บันทึกไว้ที่ c:\tomcat\webapps\myexam\jsp\errorhandling\errorpage.jsp

<%-- ใส่คำสั่ง: Indicate that this is an error page using the page tag --%>

<html>

<body>

<h1>

Error Page

</h1>

<hr>

<h2>

Received the exception:<br>

<font color=red>

<%= exception.toString() %>

</font>

</h2>

</body>

</html>

ต่อไปเราจะศึกษาเรื่อง scope และเพื่อให้เห็นความแตกต่างได้อย่างชัดเจน ให้เขียนโปรแกรม Counter ที่เรียกใช้ Bean ในสองลักษณะที่แตกต่างกัน คือเป็น session กับ application ในตัวอย่างนี้ก็จะมีทั้งส่วนที่เป็น jsp และส่วนที่เป็น Bean สำหรับส่วน script นั้น ให้บันทึกเป็นไฟล์ c:\jswdk\myexam\jsp\counter\Counter.jsp หรือถ้าใช้ tomcat ให้บันทึกไว้ที่ c:\tomcat\webapps\myexam\jsp\counter\Counter.jsp

<%@ page import="net.energythai.CounterBean" %>

<%-- provide appropriate values for the class and scope attributes --%>

<jsp:useBean id="session_counter" class="ใส่คำสั่ง:" scope="ใส่คำสั่ง:" />

<jsp:useBean id="app_counter" class="ใส่คำสั่ง:" scope="ใส่คำสั่ง:" />

<% session_counter.increaseCount();

synchronized(page) {

app_counter.increaseCount();

}

%>

<h3>

Number of accesses within this session:

<%-- provide appropriate values for the name attribute --%>

<jsp:getProperty name="ใส่คำสั่ง:" property="count" />

</h3>

<p>

<h3>

Total number of accesses:

<%-- provide appropriate values for the name attribute --%>

<% synchronized(page) { %>

<jsp:getProperty name="ใส่คำสั่ง:" property="count" />

<% } %>

</h3>

สำหรับส่วนที่เป็น Bean เราต้องเขียนโปรแกรมจาวาก่อนแล้ว compile ด้วยการเปิด console ใหม่ขึ้นมา set environment ให้พร้อมใช้จาวา แล้วสั่ง

javac CounterBean.java

โดยนำ class file ที่ได้ ไปเก็บไว้ตามที่กำหนดของ server

ในที่นี้เพื่อให้ง่ายขึ้น จะเก็บ java source code ไว้ที่เดียวกันกับ Bean ที่เราจะใช้เลย ซี่งถ้าใช้ container เป็น jswdk ให้บันทึกไว้ที่ c:\jswdk\myexam\WEB-INF\jsp\beans\net\energythai\CounterBean.java หรือ ถ้าใช้ tomcat ก็ให้บันทึกไว้ที่ c:\tomcat\webapps\myexam\WEB-INF\classes\net\energythai\CounterBean.java

package net.energythai;

public class CounterBean {

//ใส่คำสั่ง

//declare a integer for the counter

public int getCount() {

//ใส่คำสั่ง

//return count

}

public void increaseCount() {

//ใส่คำสั่ง

//increment count;

}

}

ตัวอย่างต่อไป เราจะสร้าง form เพื่อให้ user สามารถกรอกข้อมูลหรือแสดงความคิดเห็น หรืออะไรต่างๆแล้วแต่จะใช้งาน โดยในที่นี้จะมีทั้งส่วนที่เป็น jsp pages และส่วนที่เป็น Bean สำหรับส่วน script นั้น ให้บันทึกเป็นไฟล์ c:\jswdk\myexam\jsp\Form\form.jsp หรือถ้าใช้ tomcat ให้บันทึกไว้ที่ c:\tomcat\webapps\myexam\jsp\Form\form.jsp

<html>

<body bgcolor="#c8d8f8">

<form action="form.jsp" method=post>

<center>

<table cellpadding=4 cellspacing=2 border=0>

<th bgcolor="#CCCCFF" colspan=2>

<font size=5>User Registration</font>

</th>

<tr>

<td valign=top>

<b>First Name</b>

<br>

<input type="text" name="firstName" size=15></td>

<td valign=top>

<b>Last Name</b>

<br>

<input type="text" name="lastName" size=15></td>

</tr>

<tr>

<td valign=top colspan=2>

<b>E-Mail</b>

<br>

<input type="text" name="email" size=20>

<br></td>

</tr>

<tr>

<td valign=top colspan=2>

<b>What languages do you program in?</b>

<br>

<input type="checkbox" name="languages" value="Java">Java&nbsp;&nbsp;

<input type="checkbox" name="languages" value="C++">C++&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type="checkbox" name="languages" value="C">C<br>

<input type="checkbox" name="languages" value="Perl">Perl&nbsp;&nbsp;

<input type="checkbox" name="languages" value="COBOL">COBOL

<input type="checkbox" name="languages" value="VB">VB<br>

</td>

</tr>

<tr>

<td valign=top colspan=2>

<b>How often can we notify you regarding your interests?</b>

<br>

<input type="radio" name="notify" value="Weekly" checked>Weekly&nbsp;&nbsp;

<input type="radio" name="notify" value="Monthly">Monthly&nbsp;&nbsp;

<input type="radio" name="notify" value="Quarterly">Quarterly

<br></td>

</tr>

<tr>

<td align=center colspan=2>

<input type="submit" value="Submit"> <input type="reset" value="Reset">

</td>

</tr>

</table>

</center>

</form>

<%-- Create the bean only when the form is posted --%>

<%

if (request.getMethod().equals("POST")) {

%>

<jsp:useBean id="formHandler" class="net.energythai.FormBean">

<jsp:setProperty name="formHandler" property="*"/>

</jsp:useBean>

<p>

<hr>

<font color=red>

<b>You submitted:<P>

First Name:</b><br>

<jsp:getProperty name="formHandler" property="firstName"/><br>

<b>Last Name:</b><br>

<jsp:getProperty name="formHandler" property="lastName"/><br>

<b>Email:</b><br>

<jsp:getProperty name="formHandler" property="email"/><br>

<b>Languages:</b><br>

<%

String[] lang = formHandler.getLanguages();

if (!lang[0].equals("1")) {

out.println("<ul>");

for (int i=0; i<lang.length; i++)

out.println("<li>"+lang[i]);

out.println("</ul>");

} else out.println("Nothing was selected<br>");

%>

<b>Notification:</b><br>

<jsp:getProperty name="formHandler" property="notify"/><br>

<%

}

%>

</font>

</body>

</html>

สำหรับส่วนที่เป็น Bean เราต้องเขียนโปรแกรมจาวาก่อนแล้ว compile โดยนำ class file ที่ได้ ไปเก็บไว้ตามที่กำหนดของ server ในที่นี้เพื่อให้ดูง่ายขึ้น จะเก็บ java source code ไว้ที่เดียวกันกับ Bean ที่เราจะใช้เลย ซี่ง ถ้าใช้ container เป็น jswdk ให้บันทึกไว้ที่ c:\jswdk\myexam\WEB-INF\jsp\beans\net\energythai\FormBean.java หรือ ถ้าใช้ tomcat ก็ให้บันทึกไว้ที่ c:\tomcat\webapps\myexam\WEB-INF\classes\net\energythai\FormBean.java

package net.energythai;

public class FormBean {

//ใส่คำสั่ง (declare)

//ใส่คำสั่ง (declare)

//ใส่คำสั่ง (declare)

//ใส่คำสั่ง (declare)

// declare properties for firstname, lastname, notify and email with the names

// matching the corrosponding form input elements

private String[] languages;

public FormBean() {

firstName="";

lastName="";

email="";

languages = new String[] { "1" };

notify="";

}

//ใส่คำสั่ง (method)

//...........

//ใส่คำสั่ง (method)

//write getter methods for firstname, lastname, notify, email and languages

//write setter methods for firstname, lastname, notify, email and languages

}

 



:: ศูนย์ฝึกอบรมบางปะกง การไฟฟ้าฝ่ายผลิตแห่งประเทศไทย 8/4 หมู่ 8 ตำบลท่าข้าม อำเภอบางปะกง จังหวัดฉะเชิงเทรา 24130 ::