110 likes | 301 Views
Chapter 6. Using JavaBeans Components in JSP Pages. What Is a Bean?. Implement a Java Bean. use a package name A bean class must have a no-argument constructor The bean properties are accessed through getter and setter methods
E N D
Chapter 6 Using JavaBeans Components in JSP Pages
Implement a Java Bean • use a package name • A bean class must have a no-argument constructor • The bean properties are accessed through getter and setter methods • the bean class should implement the java.io.Serializable or the java.io.Externalizable interface • The property name is case-sensitive and always starts with a lowercase letter
Reading Bean Properties • Using the <jsp:getProperty> Action • <jsp:getProperty name="cartoon" property="fileName" /> • Using the JSP Expression Language • <img src="images/${cartoon.fileName}"> • If you need to generate an image dynamically, you should use a servlet • <img src="imageGenServlet?width=100&height=100">
Setting Bean Properties • Using the <jsp:setProperty> Action • <jsp:setProperty name="msg" property="category" value="thoughts" /> • Using the JSTL <c:set> Action • <c:set target="${msg}" property="category" value="thoughts" />
To set a JSP action attribute to the value produced by another action
Automatic Type Conversions • When you use the <jsp:setProperty> or the JSTL <c:set> action, the container takes care automatically of the conversion from text values to the Java types
Setup JavaBean • Setup Environment Variables • path %java_home%\bin;%path% • set ClassPath %java_home%\lib • Compile JavaBean • Javac CartoonBean.java • Javac –classpath servlet-api.jar MyServlet.java • Create directories in classes • com\ora\jsp\beans\motd • Copy MyBean.class to com\ora\jsp\beans\motd • Reload Web Application