JavaBeans Example: Atoms.java, Atoms.jsp, Atoms.class, Atoms.html |
|
1. The steps
1. Create new directory : newBeans under:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\WEB-INF\classes\
directory. And then a new one under newBeans: newBeans\AtomBeans
2. Place the Java program Atoms in it :
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\WEB-INF\classes\newBeans\AtomBeans\Atoms.java
The Java program Atoms.java will have the following package:
package newBeans.AtomBeans; (subdirectories after \classes\)
3. Create a subdirectory (or subdirectories) under \webapps\ like this:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\NewWork\BeansAtoms\
\examples\ already exist; NewWork\BeansAtoms\ is created.
4. Place the JSP program Atoms.jsp in it as follows:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\examples\NewWork\BeansAtoms\Atoms.jsp
5. Rum the program Atoms.jsp by browsing:
http://localhost/examples/NewWork/BeansAtoms/Atoms.jsp
2. The bean program Java:Atoms.java
package newBeans.AtomBeans;
public class Atoms implements java.io.Serializable {
private static final long serialVersionUID = 1L;
//Fields = properties
private String element = null;
private int charge = 0;
private int shell = 0;
// Constructor
public Atoms() {}
// Methods: Setter
public void setelement(String element) {
this.element = element;
}
public void setcharge(int charge) {
this.charge = charge;
}
public void setshell(int shell) {
this.shell = shell;
}
// Method: Getter
public String getelement() {
return this.element;
}
public int getcharge() {
return this.charge;
}
public int getshell() {
return this.shell;
}
}
3. Atoms.jsp
<html>
<head>
<title> The atoms bean JSP Page</title>
</head>
<body>
<%@ page language="java" import="newBeans.AtomBeans.Atoms"%>
<%@ page contentType="text/html" %>
<%-- Create the JavaBean: of ID = Bohr --%>
<jsp:useBean id = "Bohr" scope="request" class = "newBeans.AtomBeans.Atoms">
<jsp:setProperty name = "Bohr" property = "element" value = "Hydrogen" />
<jsp:setProperty name = "Bohr" property = "charge" value="1" />
<jsp:setProperty name = "Bohr" property = "shell" value="1" />
</jsp:useBean>
<p>
Retrieve the properties from JavaBean and display their values :<br>
The charge and the shell of the element
<jsp:getProperty name="Bohr" property = "element"/>
are respectively:
<jsp:getProperty name="Bohr" property = "charge"/>
and
<jsp:getProperty name="Bohr" property = "shell"/>.
<br>
</p>
</body>
</html>
4.The result
The result under: http://localhost/examples/NewWork/BeansAtoms/Atoms.jsp
is:
Retrieve the properties from JavaBean and display their values :
The charge and the shell of the element Hydrogen are respectively: 1 and 1.
© The Scientific Sentence. 2007 |