Download - Week8 Jsp Final Remarks

Transcript
  • 8/11/2019 Week8 Jsp Final Remarks

    1/17

    Server-Side Web Development

    Server-Side Web Development

    JSP Final Remarks

    09thMarch 2006

    Bogdan L. Vrusias

    [email protected]

  • 8/11/2019 Week8 Jsp Final Remarks

    2/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 2

    Contents

    Java Collections API and Casting

    Server-Side Dynamic Construction of JavaScript

    Login Pattern Example

    Importing and Using JavaBeans within JavaBeans

  • 8/11/2019 Week8 Jsp Final Remarks

    3/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 3

    Java Collection API

    Array

    Vector

    Hashtable

    ArrayList

    LinkedList

    ...

    import java.util.*;

  • 8/11/2019 Week8 Jsp Final Remarks

    4/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 4

    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];

  • 8/11/2019 Week8 Jsp Final Remarks

    5/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 5

    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();

    }

  • 8/11/2019 Week8 Jsp Final Remarks

    6/17

  • 8/11/2019 Week8 Jsp Final Remarks

    7/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 7

    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/11/2019 Week8 Jsp Final Remarks

    8/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 8

    Java Type Conversion / Casting

    If vis a Vectortype 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 objectsString s =

    ((Vector)v.elementAt(x)).elementAt(y).toString();

  • 8/11/2019 Week8 Jsp Final Remarks

    9/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 9

    Server-Side Dynamic Construction of JavaScript

    Target output (for the client machine):

    ...

    function doit() {

    var names = new Array(5);

    names[0] = "Tony";

    names[1] = "Mike";

    names[2] = "Sarah";

    names[3] = "Helen";

    names[4] = "Alex";

    for (i = 0; i < names.length; i++) {

    alert(names[i]);

    }

    }

    ...

  • 8/11/2019 Week8 Jsp Final Remarks

    10/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 10

    Server-Side Dynamic Construction of JavaScript

    JSP processed on the server side:

    ...

    function doit() {

    var names = new Array();

    names[] = "";

    for (i = 0; i < names.length; i++) alert(names[i]);

    }

    ...

  • 8/11/2019 Week8 Jsp Final Remarks

    11/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 11

    Server-Side Dynamic Construction of JavaScript

    Follow the class demo

  • 8/11/2019 Week8 Jsp Final Remarks

    12/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 12

    Login Pattern Example

    First we create a JavaBean that checks the username and password of auser, and then stores the status of the login.

    Usernames and password can be stored either within a database (mostcommon) or within a file that is not accessible externally.

    Use the JavaBean within the login page to check the password. If thelogin is incorrect then keep the user within the login page, otherwiseredirect to the desired page.

    Each consequent page that should only be accessed by logged in usersshould first of all (at the top of the page) use the previous loginJavaBean to check the status of the login. If the user is logged in thenthe page is displayed, otherwise the user is redirected to the login page.

  • 8/11/2019 Week8 Jsp Final Remarks

    13/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 13

    Login Pattern Example

    login.jsp

    securePage.jsp

    check

    login

    loginBean

    correct

    incorrect

    submit formaccess page

    get login

  • 8/11/2019 Week8 Jsp Final Remarks

    14/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 14

    Login Pattern Example

    Follow the class demo

  • 8/11/2019 Week8 Jsp Final Remarks

    15/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 15

    Importing & Using JavaBeans within JavaBeans

    To invoke and use a JavaBean from another JavaBean just follow the standard

    Java approach of calling any other Java Class:

    Import the appropriate library (not necessary if the class is in the same package)

    import webtech.lab5.MyClass;

    If the class has a state (is an object) then instantiate it.

    MyClass mc = new MyClass();

    Call the methods within the new object

    mc.setMethod(abc);

    String v = mc.getMethod();

  • 8/11/2019 Week8 Jsp Final Remarks

    16/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 16

    Importing & Using JavaBeans within JavaBeans

    Follow the class demo

  • 8/11/2019 Week8 Jsp Final Remarks

    17/17

    Server-Side Web Development

    09thMarch 2006 Bogdan L. Vrusias 2006 17

    Closing

    Questions???

    Remarks???

    Comments!!!

    Evaluation!