Original PDF Flash format introduction  


Introduction

Introduction
Literature:
Steelman & Murach,
This course “Software Architecture with Java” will dis-
Murach’s Java Servlets and JSP.
cuss the following topics:
Mike Murach & Associates Inc, 2003
Java servlets
Software:
Java Server Pages (JSP’s)
J2SE, Java Standard Edition, version 1.4.x
Java Beans
Tomcat, www.apache.org, we use version 4.1.18
JDBC, connections to RDBMS and SQL
MySQL, www.mysql.org
XML and XML translations
All of this is freeware and can be downloaded, (or
installed from the CDROM that comes with the book)
These tools will then be used to construct webapplica-
tions according to different models.
Introduction
15 January 2004
1
Introduction
15 January 2004
2

What is a web application?
A browser that requests dynamic pages from a server.
These can be generated by PHP or some similar system.
That is a set of web pages that are generated in response
This kind of system is called a “thin client” since the
to a user request.
processing is done in the web server.
Examples:
request
A browser that requests html-pages from a server. In the
simplest case, these pages are static.
response
Browser
Server
PHP
request
A browser that requests dynamic pages from a server. The
server somehow forwards these requests to a web-con-
response
tainer that generates and returns html-pages.
Browser
Server
A browser that executes one or more Java applets. This is
all done inside the browser (the client), therefore this is
RDBMS
known as a “fat client”.
Browser
Server
Webappcontainer
We are to discuss the last kind of systems.
browser
javaplugin
Introduction
15 January 2004
3
Introduction
15 January 2004
4

Why do we need a special webcontainer?
What is a servlet?
An application may result in several request/response
A servlet is a Java class that is derived from the
pairs. Since HTML is stateless (has no memory), we need
HTTPServlet class.
something that can create resources, remember and iden-
tify connections and keep resources alive.
It is executed on behalf of the webserver when requested
from a user. The servlet receives the request, and produces
An example of a webcontainer is Jakarta Tomcat. It is not
html code that is returned via the response mechanism.
a complete J2EE container, it can therefore be called a
Since the servlet is a program it can generate dynamic
servlet container.
html code based on different condititions.
There is a standard framework definition for Javabased
Since this is pure Java it is transportable and follows an
webcontainers. This is known as J2EE, The Java2 Enter-
official standard. It is multithreaded and only one instansi-
prise Edition. Servlets and JSP’s are part of the J2EE
ation is needed.
specification. We will use version 2.3 of the Servlet spec.
and 1.2 of the JSP specification.
Tomcat implements part of the J2EE specification but not
all of it.
Introduction
15 January 2004
5
Introduction
15 January 2004
6

A simple servlet can look like
This will receive a request and generate an HTML header-
line as response to the browser.
package serv;
To compile this you need some jar-files that are not part of
import javax.servlet.*;
the J2SE, but they are included in tomcat and in J2EE
import javax.servlet.http.*;
implementations.
import java.io.*;
To run it you need Tomcat or some other container. Tom-
public class Serv extends HttpServlet {
cat can execute the servlet directly on reqeust from the
browser, but the normal use of a servlet is as part of a
public void doGet(HttpServletRequest request,
deployed application.
HttpServletResponse response)
throws IOException, ServletException {

A deployed application is a complete system with differ-
response.setContentType(“text/html”);
ent components that are kept together with a description
PrintWriter out = response.getWriter();
file.
out.println(“<H1> hello </H1>”);
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}

Introduction
15 January 2004
7
Introduction
15 January 2004
8

What is a JSP?
An example of a JSP is:
Java Server Pages consists of HTML-code, Java code
<HTML>
embedded in a script language (NOT Javascript!) and
<!-- Copyright (c) 1999 The Apache Software
other JSP tags.
Foundation. All rights reserved.-->
The purpose of a JSP is to generate HTML code.
<HEAD><TITLE>
Calendar: A JSP APPLICATION

Technically a JSP is compiled into a servlet by the web-
</TITLE></HEAD>
container.
<BODY BGCOLOR=”white”>
Since it is Java it is transportable, but you do not need to
<jsp:useBean id=”table” scope=”session”
compile it, therefore it is easier to work with.
class=”cal.T ableBean” />
JSP is extentable, i. e. you can introduce new tags that
<%
extends the functionality of JSP.
String time = request.getParameter (“time”);
%>
<FONT SIZE=5> Please add the following event:
<BR> <h3> Date <%= table.getDate() %>
<BR> Time <%= time %> </h3>
</FONT>
<FORM METHOD=POST ACTION=cal1.jsp>
<BR>
<BR> <INPUT NAME=”date” TYPE=HIDDEN
VALUE=”curr ent”>
<BR> <INPUT NAME=”time” TYPE=HIDDEN
VALUE=<%= time %>
<BR> <h2> Description of the event <INPUT
NAME=”description” TYPE=TEXT SIZE=20>
</h2>
<BR> <INPUT TYPE=SUBMIT VALUE=”submit”>
</FORM></BODY></HTML>

Introduction
15 January 2004
9
Introduction
15 January 2004
10

What is a Bean?
What is JDBC, RDBMS and SQL?
A Bean is an ordinary Java class that adheres to the Bean
JDBC (Java DataBase Connectivity) is a serverbased,
rules.
pure Java piece of software that allows Java classes to
connect to a database and to perform queries and updates
Beans can be used for anything, but in a webapplication
via SQL-statements. Each RDBMS (Relation Database
they are often used to perform datahandling.
managing System) requires a vendor specific JDBC
driver.
An example:
SQL (Standard Query Language) is a language that
package test;
allows you to maintain your database. You can store and
public class TestBean {
retrieve data.
String name;
Integer age;

public TestBean() { }
public String getName() {
return name;
}

public void setName(String newName) {
name = newName;
}

public Integer getAge() {
return age;
}

public void setAge(Integer newAge) {
age = newAge;
}}

Introduction
15 January 2004
11
Introduction
15 January 2004
12

What is XML?
An example:
XML (Extensible Markup Language) is a markup lan-
<?xml version=”1.0” encoding=”ISO8859-1” ?>
guage that is mainly used to store structured data. It is
<?xml-stylesheet type=”te xt/xsl”
somewhat similar to HTML but there is no predefined
href=”employees.xsl”?>
tags. You can use any tags, but you must yourself assign
meaning to the tags.
<employees>
<employee empid=”1”>

The benefit of XML is that it can be translated into other
<name>Fredrik Ålund</name>
formats, i. e. HTML, PDF etc. Therefore it can be used as
<department depid=”3”>Services</department>
a general, transportable format.
</employee>
XML can be translated into HTML by XSLT, Extensible
<employee empid=”2”>
Style Language Translations.
<name>Helena Larsson</name>
<department depid=”3”> Services </department>
</employee>
</employees>

Case sensitive, more strict than HTML. You must use
closing tags for ALL elements.
Introduction
15 January 2004
13
Introduction
15 January 2004
14

How do you combine these into an application?
Beans are good to obtain and maintain data. They can
connect to databases to get/store data. Since they are pure
One way to organize this is the MVC model. This means
Java you have the full power of the language with all
Model, View and Control.
datatypes.
Model is the datahandling and business logic
JSP’s are used to produce HTML. In addition you can
embed JAVA in a JSP. However this makes them more dif-
View is the presentation, i. e. the HTML pages
ficult to read and to maintain because you mix a number
Control is the scheduler, the one that distributes the work.
of languages. Therefore they are most often used to pro-
duce presentation, i. e. HTML, of data produced by some
XML-files are used to describe the application.
beans.
Servlets are used as control. They receive the requests
from the client, distributes the work to JSP’s and beans
and return the response. Usually you avoid generating
HTML in the servlet because too much of that makes the
servlet cluttered with strange HTML-strings hard to main-
tain.
Introduction
15 January 2004
15
Introduction
15 January 2004
16

We have no special development tools available here but
there are such tools:
Browser
Full J2EE tools
Oracle JDeveloper. Complete environment, arbitrary data-
base. Free for personal use. High quality. Se www.ora-
cle.com
Servlets
Borland Jbuilder Enterprise. A 30-day trial version can be
downloaded from www.borland.com
Eclips, Open Source but need plugins. www.eclipse.org
JSP’s
Just JSP and Servlets:
Sun Java Studio standard, www.sun.com
Beans
NetBeans, www.netbeans.org
XML-file
RDBMS
textfile
Introduction
15 January 2004
17
Introduction
15 January 2004
18