Docsity

Java web programming-Skripta-Java tehnologije-Softversko inzenjerstvo, Rezime od Java programiranje

Delimični pregled teksta

Preuzmite Java web programming-Skripta-Java tehnologije-Softversko inzenjerstvo i više Rezime u PDF od Java programiranje samo na Docsity!

Java Web Programming

n + 1, Inc

Feb 2009

Copyright (c) 2008 n + 1, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back- Cover Texts. A copy of the license is included in the section entitled ”GNU Free Documentation License”.

Page 2 ©c2009 n + 1, Inc All Rights Reserved

CONTENTS

A.5 Lab 6.......................................... 289 A.6 Lab 7.......................................... 296 A.7 Lab 8.......................................... 303 A.8 Lab 9.......................................... 312 B GNU Free Documentation License............................. 321

Page 8 ©c2009 n + 1, Inc All Rights Reserved

LIST OF TABLES

Page 10 ©c2009 n + 1, Inc All Rights Reserved

List of Figures

8.1 Basic MVC Framework.................................... 197 9.1 Intercepting Filters...................................... 224

List of Programs

  • 1 Java Web Programming Introduction
    • 1.1 Introduction
  • 2 HTML and HTTP Protocol
    • 2.1 HTTP Protocol
      • 2.1.1 URLs
      • 2.1.2 HTTP
    • 2.2 HTML
      • 2.2.1 Structure Tags
      • 2.2.2 Simple HTML Formatting Tags
      • 2.2.3 Lists
      • 2.2.4 Tables
      • 2.2.5 Form Tag
    • 2.3 Lab Activity
  • 3 Servlets
    • 3.1 Servlets
      • 3.1.1 How Servlet Container Works
      • 3.1.2 Life Cycle of Servlets
      • 3.1.3 Complete Servlet Example
    • 3.2 Deploying Web Applications
      • 3.2.1 Structure of WAR file CONTENTS
      • 3.2.2 Deployment Descriptor
    • 3.3 Building War File With Ant
      • 3.3.1 Ant Configuration File
      • 3.3.2 Ant Variables
      • 3.3.3 Printing Information To Screen
      • 3.3.4 Compiling Java Files
      • 3.3.5 Packaging a WAR File
      • 3.3.6 Deploying a WAR File
      • 3.3.7 Cleaning Workspace
      • 3.3.8 Example build.xml File
      • 3.3.9 Running Ant Build
    • 3.4 Lab Activity
  • 4 Servlet Request and Response
    • 4.1 Servlet Response
      • 4.1.1 Sending Data To The Client
      • 4.1.2 Error Codes
      • 4.1.3 Response Header
      • 4.1.4 Response Forward
    • 4.2 Servlet Request
      • 4.2.1 HTML Web Form
      • 4.2.2 Processing Web Form Data
      • 4.2.3 Handling GET Data
      • 4.2.4 HTTP Header Data
      • 4.2.5 URL Elements
      • 4.2.6 Servlet Context
      • 4.2.7 Forwards And Includes
    • 4.3 Lab Activity JAVA WEB PROGRAMMING
  • 5 Session Management
    • 5.1 Session Management
      • 5.1.1 HttpSession Object
      • 5.1.2 HttpSession Methods
      • 5.1.3 Guessing Game Example
    • 5.2 Cookies
      • 5.2.1 Creating New Cookie
      • 5.2.2 Retrieving Cookie Values
    • 5.3 Lab Activity
  • 6 JavaServer Pages
    • 6.1 JavaServer Pages
      • 6.1.1 Mechanics of JSP
      • 6.1.2 JSP Basics
      • 6.1.3 Implicit Variables
      • 6.1.4 Include Directive
      • 6.1.5 Page Directive
      • 6.1.6 JSP Error Handling
      • 6.1.7 How JSPs Are Converted To Servlets
    • 6.2 Expression Language
      • 6.2.1 EL Basics
      • 6.2.2 Accessing Data From Complex Variables
      • 6.2.3 Variables as Maps, Lists, and Arrays
      • 6.2.4 Implicit Objects
      • 6.2.5 EL Operators
    • 6.3 Lab Activity
  • 7 Tag Libraries CONTENTS
    • 7.1 Tag Libraries
    • 7.2 JSTL
      • 7.2.1 Including JSTL Libraries
      • 7.2.2 Core Tags
      • 7.2.3 Format Tags
    • 7.3 Custom Tag Libraries
      • 7.3.1 Simple Tag Files
      • 7.3.2 Tag With Body
    • 7.4 Lab Activity
  • 8 Java Web Programming Best Practices
    • 8.1 Best Practices
      • 8.1.1 Goals For Web Framework
      • 8.1.2 MVC Pattern
    • 8.2 Front Controller Servlet Pattern
      • 8.2.1 Creating The Front Controller Servlet
      • 8.2.2 Handling Incoming Requests and Displaying The View
    • 8.3 Application Controller Pattern
      • 8.3.1 Actions
      • 8.3.2 Action Factory
      • 8.3.3 Application Controller Code
      • 8.3.4 Lookup The View
    • 8.4 Concrete Example Using The Framework
      • 8.4.1 Mapping Todo URL
      • 8.4.2 Creating TodoList Action
      • 8.4.3 Mapping Todo View
      • 8.4.4 Todo View
      • 8.4.5 Adding A Task JAVA WEB PROGRAMMING
      • 8.4.6 Creating AddTask
      • 8.4.7 View Lookup
      • 8.4.8 Add Task View
      • 8.4.9 Processing New Tasks
      • 8.4.10 Creating ProcessTask
      • 8.4.11 Final Thoughts
    • 8.5 Lab Activity
  • 9 Best Practices: Intercepting Filters
    • 9.1 Intercepting Filters
    • 9.2 Context Filter
      • 9.2.1 ContextFilter Class
      • 9.2.2 RequestContext
      • 9.2.3 Incoming Filter
      • 9.2.4 Outgoing Filter
      • 9.2.5 Modifying Framework
    • 9.3 Security Filtering
      • 9.3.1 Authentication Filter
      • 9.3.2 Authorization Filter
    • 9.4 Lab Activity
  • Appendices
    • A Lab Results
      • A.1 Lab
      • A.2 Lab
      • A.3 Lab
      • A.4 Lab
  • 2.1 HTTP Request Types
  • 2.2 Common Table Attributes
  • 3.1 Eight Protocol Specific Methods
  • 3.2 WAR File Structure
  • 3.3 Pattern Matches
  • 4.1 Response Content Types
  • 4.2 Result Codes
  • 4.3 Error Code Attributes
  • 5.1 Cookie Methods
  • 6.1 Implicit Variables And Their Corresponding Classes
  • 6.2 Implicit EL Variables
  • 7.1 URIs For JSTL
  • 7.2 URIs For JSTL
  • 7.3 Date / Time Types
  • 7.4 Date Pattern Symbols
  • 1 Example Servlet Part
  • 2 Example Servlet Part
  • 3 Example Output Servlet
  • 4 Example Output Servlet
  • 5 Program To Process A Web Form
  • 6 Display List of Headers
  • 7 Displays Different Segments of URI
  • 8 Servlet A
  • 9 Servlet B
  • 10 Guessing Game Part
  • 11 Guessing Game Part
  • 12 Front Controller Servlet
  • 13 Action Interface
  • 14 Action Factory
  • 15 View Lookup
  • 16 Application Controller
  • 17 Request Context
  • 18 Final Request Context Part
  • 19 Final Request Context Part
  • 20 Context Filter Part
  • 21 Context Filter Part
  • 22 Security Filter

LIST OF PROGRAMS

Page 14 ©c2009 n + 1, Inc All Rights Reserved

CHAPTER 1. JAVA WEB PROGRAMMING INTRODUCTION

1.1 Introduction

Java Web Programming provides an in depth look at web based development using the Java programming environment. The course is broken into three sections. The first provides a quick overview of the HTTP protocol and review of basic HTML. The purpose of this section is to provide an understanding of the foundational elements of basic HTML development. The goal is not to provide intimate details of HTML, but to provide a basic background of HTML elements in order to create simple web pages. The second part of the course focuses on the building blocks for developing a Java web application. The primary concern will be on understanding and using Servlets and Java Server pages to develop web based applications. The class concentrates on the core elements of each technology without delving into every nuance of the interface. The final section takes the the foundational building blocks and combines them to develop a powerful web framework. One that is both robust and flexible. It provides a tour of the important web design patterns and how they are applied to create a workable web framework.

Page 16 ©c2009 n + 1, Inc All Rights Reserved

Chapter 2

HTML and HTTP Protocol

Objectives

  • Understand the basics of the HTTP protocol
  • Learn URL naming conventions
  • Learn basics of how a HTTP Server works
  • Understand core HTML tags

JAVA WEB PROGRAMMING

2.1.1 URLs

Uniform Resource Locators, URLs, are used to describe the location of a networked resource. The URL can be broken into four distinct parts for describing a location. The first part defines the protocol necessary to access the resource. The second part defines the address of the service on the network. This information is generally provided as a DNS name but the direct IP address can be used. The third section is optional and is used to tell what port or application needs to be accessed for the resource. Lastly, the URL provides information about retrieving the resource from the server’s application. The last part is known as the uniform resource identifier or URI. Below is an example URL which incorporates each of these elements.

http://nplus1.net:8080/some/location/myfile.txt

The protocol used to access the resource is the HTTP protocol. The service is located at the DNS name nplus1.net and can be found on port 8080. The resource, myfile.txt, is located at the “/some/location/” path. It is possible to add parameters to the end of the URL that can be used by the server for processing. Parameters are added to the end of the URL by first appending a “?”. Then the parameters are added as a series of name value pairs that are separated by the equal sign. Each name pair is tokenized by using the & character. The series of parameters are known as the query string. Below is an example of a URL with a query string.

http://nplus1.net:8080/servlet/myPage.html?name=foo&val=bar

In this case, there are two sets of name value pairs after the “?”. They are “name = foo” and “val = bar”. Most server side programming languages allow the developer to access these variables. Java is no exception. In fact these are integral to web based programming. It is important to note that the URI defined above does not include the query string parameters.

Page 19 ©c2009 n + 1, Inc All Rights Reserved

CHAPTER 2. HTML AND HTTP PROTOCOL

2.1.2 HTTP

HTTP is the protocol used to transfer resources defined by a URL. While the HTTP protocol is used to facilitate transmission it does not actually transfer the resource. The actual transfer of the file is handled by the network protocol such as TCP/IP. HTTP is a protocol that piggy backs on top of the transfer protocols to send the actual data. Because it uses other transmission protocols there is no way to maintain state between requests. HTTP is at its most basic a simple request/response protocol between a server and a client. The server receives the request and sends a response back to the client.

Page 20 ©c2009 n + 1, Inc All Rights Reserved