Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax 24 th February 2005 Bogdan L. Vrusias
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © Contents JavaBeans Examples Sessions
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 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
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 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.
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 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
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 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).
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © JavaBeans: Basic Tasks In a JSP there are three main JavaBean constructs: –jsp:useBean –jsp:getProperty –Jsp:setProperty (CAREFUL) Otherwise
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 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: beanName must be the same as that specified in a useBean element (using the id attribute), and there must be a getPName method in the JavaBeans component.
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © JavaBean Example I StringBean.java package webtech; public class StringBean { private String message = "No message"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; }
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © JavaBean Example II StringBean.jsp... Initial value (getProperty): Initial value (JSP expression): Value after setting property with setProperty: Value after setting property with scriptlet:...
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © Sharing Beans (Default) –Bean is not shared and a new bean is created for each 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 –Bean is shared within a session –Bean is shared by all servlets and JSP pages in a Web application
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 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.
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © Sharing Beans: The JavaBean BakedBean.java package webtech; public class BakedBean { private String level = "half-baked"; private String goesWith = "hot dogs"; public String getLevel() { return(level); } public void setLevel(String newLevel) { level = newLevel; } public String getGoesWith() { return(goesWith); } public void setGoesWith(String dish) { goesWith = dish; }
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © Sharing Beans: Page Scope Example BakedBeanDisplay-page.jsp … Baked Bean Values: page-based Sharing Bean level: Dish bean goes with: …
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © Sharing Beans: Request Scope Example BakedBeanDisplay-request.jsp … Baked Bean Values: request-based Sharing <jsp:useBean id="requestBean" class="webtech.BakedBean" scope="request" /> Bean level: Dish bean goes with: …
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © Sharing Beans: Request Scope Example BakedBeanDisplay-snippet.jsp Repeated Baked Bean Values: request-based Sharing <jsp:useBean id="requestBean" class="webtech.BakedBean" scope="request" /> Bean level: Dish bean goes with:
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © Sharing Beans: Session Scope Example BakedBeanDisplay-session.jsp … Baked Bean Values: session-based Sharing <jsp:useBean id="sessionBean" class="webtech.BakedBean" scope="session" /> Bean level: Dish bean goes with: …
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © Sharing Beans: Application Scope Example BakedBeanDisplay-application.jsp … Baked Bean Values: application-based Sharing <jsp:useBean id="applicationBean" class="webtech.BakedBean" scope="application" /> Bean level: Dish bean goes with: …
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 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
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © JSP Sessions I 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.
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © JSP Sessions II Session tracking is a technique for maintaining user information across pages: –HTTP information (not used much… privacy issues) –Hidden fields (very popular… but again privacy issues) –Extended Path information and URL-rewriting ( privacy issues) Next –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")
Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © Closing Questions??? Remarks??? Comments!!! Evaluation!