You are on page 1of 17

Server-Side Web Development

Server-Side Web Development


JSP Final Remarks
09
th
March 2006
Bogdan L. Vrusias
b.vrusias@surrey.ac.uk
Server-Side Web Development
09
th
March 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
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 3
Java Collection API
Array
Vector
Hashtable
ArrayList
LinkedList
...

import java.util.*;

Server-Side Web Development
09
th
March 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];
Server-Side Web Development
09
th
March 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();
}
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 6
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);
Server-Side Web Development
09
th
March 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")) {...}
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 8
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();
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 9
Server-Side Dynamic Construction of JavaScript
Target output (for the client machine):
...
<script language="JavaScript">
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]);
}
}
</script> ...
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 10
Server-Side Dynamic Construction of JavaScript
JSP processed on the server side:
...
<%
String[] a = {"Tony", "Mike", "Sarah", "Helen", "Alex"};
%>
<script language="JavaScript">
function doit() {
var names = new Array(<%= a.length %>);
<% for (int i = 0; i < a.length; i++) { %>
names[<%=i%>] = "<%=a[i]%>";
<% } %>

for (i = 0; i < names.length; i++) alert(names[i]);
}
</script> ...
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 11
Server-Side Dynamic Construction of JavaScript
Follow the class demo
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 12
Login Pattern Example
First we create a JavaBean that checks the username and password of a
user, and then stores the status of the login.

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

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

Each consequent page that should only be accessed by logged in users
should first of all (at the top of the page) use the previous login
JavaBean to check the status of the login. If the user is logged in then
the page is displayed, otherwise the user is redirected to the login page.
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 13
Login Pattern Example
login.jsp
securePage.jsp
check
login
loginBean
correct
incorrect
submit form access page
get login
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 14
Login Pattern Example
Follow the class demo
Server-Side Web Development
09
th
March 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();
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 16
Importing & Using JavaBeans within JavaBeans
Follow the class demo
Server-Side Web Development
09
th
March 2006 Bogdan L. Vrusias 2006 17
Closing

Questions???
Remarks???
Comments!!!
Evaluation!

You might also like