Introduction to ServerSide Web Development Introduction to ServerSide
Introduction to Server-Side Web Development Introduction to Server-Side Web Java. Beans; basic concepts and syntax 24 th February 2005 Bogdan L. Vrusias b. vrusias@surrey. ac. uk 24 th February 2005 Bogdan L. Vrusias © 2005
Introduction to Server-Side Web Development Contents • Java. Beans • Examples • Sessions 24 th February 2005 Bogdan L. Vrusias © 2005 2
Introduction to Server-Side Web Development Invoking Java code from JSP Simple application or small development team • Call Java code directly • Call Java code indirectly • Use beans • Use the Model-View-Controller architecture Complex application or large development team 24 th February 2005 • Use the JSP expression language (EL) • Use custom tags Bogdan L. Vrusias © 2005 3
Introduction to Server-Side Web Development Java. Beans • Java. Beans is a portable (platform-independent) component model written in Java and was developed in collaboration with industry leaders. • Java. Beans 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 Java. Beans component. • Java. Server Pages technology directly supports using Java. Beans components with JSP language elements. • Java. Beans will minimize the code on the JSP. 24 th February 2005 Bogdan L. Vrusias © 2005 4
Introduction to Server-Side Web Development Java. Beans: 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 24 th February 2005 Bogdan L. Vrusias © 2005 5
Introduction to Server-Side Web Development Java. Beans: 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 set. Xxx and get. Xxx (or is. Xxx for Boolean). 24 th February 2005 Bogdan L. Vrusias © 2005 6
Introduction to Server-Side Web Development Java. Beans: Basic Tasks • In a JSP there are three main Java. Bean constructs: – jsp: use. Bean <jsp: use. Bean id="bean. Name" class="package. classname" scope="scope"/> – jsp: get. Property <jsp: get. Property name="bean. Name" property="p. Name"/> – Jsp: set. Property <jsp: set. Property name="bean. Name" property="p. Name" value="p. Value" /> <jsp: set. Property name="bean. Name" property="*" /> (CAREFUL) • Otherwise <%= bean. Name. get. PName() %> <% bean. Name. set. PName() %> 24 th February 2005 Bogdan L. Vrusias © 2005 7
Introduction to Server-Side Web Development Java. Beans: Basic Tasks • Properties of a Java. Bean 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 Property. Class get. Property() {. . . } – For each writable property, the bean must have a method of the form set. Property(Property. Class pc) {. . . } • NOTE: bean. Name must be the same as that specified in a use. Bean element (using the id attribute), and there must be a get. PName method in the Java. Beans component. 24 th February 2005 Bogdan L. Vrusias © 2005 8
Introduction to Server-Side Web Development Java. Bean Example I String. Bean. java package webtech; public class String. Bean { private String message = "No message"; public String get. Message() { return(message); } public void set. Message(String message) { this. message = message; } } 24 th February 2005 Bogdan L. Vrusias © 2005 9
Introduction to Server-Side Web Development Java. Bean Example II String. Bean. jsp. . . <jsp: use. Bean id="string. Bean" class="webtech. String. Bean" /> <p>Initial value (get. Property): <I><jsp: get. Property name="string. Bean" property="message" /></I></p> <p>Initial value (JSP expression): <I><%= string. Bean. get. Message() %></I></p> <jsp: set. Property name="string. Bean" property="message" value="Best string bean: Fortex" /> <p>Value after setting property with set. Property: <I><jsp: get. Property name="string. Bean" property="message" /> </I></p> <% string. Bean. set. Message("My favorite: Kentucky Wonder"); %> <p>Value after setting property with scriptlet: <I><%= string. Bean. get. Message() %></I></p>. . . 24 th February 2005 Bogdan L. Vrusias © 2005 10
Introduction to Server-Side Web Development Sharing Beans • <jsp: use. Bean … scope="page"/> (Default) – Bean is not shared and a new bean is created for each request • <jsp: use. Bean … 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: use. Bean … scope="session"/> – Bean is shared within a session • <jsp: use. Bean … scope="application"/> – Bean is shared by all servlets and JSP pages in a Web application 24 th February 2005 Bogdan L. Vrusias © 2005 11
Introduction to Server-Side Web Development Sharing Beans: Example • This example demonstrates the use of Java. Beans and how the beans are shared. • There are four possibilities: – – • Page Request Session Application For this example these is one Java. Bean that stores two parameters, and five JSP pages to demonstrate the use of the bean. 24 th February 2005 Bogdan L. Vrusias © 2005 12
Introduction to Server-Side Web Development Sharing Beans: The Java. Bean Baked. Bean. java package webtech; public class Baked. Bean { private String level = "half-baked"; private String goes. With = "hot dogs"; public String get. Level() { return(level); } public void set. Level(String new. Level) { level = new. Level; } public String get. Goes. With() { return(goes. With); } public void set. Goes. With(String dish) { goes. With = dish; } } 24 th February 2005 Bogdan L. Vrusias © 2005 13
Introduction to Server-Side Web Development Sharing Beans: Page Scope Example Baked. Bean. Display-page. jsp … <H 1>Baked Bean Values: page-based Sharing</H 1> <jsp: use. Bean id="page. Bean" class="webtech. Baked. Bean" /> <jsp: set. Property name="page. Bean" property="*" /> <H 2>Bean level: <jsp: get. Property name="page. Bean" property="level" /></H 2> <H 2>Dish bean goes with: <jsp: get. Property name="page. Bean" property="goes. With" /></H 2> … 24 th February 2005 Bogdan L. Vrusias © 2005 14
Introduction to Server-Side Web Development Sharing Beans: Request Scope Example Baked. Bean. Display-request. jsp … <H 1>Baked Bean Values: request-based Sharing</H 1> <jsp: use. Bean id="request. Bean" class="webtech. Baked. Bean" scope="request" /> <jsp: set. Property name="request. Bean" property="*" /> <H 2>Bean level: <jsp: get. Property name="request. Bean" property="level" /></H 2> <H 2>Dish bean goes with: <jsp: get. Property name="request. Bean" property="goes. With" /></H 2> <jsp: include page="Baked. Bean. Display-snippet. jsp" /> … 24 th February 2005 Bogdan L. Vrusias © 2005 15
Introduction to Server-Side Web Development Sharing Beans: Request Scope Example Baked. Bean. Display-snippet. jsp <H 1>Repeated Baked Bean Values: request-based Sharing</H 1> <jsp: use. Bean id="request. Bean" class="webtech. Baked. Bean" scope="request" /> <H 2>Bean level: <jsp: get. Property name="request. Bean" property="level" /></H 2> <H 2>Dish bean goes with: <jsp: get. Property name="request. Bean" property="goes. With" /></H 2> 24 th February 2005 Bogdan L. Vrusias © 2005 16
Introduction to Server-Side Web Development Sharing Beans: Session Scope Example Baked. Bean. Display-session. jsp … <H 1>Baked Bean Values: session-based Sharing</H 1> <jsp: use. Bean id="session. Bean" class="webtech. Baked. Bean" scope="session" /> <jsp: set. Property name="session. Bean" property="*" /> <H 2>Bean level: <jsp: get. Property name="session. Bean" property="level" /></H 2> <H 2>Dish bean goes with: <jsp: get. Property name="session. Bean" property="goes. With" /></H 2> … 24 th February 2005 Bogdan L. Vrusias © 2005 17
Introduction to Server-Side Web Development Sharing Beans: Application Scope Example Baked. Bean. Display-application. jsp … <H 1>Baked Bean Values: application-based Sharing</H 1> <jsp: use. Bean id="application. Bean" class="webtech. Baked. Bean" scope="application" /> <jsp: set. Property name="application. Bean" property="*" /> <H 2>Bean level: <jsp: get. Property name="application. Bean" property="level" /></H 2> <H 2>Dish bean goes with: <jsp: get. Property name="application. Bean" property="goes. With" /></H 2> … 24 th February 2005 Bogdan L. Vrusias © 2005 18
Introduction to Server-Side Web Development Installing Java. Beans • Each Java. Bean should be located within your web application under: WEB-INF/classes • Or, if the bean is within a package (recommended), then: WEB-INF/classes/subdirectory. Matching. Package. Name • Or, if the bean is within a. JAR, then place the. JAR in: WEB-INF/lib 24 th February 2005 Bogdan L. Vrusias © 2005 19
Introduction to Server-Side Web Development 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. 24 th February 2005 Bogdan L. Vrusias © 2005 20
Introduction to Server-Side Web Development 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) <input type=“hidden” name=“my. Key” value=“my. Value” > – Extended Path information and URL-rewriting ( privacy issues) <a href=“/my. Page. jsp? login=james&item=book_1”> Next</a> – Cookies (data can be encrypted) Cookie uid = new Cookie(“uid”, “ 234 ff 543333 c”); response. add. Cookie(uid); – Session (data can be encrypted) session. put. Value("user_id", user_id); session. get. Value("user_id") 24 th February 2005 Bogdan L. Vrusias © 2005 21
Introduction to Server-Side Web Development Closing • Questions? ? ? • Remarks? ? ? • Comments!!! • Evaluation! 24 th February 2005 Bogdan L. Vrusias © 2005 22
- Slides: 22