JavaBeans; basic concepts and syntax 23 rd February 2005 Bogdan L. Vrusias b.vrusias@surrey.ac.uk Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 2 Contents JavaBean definition and syntax Sharing JavaBean components JavaBeans Examples Session Tracking Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 3 Invoking Java code from JSP
Call Java code directly Call Java code indirectly Use beans Use the Model-View-Controller architecture Use the JSP expression language (EL) Use custom tags Simple application or small development team Complex application or large development team Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 4 JavaBeans JavaBeans is a portable (platform-independent) component model written in Java and was developed in collaboration with industry leaders. JavaBeans components are Java classes that can be easily reused and composed together into applications. Any Java class that follows certain design conventions can be a JavaBeans component. JavaServer Pages technology directly supports using JavaBeans components with JSP language elements. JavaBeans will minimize the code on the JSP. Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 5 JavaBeans: Advantages No Java syntax in the JSP Stronger separation between content and presentation Good for separating Web and Java developers Simple object sharing Due to the JSP bean constructs Convenient correspondence between request parameters and object properties Simple process of reading request parameters Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 6 JavaBeans: Basics A bean class must have a zero-argument (default) constructor. A bean should have no public instance variables (fields). Persistent values should be accessed through methods called setXxx and getXxx (or isXxx for Boolean). Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 7 JavaBeans: Basic Tasks In a JSP there are three main JavaBean constructs: jsp:useBean <jsp:useBean id="beanName" class="package.classname" scope="scope"/>
Otherwise <%= beanName.getPName() %> <% beanName.setPName() %> Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 8 JavaBeans: Basic Tasks Properties of a JavaBean class must simply be accessible using public methods that conform to certain conventions: For each readable property, the bean must have a method of the form PropertyClass getProperty() { ... } For each writable property, the bean must have a method of the form setProperty(PropertyClass pc) { ... }
NOTE 1: beanName must be the same as the one specified in a useBean element (using the id attribute), and there must be a getPName method in the JavaBeans component. NOTE2: The official version of a Java Bean class extends the Object class and implements the Serializable class. But for simplicity reasons we will not be doing that. Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 9 JavaBean Example I StringBean.java package webtech.w6;
public class StringBean { private String message = "No message";
public String getMessage() { return(message); }
public void setMessage(String message) { this.message = message; } } Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 10 JavaBean Example II StringBean.jsp ... <jsp:useBean id="stringBean" class="webtech.StringBean" /> <p>Initial value (getProperty): <I><jsp:getProperty name="stringBean" property="message" /></I></p> <p>Initial value (JSP expression): <I><%= stringBean.getMessage() %></I></p> <jsp:setProperty name="stringBean" property="message" value=Say something" /> <p>Value after setting property with setProperty: <I><jsp:getProperty name="stringBean" property="message" /> </I></p> <% stringBean.setMessage(... well ok, something!"); %> <p>Value after setting property with scriptlet: <I><%= stringBean.getMessage() %></I></p> ... Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 11 Sharing Beans <jsp:useBean scope="page"/> (Default) Bean is not shared and a new bean is created for each request
<jsp:useBean scope="request"/> Same as "page" scope but, two JSP pages or a JSP page and a servlet will share the bean when you use jsp:include
<jsp:useBean scope="session"/> Bean is shared within a session
<jsp:useBean scope="application"/> Bean is shared by all servlets and JSP pages in a Web application Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 12 Sharing Beans: Example This example demonstrates the use of JavaBeans and how the beans are shared. There are four possibilities: Page Request Session Application For this example these is one JavaBean that stores two parameters, and five JSP pages to demonstrate the use of the bean. To test the bean sharing we will try to pass parameters by directly entering the parameters within the URL: scope_page.jsp?sport=cricket&rating=boring
Parameter 1 Parameter 2 Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 13 Sharing Beans: The JavaBean msgBean.java package webtech.w6; public class msgBean { private String sport = "football"; private String rating = "fun";
public String getRating() { return rating; } public void setRating(String newRating) { rating = newRating; } public String getSport() { return sport; } public void setSport(String newSport) { sport = newSport; } } Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 14 Sharing Beans: Page Scope Example scope_page.jsp
Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 19 Installing JavaBeans Each JavaBean should be located within your web application under: WEB-INF/classes
Or, if the bean is within a package (recommended), then: WEB-INF/classes/subdirectoryMatchingPackageName
Or, if the bean is within a .JAR, then place the .JAR in: WEB-INF/lib
Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 20 JSP Session Tracking A session can be defined as a series of related interactions between a single client and the server, which take place over a period of time. A session object can be used for storing and retrieving information. Every time the client accesses the resources on the server, the client provides the session ID that was assigned by the server. A session has a one-to-one association between a client and the server. Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 21 JSP Session Tracking Session tracking is a technique for maintaining user information across pages:
Extended Path information and URL-rewriting ( privacy issues) <a href="/myPage.jsp?login=james&item=book_1"> Next</a> Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 22 JSP Session Tracking Cookies (data can be encrypted) Cookie uid = new Cookie("uid", "234ff543333c"); response.addCookie(uid);
Session (data can be encrypted) session.putValue("user_id", user_id); session.getValue("user_id")
JavaBeans using the session scope Server-Side Web Development 23 rd February 2006 Bogdan L. Vrusias 2006 23 Closing