Upload
eugenia-gardner
View
224
Download
4
Embed Size (px)
Citation preview
Lecture 1 - Introduction
ENTERPRISE JAVA
2
Contents Staff Assignments Topics Readings
3
Unit Staff
Dr John Casey Email: [email protected] Room 3008, building 183 Appointments can be made via email
4
My Experience Oracle Certified Java Programmer (OCJP) Programmer
P2P, Sockets, Distributed Systems, Information Retrieval Motor Registry System – State of Tasmania Fines and Infringement Notices Database – State of
Tasmania PayGlobal – Victorian Country Fire Authority LifeLink - Births, Deaths and Marriages State of NSW …
Educator Deakin University, Melbourne Unitec
5
Unit Objectives Exposure to a variety of new Java
technologies Develop your programming ability Ability to use online resources
Reading other peoples code Quickly discern what is good and what is bad Use API guidelines to find required function
Develop ability to learn new technologies Become fluent in Java and the open source
Java technology stack
6
Who should do this unit? Students that are nearly ready to
graduate You already know how to program, and
want to learn some cool new technologies
You are inquisitive and you are ready to do a lot of work out of class to develop your career
You want to know what else is out there (open source frameworks etc.) – ninja programming techniques, design patterns etc.
7
Who should not do this subject? Students that are still having problems
with syntax and logic Students that did well in lower level
programming subjects through sheer brute force
Students can do well if they are Motivated Have Self-Control and Perseverance Have a logical mind Enjoy programming
8
Technologies Apache Lucene
Open source free text search engine Breaks documents into keywords, counts word
frequencies, builds an index Indexes keywords by document, weights query
matches based on term frequency Apache Struts
Model View Controller Separate business logic, database, screen and
control Based on JSP and Servlets
9
Technologies Java Server Faces
Another Model View Controller framework Component based and event oriented Uses Facelets which is another presentation tech
(similar to JSP) Hibernate
Object-Relational Mapping between code and database
Auto generates code and database mappings based on either class files or database schema
Saves lots of time as you do not have to embed SQL in your application
10
Technologies Spring Framework
Acts as a router for different system components
Looks at class / method properties using Java Reflection
Objects bound at runtime instead of at compile time Allows system components to be swapped Allows testing using mock objects
Handles transactions and data access layer
11
Technologies Socket Programming
Synchronous and Asynchronous Sockets Java Enterprise Edition
Servers like Jboss Java Beans
Mule – Enterprise Service Bus Event-driven batching system
Reports BIRT Jasper Reports
12
Assessments
Lab Exercises: 30% In class practical exercises due every week,
can be submitted by email up to one week late.
Assignment: 45% Two major assignments
Final exam: 25% Code and theory
13
Foundations Web Application Development Servlets and CGI JSP JDBC
14
Common Gateway Interface Allows web server to generate page content
using external program Web server re-directs I-O messages to external
command line program – usually written in Perl/Python/Bash script
Simple interface parameters supplied by HTTP GET request QUERY_STRING The part of the URL after ?
If you have lots of users - setting up the environment / process for your external CGI program can be slow...
15
Servlets Java + CGI Servlets run inside the web server
Each servlet runs inside its own thread or its own process Much faster: web server no longer needs to setup an entire
environment each time a user makes a request Sandboxed environment
Jetty Tomcat JBoss Websphere Glassfish ...
16
Servlet Codepublic class testServlet extends HttpServlet {
public testServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = new PrintWriter(response.getWriter());
response.setContentType("text/html");
out.println("<html>Hello World! this is my first Java servlet.</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
17
Java Server Pages Mix HTML and Java Code Oracle/Sun’s answer to ASP and PHP Blocks of static HTML + blocks of dynamically
generated HTML Simplifies the creation of dynamic web
applications You do not need to output lots of messy dynamically
generated HTML code from your Java code Two basic elements HTML markup and JSP
scriptlet code blocks
18
Java Server Pages Java code executed in place: there is no need
to define a main method or other entry point Code runs in sequential order – top to bottom
Can also define methods – more on this later Tomcat / Servlet containers effectively
compile JSP pages into a Servlet Java Code blocks wrapped in <% %> brackets
<HTML> <BODY>
<% java.util.Date date = new java.util.Date(); %>
Hello! The time is now <%= date %>
</BODY> </HTML>
19
JSP -> Servlet<%@ page errorPage="myerror.jsp" %>
<%@ page import="com.foo.bar" %>
<html> <head>
<%! int serverInstanceVariable = 1;%>
<% int localStackBasedVariable = 1; %>
<table> <tr><td>
<%= toStringOrBlank( "expanded inline data " + 1 ) %>
</td></tr>...
20
Generated Code...import …
class _myservlet implements Servlet, HttpJspPage {
int serverInstanceVariable = 1;
… public void _jspService(HttpServletRequest request, HttpServletResponse response ) throws ServletException, java.io.IOException {
ServletConfig config = …; // Get the servlet config Object
page = this;
PageContext pageContext = …; // Get the page context for this request
JspWriter out = pageContext.getOut();
HttpSession session = request.getSession( true );
try {
out.print( "<html>\r\n" );
out.print( "<head>\r\n" );
…
int LocalStackBasedVariable = 1;
…
out.print( "<table>\r\n" );
out.print( " <tr><td>" );
out.print( toStringOrBlank( "expanded inline data " + 1 ));
out.print( " </td></tr>\r\n" );
…
} catch ( Exception _exception ) { ...
} } }
21
JSP Methods Method definition<%! public String sayHello()
{
return "Hello this is a method";
}
%>
Method call<%= sayHello() %>
22
JSP Directives <%@ page import="java.util.*" %> <%@ page
import="java.util.*,java.text.*" %>
<HTML> <BODY> Going to include hello.jsp...<BR> <%@ include file="hello.jsp" %> </BODY> </HTML>
23
Builtin JSP Objects HTTPServletRequest request
Read different key / value pairs Header attributes Cookies ... + lots more
HTTPServletResponse reponse Write different key / value pairs
JspWriter out Default output stream / writer for a JSP page
24
JDBC – Java Database Connectivity A common interface which allows a Java Program to access a SQL /
relational database technology programmatically JDBC classes and factory methods abstract common SQL database
functions such as: Connect / Disconnect Statements
Select Insert Update Delete DDL ( create table etc …)
ResultSets PreparedStatements ResultSetMetaData
Classes and Factory methods live in the java.sql.* package
25
What is JDBC? API written in such a way to provide an abstracted
interface to allow applications written using JDBC to access and interact with many different database products
JDBC uses a Driver based model which allows a Java application to access any database server for which a JDBC driver exists
Makes database / application development easier as programmers do not need to familiarize themselves with the database libraries provided by different vendors. ie. low level API’s provided by the likes of IBM (DB2),
Microsoft (SQL Server) and Oracle. Hides many unnecessary details
26
Oracle Database
Custom Oracle API gives programmatic access to database server
Program once compiled can only talk to an Oracle database
Very fast compared with all the translation that needs to happen with JDBC
SQL Server, Sybase, MySQL etc. all have their define their custom own APIs / protocols
27
DataSource Factory method for defining and obtaining access
to a database Connection object Allows programmer to specify data about a
database connection Vendors often provide their own variations of the
generic Java DataSource Sets up information related to database:
Port Hostname Schema And Database name
28
ExampleSQLServerDataSource ds = new SQLServerDataSource ();
ds.setIntegratedSecurity(false);
ds.setServerName("hyperdisc.unitec.ac.nz");
ds.setPortNumber(1433);
ds.setUser("patelc08");
ds.setPassword(“xxxxxxxx");
ds.setDatabaseName("patelc08sqlserver1");
ds.setInstanceName("dbo");
Connection conn = ds.getConnection();
29
Connection Java wrapper around database session / connection Provides metadata about database connection
SQL dialect (vendor MS, Oracle etc.) Transaction isolation level
Factory methods for creating Statement objects Connection methods need to be explicitly closed in
a finally { //… } block otherwise you may end up needing to restart your database a lot
Factory methods to create prepared statements – which are a pool of pre-compiled SQL statements stored on a database server
30
Statements etc Statement Java wrapper around SQL DML/DDL
Data Manipulation Language1. Insert2. Delete3. Update4. Select Data Definition Language1. Create table2. Drop table3. etc.
31
ResultSets Wrapper around a database’s SQL result set Allows a programmer to scroll forward through
the result set using a database defined cursor Can also scroll backwards and forwards
through the result set and to arbitrary positions depending on the JDBC driver and database technology
Provides meta data about the results such as column name and data type
How many results were returned etc.
32
Readings http://struts.apache.org/primer.html http://www.ibm.com/developerworks/libr
ary/j-struts/