Tutorial: Server-Side Web Applications with Java, JSP and Tomcat

clangpotatoSoftware and s/w Development

Oct 28, 2013 (3 years and 7 months ago)

64 views

Tutorial:

Server
-
Side Web Applications with
Java, JSP and Tomcat

Eran Toch

December 2004

Methodologies in Information Systems
Development

2

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Agenda


Introduction and architecture


Installing Tomcat


Building a Web Application (through Eclipse)


The
Phones
Web Application


Writing JavaBeans


Writing JSPs


Includes, session and application


Model 2 Architecture

3

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Web Application vs. Web Site

Web Site

Web Application

4

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

The Difference


cont’d

Web sites are


Static


File based


May include client’s JavaScript code


Web Applications are


Dynamic (output depend on input)


Database based


Multi
-
layered


Session dependent (most of the times)


Personalized (some of the times)

5

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

JSP

Java Web Application Architecture

Application Server

Browser

Servlet

Java Classes

/ EJB

HTTP / HTML

DB / ERP /

Legacy

6

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Agenda


Introduction and architecture


Installing Tomcat


Building a Web Application (through Eclipse)


The
Phones
Web Application


Writing JavaBeans


Writing JSPs


Includes, session and application


Model 2 Architecture

7

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Tomcat Installation


Go to:
http://jakarta.apache.org/tomcat/


Download the latest version (currently
5.5.4
)
by going to: Downloads
-
> Binaries
-
>
Tomcat
5.5.4
.


For Windows, download the
exe
version


with a
Windows setup. The direct link:
http://apache.fresh.co.il/jakarta/tomcat
-
5
/v
5.5.4
/bin/jakarta
-
tomcat
-
5.5.4
.exe



For Linux, read the setup manual:
http://jakarta.apache.org/tomcat/tomcat
-
5.5
-
doc/setup.html

8

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Tomcat and Java
5


Please note that Tomcat v.
5.5.4
requires
J
2
SE
1.5
(also known as J
2
SE
5
) or above.


If you want to use J
2
SE
1.4
, download
Tomcat
5.0
.+

9

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Tomcat Installation


cont’d

If you want Tomcat to
startup every time the
computer is rebooted,
check the “service” option.
If you wish to use Tomcat
for only for development,
it’s best to leave the
service option unchecked.


10

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Tomcat Installation
-

Port

You can configure the port
that Tomcat will be using. If
you want it to be available
publicly (behind firewalls
etc), change the default port
to
80
. Otherwise, leave it as
it is (
880
). You can always
change it later, in the
[TOMCAT_HOME]/conf/serv
er.xml configuration file.


11

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Running Tomcat


Start Tomcat by running Tomcat monitor
(Start Menu
-
> Apache Tomcat
-
> Monitor
Tomcat), and click on Start.


Point your browser to:
http://localhost:
8080
. If
Tomcat works, you should
see something like this


If not, check out the logs
(C:
\
[TOMCAT_HOME]
\
logs
\
s
tdout) and see what went
wrong.

12

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Managing Tomcat


Go to the management console by clicking
on the “management” link in the Tomcat root
homepage, or directly by going to:
http://localhost:
8080
/manager/html
.


The default username is “admin”, and the
default password is ””


You can change it by changing the
[TOMCAT_HOME]
\
conf
\
tomcat
-
users.xml.


13

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Managing Tomcat Console

Web Application
management


Number of
current
sessions


Start, stop, restart
and un
-
deploy
applications

14

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Agenda


Introduction and architecture


Installing Tomcat


Building a Web Application (through Eclipse)


The
Phones
Web Application


Writing JavaBeans


Writing JSPs


Includes, session and application


Model
2
Architecture

15

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Building A Web Application


Build the following folder structure under
[TOMCAT
-
HOME]
\
webapps


Tomcat
-
home

conf

common

webapps

...

phones

WEB
-
INF

lib

classes

jsp

This is the
hard way…

There are
better

16

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Creating JSP Project in Eclipse

1.
Open a project
for the JSP
pages

2.
Choose the type
“Simple project”

17

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Creating JSP Project


cont’d


Choose the path
to the jsp folder


Click “Finish”

18

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Creating Java Classes Project


Create a new
project


Choose “Java
Project”

19

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Creating Java Project


cont’d


Choose the path
to the WEB
-
INF/classes
folder

20

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Creating Java Project


cont’d


Make sure the
output build path
is in the WEB
-
INF
classes folder


Necessary
libraries could be
added now


21

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Better ways


Download MyEclipse IDE extension from:

http://www.myeclipseide.com/


Download Lomboz plugin for Eclipse at:
http://www.objectlearn.com/index.jsp


Both of these plugins will give you:


Easier deployment of projects


JSP debugging


JSP error detection (important!)

22

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Agenda


Introduction and architecture


Installing Tomcat


Building a Web Application (through Eclipse)


The
Phones
Web Application


Writing JavaBeans


Writing JSPs


Includes, session and application


Model
2
Architecture

23

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Our “Phones” Application


Simple application that displays phone
records from the database


(stupid, shameless)

Architecture:

select_entry.jsp

display_phone.jsp

PhoneEntry.java

DBConnector.java

Database

24

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

PhoneEntry.java

package edu.technion.methodologies.phones
;



public class PhoneEntry

{


private int id =
0
;


private String name
;


private

String phone
;


private

String email
;




public void load(int id
)

{



DBConnector.loadPhone(id, this
);


}


public void save
()

{



if (id ==
0
)

{




id = DBConnector.insertPhone(this
);



}



else

{




DBConnector.updatePhone(id, this
);



}


}


//getters and setters…

}

25

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

DBConnector.java


See the advanced Java tutorial…

26

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Writing Scriplets in JSP


Write HTML as normal


Dynamic parts between <% %>


Eg


<HTML>


<BODY> Hello!


The time is now <%= new java.util.Date() %>


</BODY>


</HTML>


27

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

JSP Scripting Elements


Expressions

<%=

expression

%>



evaluated and inserted into the output


Scriptlets

<%

code

%>



inserted into the servlet's
service

method


Declarations

<%!

code

%>


inserted into the body of the servlet class, outside of
any existing methods


28

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

JSP Directives


There are two main types of directive:


page



lets you do things like import classes, customize the
servlet superclass etc


Include


lets you insert a file into the servlet class at the time the
JSP file is translated into a servlet.




<%@

directive attribute="value"
%>



29

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

select_entry.jsp

<!DOCTYPE HTML PUBLIC "
-
//W
3
C//DTD HTML
4.0
Transitional//EN">

<HTML>

<HEAD>

<TITLE>
Phones Application
</TITLE>

</HEAD>


<BODY>

<form
action
=
"display_phone.jsp"

method
=
"post"
>

<table
border
=
0
>

<tr>

<td>
ID:
</td>

<td><input
type
=
"text"

name
=
"id"
></td>

</tr>

<tr>

<td>
&nbsp;
</td>

<td><input
type
=
"submit"
></td>

</tr>

</table>


</form>

</BODY>

</HTML>

HTML
Header

HTML form

Nested HTML
table structure

HTML text input

30

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

select_entry.jsp


the HTML output

31

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

display_phone.jsp

<!DOCTYPE HTML PUBLIC "
-
//W
3
C//DTD HTML
4.0
Transitional//EN">

<%@
page

import
=
"edu.technion.methodologies.phones.*"

%>

<HTML>

<HEAD>

<TITLE>
Phone Entry
</TITLE>

</HEAD>


<BODY>

<%

String idParam = request.getParameter(
"id"
);

int

id =
0
;

if

(idParam !=
null
){

id = Integer.parseInt(idParam);

}

PhoneEntry phoneEntry =
new

PhoneEntry();

phoneEntry.load(id);

%>

Handling the
request

Creating the
JavaBean

Importing
libraries

32

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

display_phone.jsp


cont’d

<table
border
=
0
>

<tr>

<td>
Name:
</td>

<td><b>
<%= phoneEntry.getName() %>
</b></td>

</tr>

<tr>

<td>
Phone:
</td>

<td>
<%= phoneEntry.getPhone() %>
</td>

</tr>

<tr>

<td>
Email:
</td>

<td>
<%= phoneEntry.getEmail() %>
</td>

</tr>

</table>


</BODY>

</HTML>

33

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

display_phone.jsp


the HTML Output

34

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Agenda


Introduction and architecture


Installing Tomcat


Building a Web Application (through Eclipse)


The
Phones
Web Application


Writing JavaBeans


Writing JSPs


Includes, session and application


Model
2
Architecture

35

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

JSP Include


Inserting elements into the JSP page in
complication time

<table
border
=
"
0
"

width
=
"
100
%"

cellpadding
=
"
4
"
>

<tr
bgcolor
=
"
99
CCFF"
>

<td><a
href
=
"select_entry.jsp"
>
home
</a></td>

</tr>

</table>

Included JSP file

36

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

JSP Include


cont’d



<BODY>

<%@
include

file
=
"header.jsp"
%>

<%



We add the include declarative into display_phone.jsp

And we get:

37

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Session and Application Objects


A session is an object associated with a visitor.




Data can be put in the session and retrieved from it







Sessions are timed
-
out


the default is ~
25
minutes


The application object is accessed similarly, but is
unique and global for the web application

//Setting an object within the session

session.setAttribute(
"last_searched_id"
,
new

Integer(id));


//Getting an object from the session

Integer lastID = (Integer)session.getAttribute(
"last_searched_id"
);

out.println(lastID);

38

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Advanced Issues


Servlets


have many advantages over JSP


Inheritance


Class structure


Tag libraries


Enterprise Java Beans


J
2
EE Frameworks (WebLogic, Web Sphere,
JBoss)


39

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Agenda


Introduction and architecture


Installing Tomcat


Building a Web Application (through Eclipse)


The
Phones
Web Application


Writing JavaBeans


Writing JSPs


Includes, session and application


Model
2
Architecture

40

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Model
2
Architecture


recommended approach for designing
medium
-

and large
-
sized Web applications


An implementation of the
Model
-
View
-
Controller (MVC) pattern


Applications have
3
layers


Model
:
containing all data and operations

»
JSP classes or beans



View
:
creating various presentations

»
JSP


focus on static data


Controller
:
receiving requests, updating the model,
and delegating to views

»
servlet

41

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Model
2
Architecture


cont’d

.jsp

.jsp

Database

classes

servlet

VIEW

CONTROLLER

MODEL

42

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

Struts


Struts is an open source framework created
within the Jakarta project by the Apache
Software Foundation


Supports Model
-
View
-
Controller (MVC)
design pattern


http://struts.apache.org/


43

Server
-
Side Web Applications with Java, JSP and Tomcat


Eran Toch

Methodologies in Information System Development

References


Core Servlets and JavaServer Pages, Vol.
1
: Core
Technologies, Second Edition, by Marty Hall, Larry
Brown


Sun’s JSP Tutorial:

http://java.sun.com/j
2
ee/tutorial/
1
_
3
-
fcs/doc/JSPIntro.html


JavaBeans Tutorial:

http://java.sun.com/j
2
ee/tutorial/
1
_
3
-
fcs/doc/JSPBeans.html


A set of advanced (and untidy) tutorials:

http://www.coreservlets.com/