Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Server-Side Web Development Introduction to Server-Side Web Development JSP Final Remarks 10th March 2005 Bogdan L. Vrusias [email protected] Introduction to Server-Side Web Development Contents • Java Collections API and Casting • JavaBeans – Final Remarks • Session Tracking 10th March 2005 Bogdan L. Vrusias © 2005 2 Introduction to Server-Side Web Development Java Collection API • Array • Vector • Hashtable • ArrayList • LinkedList • ... import java.util.*; 10th March 2005 Bogdan L. Vrusias © 2005 3 Introduction to Server-Side Web Development 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]; 10th March 2005 Bogdan L. Vrusias © 2005 4 Introduction to Server-Side Web Development 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(); } 10th March 2005 Bogdan L. Vrusias © 2005 5 Introduction to Server-Side Web Development 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); 10th March 2005 Bogdan L. Vrusias © 2005 6 Introduction to Server-Side Web Development 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")) {...} 10th March 2005 Bogdan L. Vrusias © 2005 7 Introduction to Server-Side Web Development 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(); 10th March 2005 Bogdan L. Vrusias © 2005 8 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 10th March 2005 • Use the JSP expression language (EL) • Use custom tags Bogdan L. Vrusias © 2005 9 Introduction to Server-Side Web Development 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 10th March 2005 Bogdan L. Vrusias © 2005 10 Introduction to Server-Side Web Development JSP Example <HTML> <HEAD><TITLE>Hello World</TITLE></HEAD> <body> <jsp:useBean id="dateBean" class="webtech.DateFormatBean" /> <p>The date is: <%= dateBean.getDate() %></p> <% dateBean.setDate("9-Mar-2005"); %> <p>The new date is: <%= dateBean.getDate() %></p> </body></html> 10th March 2005 Bogdan L. Vrusias © 2005 11 Introduction to Server-Side Web Development 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. 10th March 2005 Bogdan L. Vrusias © 2005 12 Introduction to Server-Side Web Development 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. 10th March 2005 Bogdan L. Vrusias © 2005 13 Introduction to Server-Side Web Development 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; } } 10th March 2005 Bogdan L. Vrusias © 2005 14 Introduction to Server-Side Web Development JavaBean Example II StringBean.jsp <jsp:useBean id="sBean" class="webtech.StringBean" /> <jsp:getProperty name="sBean" property="message" /> <%= sBean.getMessage() %> <jsp:setProperty name="sBean" property="message" value=“message 1" /> <jsp:getProperty name="sBean" property="message" /> <% sBean.setMessage(“message 2"); %> <%= sBean.getMessage() %> 10th March 2005 Bogdan L. Vrusias © 2005 15 Introduction to Server-Side Web Development 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 10th March 2005 Bogdan L. Vrusias © 2005 16 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. 10th March 2005 Bogdan L. Vrusias © 2005 17 Introduction to Server-Side Web Development 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) <input type="hidden" name="myKey" value="myValue" > – Extended Path information and URL-rewriting ( privacy issues) <a href="/myPage.jsp?login=james&item=book_1"> Next</a> 10th March 2005 Bogdan L. Vrusias © 2005 18 Introduction to Server-Side Web Development 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") 10th March 2005 Bogdan L. Vrusias © 2005 19 Introduction to Server-Side Web Development Closing • Questions??? • Remarks??? • Comments!!! • Evaluation! 10th March 2005 Bogdan L. Vrusias © 2005 20