Chapter 16: JavaServer Pages: Part II
pages 1 - 16
Writing JavaBeans
- Write a bean. A bean is a reusable, platform independent
component.
- Place the code in this directory:
C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT\WEB-INF\classes
- At the DOS prompt, compile the file with:
javac -d . name.java.
Note: if javac is not in your classpath, follow the
instructions
here
- Place a jsp file in
C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT
or any of its subdirectories.
- Start Apache Tomcat. Note: it is very important to start Tomcat
after the WEB-INF\classes
directory has been created so that Tomcat can
find relevant packages.
- Load the above .jsp file into Apache Tomcat.
Relevant JSP Commands
- <jsp:useBean id="beanName" class="package.class" />
- <jsp:getProperty id="beanName" property="propertyName" />
- <jsp:setProperty id="beanName" property="propertyName" value="newValue" />
JavaBean Scope
The scope attribute specifies how long a bean exists in
memory. The scopes will be demonstrated with
Counter.java.
- Page Scope (the default).
Example.
- Request Scope. The bean is kept alive when the following
is encountered: <jsp:forward page="relativeURL" />
- Session Scope. The bean is in scope until the browser
session is closed.
Example.
- Application Scope. The bean is in scope until Apache
Tomcat is restarted.
Example.
Shopping Cart Example
- ShopCart.java
- example6.html
- example6-1.jsp
- example6-2.jsp
Exercise
- Make the above Shopping Cart Example work on your laptop: 3 points.
- Look at and understand the four files that make up the Shopping
Cart Example. Be able to answer questions that Jack and John
ask you about the code. 2 points.