tutorial: server-side web applications with java, jsp and tomcat

43
Tutorial: Tutorial: Server-Side Web Applications Server-Side Web Applications with Java, JSP and Tomcat with Java, JSP and Tomcat Eran Toch December 2004 Methodologies in Information Systems Development

Upload: babu

Post on 11-Feb-2016

52 views

Category:

Documents


1 download

DESCRIPTION

Methodologies in Information Systems Development. Tutorial: Server-Side Web Applications with Java, JSP and Tomcat. Eran Toch December 2004. Agenda. Introduction and architecture Installing Tomcat Building a Web Application (through Eclipse) The Phones Web Application - PowerPoint PPT Presentation

TRANSCRIPT

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

Tutorial: Tutorial: Server-Side Web Applications with Server-Side Web Applications with

Java, JSP and TomcatJava, JSP and Tomcat

Eran TochDecember 2004

Methodologies in Information Systems Development

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

2

AgendaAgenda• 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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

3

Web Application vs. Web SiteWeb Application vs. Web Site

Mainframe

Data

WebService

Web Site Web Application

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

4

The Difference – cont’dThe 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)

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

5

JSP

Java Web Application ArchitectureJava Web Application Architecture

Application Server

Browser

Servlet

Java Classes/ EJB

HTTP / HTML

DB / ERP / Legacy

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

6

AgendaAgenda• 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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

7

Tomcat InstallationTomcat 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/v5.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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

8

Tomcat and Java 5Tomcat and Java 5

• Please note that Tomcat v. 5.5.4 requires J2SE 1.5 (also known as J2SE 5) or above.

• If you want to use J2SE 1.4, download Tomcat 5.0.+

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

9

Tomcat Installation – cont’dTomcat 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.

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

10

Tomcat Installation - PortTomcat 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/server.xml configuration file.

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

11

Running TomcatRunning 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\stdout) and see what went wrong.

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

12

Managing TomcatManaging 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.

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

13

Managing Tomcat ConsoleManaging Tomcat Console

Web Application management

Number of current sessions

Start, stop, restart and un-deploy applications

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

14

AgendaAgenda• 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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

15

Building A Web ApplicationBuilding A Web Application

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

Tomcat-home

confcommonwebapps ...

phones

WEB-INF

lib

classes

jsp

This is the hard way…There are

better

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

16

Creating JSP Project in EclipseCreating JSP Project in Eclipse

1. Open a project for the JSP pages

2. Choose the type “Simple project”

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

17

Creating JSP Project – cont’dCreating JSP Project – cont’d

• Choose the path to the jsp folder

• Click “Finish”

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

18

Creating Java Classes ProjectCreating Java Classes Project

• Create a new project

• Choose “Java Project”

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

19

Creating Java Project – cont’dCreating Java Project – cont’d

• Choose the path to the WEB-INF/classes folder

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

20

Creating Java Project – cont’dCreating Java Project – cont’d

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

• Necessary libraries could be added now

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

21

Better waysBetter 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!)

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

22

AgendaAgenda• 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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

23

Our “Phones” ApplicationOur “Phones” Application

• Simple application that displays phone records from the database

• (stupid, shameless) Architecture:

select_entry.jsp display_phone.jsp

PhoneEntry.java

DBConnector.javaDatabase

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

24

PhoneEntry.javaPhoneEntry.javapackage 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…

}

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

25

DBConnector.javaDBConnector.java

• See the advanced Java tutorial…

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

26

Writing Scriplets in JSPWriting Scriplets in JSP

• Write HTML as normal• Dynamic parts between <% %> • Eg

<HTML> <BODY> Hello!  The time is now <%= new java.util.Date() %> </BODY>

</HTML>

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

27

JSP Scripting ElementsJSP 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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

28

JSP DirectivesJSP 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" %>

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

29

select_entry.jspselect_entry.jsp<!DOCTYPE HTML PUBLIC "-//W3C//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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

30

select_entry.jsp – the HTML outputselect_entry.jsp – the HTML output

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

31

display_phone.jspdisplay_phone.jsp<!DOCTYPE HTML PUBLIC "-//W3C//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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

32

display_phone.jsp – cont’ddisplay_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>

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

33

display_phone.jsp – the HTML Outputdisplay_phone.jsp – the HTML Output

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

34

AgendaAgenda• 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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

35

JSP IncludeJSP Include

• Inserting elements into the JSP page in complication time

<table border="0" width="100%" cellpadding="4"><tr bgcolor="99CCFF"><td><a href="select_entry.jsp">home</a></td></tr></table>

Included JSP file

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

36

JSP Include – cont’dJSP Include – cont’d

…<BODY><%@include file="header.jsp"%><%…

We add the include declarative into display_phone.jsp

And we get:

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

37

Session and Application ObjectsSession 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 sessionsession.setAttribute("last_searched_id", new Integer(id));

//Getting an object from the sessionInteger lastID = (Integer)session.getAttribute("last_searched_id");out.println(lastID);

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

38

Advanced IssuesAdvanced Issues

• Servlets – have many advantages over JSP– Inheritance– Class structure

• Tag libraries• Enterprise Java Beans• J2EE Frameworks (WebLogic, Web Sphere,

JBoss)

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

39

AgendaAgenda• 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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

40

Model 2 ArchitectureModel 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

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

41

Model 2 Architecture – cont’dModel 2 Architecture – cont’d

.jsp

.jsp Form re

quest

forward

Databaseclassesservlet

VIEW CONTROLLER MODEL

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

42

StrutsStruts

• 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/

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

Server-Side Web Applications with Java, JSP and Tomcat – Eran TochMethodologies in Information System Development

43

ReferencesReferences

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

• Sun’s JSP Tutorial:http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro.html

• JavaBeans Tutorial:http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPBeans.html

• A set of advanced (and untidy) tutorials:http://www.coreservlets.com/