Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax.

Similar presentations


Presentation on theme: "Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax."— Presentation transcript:

1 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 b.vrusias@surrey.ac.uk

2 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 20052 Contents JavaBeans Examples Sessions

3 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 20053 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

4 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 20054 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.

5 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 20055 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

6 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 20056 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).

7 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 20057 JavaBeans: Basic Tasks In a JSP there are three main JavaBean constructs: –jsp:useBean –jsp:getProperty –Jsp:setProperty (CAREFUL) Otherwise

8 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 20058 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.

9 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 20059 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; }

10 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200510 JavaBean Example II StringBean.jsp... Initial value (getProperty): Initial value (JSP expression): Value after setting property with setProperty: Value after setting property with scriptlet:...

11 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200511 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

12 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200512 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.

13 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200513 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; }

14 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200514 Sharing Beans: Page Scope Example BakedBeanDisplay-page.jsp … Baked Bean Values: page-based Sharing Bean level: Dish bean goes with: …

15 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200515 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: …

16 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200516 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:

17 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200517 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: …

18 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200518 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: …

19 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200519 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

20 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200520 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.

21 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200521 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")

22 Introduction to Server-Side Web Development 24 th February 2005Bogdan L. Vrusias © 200522 Closing Questions??? Remarks??? Comments!!! Evaluation!


Download ppt "Introduction to Server-Side Web Development Introduction to Server-Side Web Development Introduction to Server-Side Web JavaBeans; basic concepts and syntax."

Similar presentations


Ads by Google