Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Server-Side Web Development Introduction to Server-Side Web Development JSP Final Remarks 10 th March 2005 Bogdan L. Vrusias

Similar presentations


Presentation on theme: "Introduction to Server-Side Web Development Introduction to Server-Side Web Development JSP Final Remarks 10 th March 2005 Bogdan L. Vrusias"— Presentation transcript:

1 Introduction to Server-Side Web Development Introduction to Server-Side Web Development JSP Final Remarks 10 th March 2005 Bogdan L. Vrusias b.vrusias@surrey.ac.uk

2 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 20052 Contents Java Collections API and Casting JavaBeans – Final Remarks Session Tracking

3 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 20053 Java Collection API Array Vector Hashtable ArrayList LinkedList... import java.util.*;

4 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 20054 Array Example int[] anArray; anArray = new int[10]; for (int i = 0; i < anArray.length; i++) { anArray[i] = i; System.out.print(anArray[i] + " "); } boolean[] answers = { true, false, true, false }; MyObject[] anObjectArray = new MyObject[5];

5 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 20055 Multidimensional Array Example int[][] aMatrix = new int[4][]; for (int i = 0; i < aMatrix.length; i++) { aMatrix[i] = new int[5]; for (int j = 0; j < aMatrix[i].length; j++) { aMatrix[i][j] = i + j; } for (int i = 0; i < aMatrix.length; i++) { for (int j = 0; j < aMatrix[i].length; j++) { System.out.print(aMatrix[i][j] + " "); } System.out.println(); }

6 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 20056 Vector Example Vector v = new Vector(); v.addElement("Hello"); v.addElement(new Integer(99)); v.addElement(99); // Error for (int i=0; i < v.size(); i++) System.out.println(v.elementAt(i)); // or v.get(i) Integer a = (Integer) v.elementAt(1);

7 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 20057 Hashtable Example Hashtable nums = new Hashtable(); nums.put("one", new Integer(1)); nums.put("two", new Integer(2)); Integer n = (Integer)nums.get("two"); if (n != null) { System.out.println("two = " + n); } for (Enumeration e = nums.keys (); e.hasMoreElements ();) { System.out.println (e.nextElement ().toString ()); } if (nums.containsKey ("one")) {...}

8 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 20058 Java Type Conversion / Casting If v is a Vector type collection object then: Getting a String from Vector String s = (String)v.elementAt(x); Vector of Vector objects Vector v = (Vector)v.elementAt(x); Getting a String from Vector of Vector objects String s = ((Vector)v.elementAt(x)).elementAt(y).toString();

9 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 20059 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

10 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 200510 Servlets vs. JSP JSPs and servlets are two different ways to accomplish the same goal: generating dynamic HTML pages using Java code. One puts Java code in your HTML, and one puts HTML in your Java code. Functionally, they are equivalent. In fact, under the covers, the web server takes a JSP and converts it to the corresponding servlet and dynamically compiles it. BUT servlets have the following deficiencies: –It is hard to write and maintain the HTML –You cannot use standard HTML tools –The HTML is inaccessible to non-Java developers

11 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 200511 JSP Example Hello World The date is: <% dateBean.setDate("9-Mar-2005"); %> The new date is:

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

13 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 200513 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 JavaBeans will minimize the code on the JSP.

14 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 200514 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; }

15 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 200515 JavaBean Example II StringBean.jsp

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

17 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 200517 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.

18 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 200518 JSP Sessions II Session tracking is a technique for maintaining user information across pages. Unsecured / Not recommended: –HTTP information (not used much… privacy issues) –Hidden fields (very popular… but again privacy issues) –Extended Path information and URL-rewriting ( privacy issues) Next

19 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 200519 JSP Sessions III More secure / Recommended: –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")

20 Introduction to Server-Side Web Development 10 th March 2005Bogdan L. Vrusias © 200520 Closing Questions??? Remarks??? Comments!!! Evaluation!


Download ppt "Introduction to Server-Side Web Development Introduction to Server-Side Web Development JSP Final Remarks 10 th March 2005 Bogdan L. Vrusias"

Similar presentations


Ads by Google