16
KopyKitab JSP and Servlets Mahesh P. Matha A Comprehensive Study Database jdbc Request Response Result Bean Servlet JSP Class

JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

  • Upload
    others

  • View
    30

  • Download
    1

Embed Size (px)

Citation preview

Page 1: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

JSP and

Servlets

Mahesh P. Matha

A Comprehensive Study

Database

jdbcRequest

Response

Result Bean

Servlet

JSP

Class

Page 2: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

JSP and ServletsA Comprehensive Study

Mahesh P. MathaAssistant Professor

Department of Computer Science (Post-Graduate section)Parvatibai Chowgule College of Arts and Science

Margao, Goa

Delhi-1100922013

Page 3: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

JSP and ServletS: a Comprehensive StudyMahesh P. Matha

© 2013 by PHI Learning Private Limited, Delhi. All rights reserved. No part of this book may be reproduced in any form, by mimeograph or any other means, without permission in writing from the publisher.

ISBn-978-81-203-4745-8

The export rights of this book are vested solely with the publisher.

Published by Asoke K. Ghosh, PHI Learning Private Limited, Rimjhim House, 111, Patparganj Industrial Estate, Delhi-110092 and Printed by Mohan Makhijani at Rekha Printers Private Limited, New Delhi-110020.

Page 4: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itabTo

My nephews and nieces

Page 5: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

Page 6: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itabPreface xiii

Acknowledgements xvii

1. HTTPandJSP-ServleTTecHnology 1–8 Chapter at a Glance 1 1.1 Introduction to HTTP 1 1.2 HTTP Request Structure 2 1.2.1 Request Line 2 1.2.2 0 or More Headers 2 1.2.3 The Empty Line 2 1.2.4 The Message Body 2 1.3 HTTP Response Structure 3 1.3.1 Status-Line 3 1.3.2 0 or More Headers 3 1.3.3 The Empty Line 3 1.3.4 Message Body 3 1.4 HTML, Client-side and Server-side Scripting 3 1.4.1 HTML 3 1.4.2 Client-side Scripting 4 1.4.3 Server-side Scripting 4 1.5 JSP-Servlet Technology 5 1.6 Installing, Configuring and Testing the Java Development Kit (JDK) 5 1.7 Installing, Configuring and Testing the Apache Tomcat Server 6 Summary 7 Review Questions 8

v

contents

Page 7: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

vi | Contents

2. JSPandServleTS:anoverview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development 10 2.4 Introduction to Java Servlets 14 2.5 Servlet Hello World Program Development 15 Summary 17 Review Questions 18

3. comPonenTSofaJSP 19–75 Chapter at a Glance 19 3.1 Introduction 19 3.2 Expressions 19 3.3 Scriptlets and Comments 20 3.4 Declarations 23 3.5 Directives 25 3.5.1 The page Directive 25 3.5.2 The include Directive 33 3.5.3 The taglib Directive 35 3.6 JSP Standard Actions 36 3.6.1 The <jsp:useBean> Action 36 3.6.2 The <jsp:forward> Action 52 3.6.3 The <jsp:include> Action 56 3.6.4 The <jsp:param> Action 60 3.6.5 The <jsp:plugin>, <jsp:params> and <jsp:fallback> Actions tags 63 3.7 Escaping Characters 67 3.8 Internal Handling of JSP Components 68 Summary 72 Review Questions 74

4. imPliciTobJecTSofJSP 76–113 Chapter at a Glance 76 4.1 Introduction 76 4.2 Implicit Objects 76 4.3 The request object 77 4.4 The response object 87 4.5 The session object 91 4.6 The application object 95 4.7 The out object 101 4.8 The exception object 103 4.9 The config object 106

Page 8: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

Contents | vii

4.10 The page object 109 4.11 The pageContext object 109 Summary 112 Review Questions 112

5. ServleTSandSeSSionTrackingmecHaniSmS 114–144 Chapter at a Glance 114 5.1 Introduction 114 5.2 javax.servlet Package 114 5.2.1 Servlet Interface 115 5.2.2 The ServletConfig Interface 115 5.2.3 The GenericServlet Class 115 5.3 javax.servlet.http Package 116 5.3.1 HttpServlet Class 116 5.4 Session Tracking 121 5.5 Cookies 122 5.6 URL Rewriting 129 5.7 Hidden Fields 134 5.8 Session Tracking with Cookies 137 5.9 Session Tracking with URL Encoding 138 Summary 143 Review Questions 144

6. aPPlicaTionevenTliSTenerS 145–171 Chapter at a Glance 145 6.1 Introduction 145 6.2 ServletContextListener Interface 146 6.3 ServletContextAttributeListener Interface 149 6.4 HttpSessionListener Interface 153 6.5 HttpSessionAttributeListener Interface 155 6.6 HttpSessionBindingListener Interface 160 6.7 HttpSessionActivationListener Interface 163 6.8 ServletRequestListener Interface 164 6.9 ServletRequestAttributeListener Interface 165 Summary 170 Review Questions 170

7. filTerS 172–194 Chapter at a Glance 172 7.1 Introduction to Filters 172 7.1.1 What is a Filter? 172 7.1.2 Life Cycle of a Filter 173 7.1.3 Uses of a Filter 174 7.1.4 Benefits of Using a Filter 174

Page 9: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

viii | Contents

7.2 Filter, FilterChain and FilterConfig Interfaces 174 7.3 Configuring Filters 180 7.4 Chaining Filters 185 7.5 Wrapping Requests and Responses 186 Summary 193 Review Questions 193

8. THeexPreSSionlanguage 195–213 Chapter at a Glance 195 8.1 Introduction to the Expression Language (EL) 195 8.2 EL Expressions and Literals 196 8.3 Extracting Property Values 198 8.4 EL Implicit Objects and Variables 201 8.5 EL Operators 205 8.5.1 Arithmetic Operators 205 8.5.2 Relational Operators 205 8.5.3 Logical Operators 206 8.5.4 Conditional Operator 206 8.5.5 Empty Operator 206 8.5.6 Precedence of Operators 206 8.6 Reserved Words in EL 209 8.7 Functions in EL 209 Summary 212 Review Questions 213

9. THeJSTlcoreTagSandfuncTionS 214–247 Chapter at a Glance 214 9.1 Introduction to JSTL 214 9.2 <c:out> Tag 215 9.3 <c:set> Tag 217 9.4 <c:remove> Tag 221 9.5 <c:catch> Tag 225 9.6 <c:if> Tag 226 9.7 <c:choose>, <c:when> and <c:otherwise> Tags 228 9.8 <c:forEach> Tag 231 9.9 <c:forTokens> Tag 234 9.10 <c:param> Tag 236 9.11 <c:import> Tag 236 9.12 <c:url> Tag 238 9.13 <c:redirect> Tag 240 9.14 JSTL Functions 242 Summary 245 Review Questions 245

Page 10: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

Contents | ix

10. JSP,xmlandTHeJSTlxmlTagS 248–281 Chapter at a Glance 248 10.1 Introduction to XML 248 10.2 Document Object Model (DOM) 250 10.3 Parsing XML Using DOM Parser 250 10.4 Simple API for XML (SAX) 255 10.5 Parsing XML Using SAX Parser 258 10.6 XSL Transformation with XSLT 262 10.6.1 Fundamentals of XSL and XSLT 262 10.6.2 Classes and Interfaces for Transformation 264 10.7 JSTL XML Tags 267 10.7.1 The <x:parse> and <x:out> Tags 267 10.7.2 The <x:forEach> and <x:set> Tags 270 10.7.3 The <x:if> Tag 271 10.7.4 The <x:choose>, <x:when> and <x:otherwise> Tags 273 10.7.5 The <x:transform> and <x:param> Tags 276 Summary 278 Review Questions 280

11. JSP,daTabaSeSandTHeJSTlSQlTagS 282–328 Chapter at a Glance 282 11.1 Installing and Configuring the MySQL DBMS 282 11.2 A Review of SQL Statements in MySQL 284 11.2.1 DDL Statements 284 11.2.2 DML Statements 285 11.3 Linking MySQL with JSP 287 11.4 Inserting a Record 289 11.5 Updating an Existing Record 292 11.6 Deleting a Record 300 11.7 The JSTL SQL Tags 304 11.7.1 The <sql:setDataSource> Tag 304 11.7.2 The <sql:update> Tag 305 11.7.3 The <sql:param> Tag 306 11.7.4 The <sql:query> Tag 309 11.7.5 The <sql:transaction> Tag 318 11.7.6 The <sql:dateParam> Tag 323 Summary 326 Review Questions 327

12. THeJSTlformaTTingTagS 329–386 Chapter at a Glance 329 12.1 Introduction 329 12.2 Locales, ResourceBundles and TimeZones 329

Page 11: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

x | Contents

12.3 Internationalization and Localization 330 12.3.1 Why is Internationalization and Localization Needed? 331 12.3.2 How to Achieve Internationalization? 331 12.4 <fmt:setLocale> Tag 331 12.5 <fmt:bundle> Tag 331 12.6 <fmt:message> Tag 332 12.7 <fmt:setBundle> Tag 338 12.8 <fmt:param> Tag 340 12.9 <fmt:requestEncoding> Tag 347 12.10 <fmt:timeZone> Tag 351 12.11 <fmt:setTimeZone> Tag 354 12.12 <fmt:formatNumber> Tag 355 12.13 <fmt:parseNumber> Tag 365 12.14 <fmt:formatDate> Tag 370 12.15 <fmt:parseDate> Tag 376 Summary 381 Review Questions 382

13. aninTroducTionTocuSTomTagS 387–413 Chapter at a Glance 387 13.1 Custom Tags 387 13.2 Developing a Custom Tag without any Body 388 13.3 Developing a Custom Tag with Attributes 392 13.4 Developing a Custom Tag with Body 396 13.5 Developing a Custom Tag that Evaluates its Body Repeatedly 397 13.6 Developing a Custom Tag that Modifies its Body 400 13.7 Co-operating Custom Tags 403 13.8 Developing a Custom Tag with Scripting Variables 409 Summary 412 Review Questions 412

14. SimPleTagexTenSionS,JSPfragmenTSandTagfileS 414–434 Chapter at a Glance 414 14.1 Introduction 414 14.2 JspFragment Class 415 14.3 The SimpleTag Interface and the SimpleTagSupport Class 416 14.3.1 The SimpleTag Interface 416 14.3.2 The SimpleTagSupport Class 416 14.4 Developing Custom Tags using SimpleTagSupport Class 417 14.4.1 Tag without any Body 417 14.4.2 Tag with Attributes 418 14.4.3 Tag with a Body 421

Page 12: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

Contents | xi

14.4.4 Tag that Evaluates its Body Repeatedly 423 14.4.5 Tag that Modifies its Body 425 14.5 Tag Files 427 14.5.1 Tag File Directives 427 14.6 Tag File Standard Actions 429 14.6.1 <jsp:invoke> Action 429 14.6.2 <jsp:doBody> Action 429 14.7 Tag File Usage 429 14.8 Invoking JSP Fragments 431 Summary 433 Review Questions 434

Glossary 435–449

Index 451–454

Page 13: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

Page 14: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itabJavaServer Pages (JSP) and Servlets technology is being widely used in the field of Information

Technology to develop Web Applications. The aim of writing this book is to assist the reader to make a comprehensive and detailed study of this technology.

organisationofthebookThe book provides 14 chapters which can be summarized as follows. Chapter 1 is an introduction to HyperText Transfer Protocol (HTTP), describing the HTTP request and HTTP response structures. It elaborates on HyperText Markup Language (HTML) and explains the meaning of client-side and server-side scripting. It introduces the reader to JSP and Servlet technology and explains the process of installation, configuration and testing of Java Development Kit (JDK) and the Apache Tomcat Server. Chapter 2 discusses the fundamentals of JavaServer Pages and Servlets. In this chapter the steps of coding a JSP, placing it in a specific directory and requesting it via a browser are discussed. A detailed explanation about the translation of a JSP into a servlet, the presence of the servlet in a specific folder, its compilation to byte code and loading and creation of a single servlet instance is provided. The explanation is done with the help of the Hello World program that is written using a JSP. Next, the discussion focuses on the coding of a generic servlet with the help of the Hello World program that is written using a servlet. The steps of writing the servlet, placing it in an appropriate directory, mapping it and finally requesting it, are discussed. Chapter 3 introduces the reader to the various components of a JSP. The components include expressions, scriptlets, comments, declarations, directives and standard actions. It also discusses the escaping of characters and the internal handling of various JSP components. Chapter 4 elucidates on the various implicit objects of JSP. The implicit objects include the request, response, session, application, out, exception, config, page and pageContext. Chapter 5 deals with two topics namely Servlets and Session Tracking. As part of the discussion of the first topic, the Servlet and ServletConfig interfaces and the GenericServlet class of the javax.servlet package are discussed. This is followed by a discussion of the HttpServlet

xiii

Preface

Page 15: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

KopyK

itab

xiv | Preface

class that is present in the javax.servlet.http package and demonstration of a HTTP servlet. As part of the discussion of the second topic, the concepts of cookies, URL rewriting and hidden fields are elaborated upon. Lastly the tracking of sessions by using cookies as well as by using URL encoding is explained. Chapter 6 discusses the different types of Application Event Listeners. Chapter 7 discusses the concept of a filter, its life cycle, uses and benefits. This is followed by a discussion on the Filter, FilterChain and FilterConfig interfaces. Next, the configuration and chaining of filters is discussed. Lastly, the wrapping of requests and responses is explained. Chapter 8 begins by introducing the reader to the Expression Language (EL). This is followed by a discussion on EL expressions and literals, extraction of property values, EL’s implicit objects, variables, operators, reserved words and EL functions. Chapter 9 introduces the reader to the JSP Standard Tag Library (JSTL), discussing the JSTL core tags and the JSTL functions. Chapter 10 begins with an introduction of XML, followed by a discussion on the Document Object Model and the parsing of XML using the DOM parser. Similarly, the Simple API for XML (SAX) API; the SAX parser; the XSL transformation with XSLT are discussed followed by the JSTL XML tags. Chapter 11 discusses the steps required to install MySQL DBMS, reviews the SQL statements in MySQL and discusses the linking of JSP and MySQL. Then the insert, delete and update operations are demonstrated by using Java code within a JSP. Lastly, the JSTL SQL tags are discussed. Chapter 12 introduces the reader to locales, resource bundles and time zones and discusses internationalization and localization and their necessity. It also discusses each of the JSTL formatting tags. Chapter 13 discusses the classic custom tags; the Tag; IterationTag; BodyTag interfaces as well as the TagSupport and BodyTagSupport classes are discussed. It explains the development of classic tags without a body, with attributes, with a body, with repeated body evaluation, with body modification, co-operating tags and the development of custom tags with scripting variables. Chapter 14 deals with two topics, namely development of tags using the SimpleTagSupport class and the Tag files. It discusses the JspFragment class, the SimpleTag interface and the SimpleTagSupport class. It explains the development of simple tags without a body, with attributes, with body, with repeated body evaluation and with body modification. Lastly this chapter elaborates on tag files. The Tag File Directives, Standard Actions, Tag file usage and Fragment invocation are discussed.

featuresofthisbookI have written this book in a simple and straightforward language. This will make it easier for the reader to comprehend the topics that are covered in this book. I have covered and illustrated the topics exhaustively and in minute detail. Certain books just describe a particular topic and do not illustrate it with examples. The reader is compelled to refer to other books or websites to search for the explanation. In my book, when a topic is discussed, it is illustrated with the help of

Page 16: JSP KopyKitab and Servlets · 2018-03-22 · JSP and ServleTS: an overview 9–18 Chapter at a Glance 9 2.1 Introduction 9 2.2 An Overview of JSP 9 2.3 JSP Hello World Program Development

JSP And Servlets: A ComprehensiveStudy

Publisher : PHI Learning ISBN : 9788120347458 Author : Mahesh P. Matha

Type the URL : http://www.kopykitab.com/product/11833

Get this eBook

25%OFF