Java Web Programming with Eclipse - CSUSB CNS

[Pages:293]Java Web Programming with Eclipse

David Turner, Ph.D. Department of Computer Science and Engineering California State University San Bernardino Jinsok Chae, Ph.D. Department of Computer Science and Engineering University of Incheon

Copyright c 2009 by David Turner and Jinseok Chae

Version December 1, 2009

Preface

The purpose of the book is to introduce students to web application development in Java with the use of Eclipse. The book assumes a familiarity with HTML and the Java programming language.

The book is in cookbook format in that it provides instructions on how to construct solutions to various problems. The intent is to show students how to accomplish typical Web development tasks in the Java language. In later chapters of the book, detailed instructions are omitted if they duplicate instructions in an earlier chapter. This assumes the student can recall or refer back to the previous instructions. Unguided exercises are also provided so that students can apply what they have covered in the main text. The book omits many details and explanations. For these, the reader will need to consult online documentation or search the Web for other tutorials and articles. Each chapter contains a list of references that the reader may find useful for filling in these missing details.

This is an active book in the sense that the reader is expected to carry out the procedures described. The code examples provided in each chapter are not self-contained; they need to be developed by progressing sequentially through the chapters.

1

2

December 1, 2009

Acknowledgements

The following people have helped to create this book. ? Eyob Zellke ? Patrick O'Conner ? The students of Winter quarter 2008 of Server Programming at CSUSB

3

4

December 1, 2009

Contents

1 Introduction to Java Web Application Development

11

1.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.3 Computer Languages Used for Web Application Development 12

1.4 The Servlet API . . . . . . . . . . . . . . . . . . . . . . . . . 13

1.5 Java 2 Enterprise Edition (J2EE) . . . . . . . . . . . . . . . . 13

1.6 Java Server Pages (JSP) . . . . . . . . . . . . . . . . . . . . . 14

1.7 Rich Site Summary (RSS) . . . . . . . . . . . . . . . . . . . . 14

1.8 Representational State Transfer (REST) . . . . . . . . . . . . 15

1.9 Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.10 Integrated Development Environments . . . . . . . . . . . . . 15

1.11 Ant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

1.12 Web Application Architecture . . . . . . . . . . . . . . . . . . 16

1.13 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

1.14 Web Application Servers . . . . . . . . . . . . . . . . . . . . . 18

1.15 Database Servers . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.16 Development versus Deployment Environments . . . . . . . . 18

2 Java Setup

21

2.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.3 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

3 Eclipse Setup

23

3.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.3 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

3.4 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

5

6

3.5 Configure File Types . . . . . . . . . . . . . . . . . . . . . . . 24 3.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

4 The Apache Tomcat Web Container

27

4.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

4.2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

4.3 Install Tomcat . . . . . . . . . . . . . . . . . . . . . . . . . . 28

4.4 Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

4.5 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

4.6 Configure Firewall . . . . . . . . . . . . . . . . . . . . . . . . 30

4.7 Manager Application . . . . . . . . . . . . . . . . . . . . . . . 31

4.8 Tomcat Documentation . . . . . . . . . . . . . . . . . . . . . 31

4.9 Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

4.10 Understanding Tomcat Class Loading . . . . . . . . . . . . . 35

4.11 Deep Restart of Tomcat . . . . . . . . . . . . . . . . . . . . . 36

4.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

5 Java Servlets

39

5.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

5.2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

5.3 Project Creation . . . . . . . . . . . . . . . . . . . . . . . . . 40

5.4 Attaching Source Code to Jar Files . . . . . . . . . . . . . . . 41

5.5 Deployment Descriptor . . . . . . . . . . . . . . . . . . . . . . 45

5.6 Create Home Servlet . . . . . . . . . . . . . . . . . . . . . . . 47

5.7 Web Application Deployment . . . . . . . . . . . . . . . . . . 54

5.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

6 Web Application Logging

57

6.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

6.2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

6.3 Add the Log4j Library to the Project . . . . . . . . . . . . . 58

6.4 The Log4j Configuration File . . . . . . . . . . . . . . . . . . 59

6.5 The Eclipse Build Process . . . . . . . . . . . . . . . . . . . . 60

6.6 Modify HomeServlet . . . . . . . . . . . . . . . . . . . . . . . 60

6.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

7 Java Server Pages

67

7.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

7.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

7.3 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

December 1, 2009

7

7.4 Create a JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 7.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

8 A Simple News Feed Application

79

8.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

8.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

8.3 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

8.4 Flow of information for RSS . . . . . . . . . . . . . . . . . . . 80

8.5 Install Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . 81

8.6 Modify the JSP . . . . . . . . . . . . . . . . . . . . . . . . . . 84

8.7 Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

8.8 Create Publisher Project . . . . . . . . . . . . . . . . . . . . . 86

8.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

9 The MySQL Database Server

89

9.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

9.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

9.3 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

9.4 Install MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . 90

9.5 Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

9.6 MySQL-Specific Commands . . . . . . . . . . . . . . . . . . . 92

9.7 Basic SQL Commands . . . . . . . . . . . . . . . . . . . . . . 94

9.8 Create a Database of News Items . . . . . . . . . . . . . . . . 97

9.9 Create Ant Build File . . . . . . . . . . . . . . . . . . . . . . 100

9.10 Run Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

9.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

10 Database-Driven Web Applications

105

10.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

10.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

10.3 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

10.4 JDBC Driver Installation . . . . . . . . . . . . . . . . . . . . 106

10.5 Setup Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . 107

10.6 Create a Dynamic News Feed . . . . . . . . . . . . . . . . . . 108

10.7 Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

10.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

11 Database Connection Pooling

115

11.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

11.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

December 1, 2009

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download