Sun Exam_for e-book June06.book Page iii Tuesday, June 11, 2002 7:10 PM SCWCD Exam Study Kit JAVA WEB COMPONENT DEVELOPER CERTIFICATION Hanumant Deshmukh Jignesh Malavia with Jacquelyn Carter MANNING Greenwich (74° w. long.) Sun Exam_for e-book June06.book Page iv Tuesday, June 11, 2002 7:10 PM For online information and ordering of this and other Manning books, go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: [email protected] ©2003 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. The authors and publisher have taken care in the preparation of this book, but make noexpress or implied warranty of any kind and assume no responsibility for errors or omissions. The authors and publisher assume no liability for losses or damages in connection with or resulting from the use of information or programs in the book and theaccompanying CD. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Liz Welch 209 Bruce Park Avenue Typesetter: D. Dalinnik Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-930110-59-6 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – VHG – 06 05 04 03 02 Sun Exam_for e-book June06.book Page v Tuesday, June 11, 2002 7:10 PM To my alma mater, IT-BHU –Hanumant To my parents –Jignesh Sun Exam_for e-book June06.book Page vi Tuesday, June 11, 2002 7:10 PM Sun Exam_for e-book June06.book Page vii Tuesday, June 11, 2002 7:10 PM brief contents Part 1 Getting started 1 1 Understanding Java servlets 3 2 Understanding JavaServer Pages 14 3 Web application and HTTP basics 21 Part 2 Servlets 29 4 The Servlet model 31 5 Structure and deployment 66 6 The servlet container model 81 7 Handling server-side exceptions 94 8 Session management 113 9 Developing secure web applications 133 10 Developing thread-safe servlets 156 Part 3 Java Server Pages 173 11 The JSP technology model—the basics 175 12 The JSP technology model—advanced topics 200 vii Sun Exam_for e-book June06.book Page viii Tuesday, June 11, 2002 7:10 PM 13 Reusable web components 231 14 Using JavaBeans 248 15 Using custom tags 282 16 Developing custom tag libraries 298 Part 4 Patterns and filters 343 17 Design patterns 345 18 Using filters 376 Appendices A Installing Tomcat 4.0.1 397 B An introduction to XML 402 C A sample web.xml file 411 D Review Q & A 415 E Exam Quick Prep 475 viii BRIEF CONTENTS Sun Exam_for e-book June06.book Page ix Tuesday, June 11, 2002 7:10 PM contents preface xvii about this book xx taking the exam xxiii about the authors xxv acknowledgments xxvi about the cover illustration xxviii Part 1 Getting started 1 1 Understanding Java servlets 3 1.1 What is a servlet? 3 (cid:1) Server responsibilities 3 Server extensions 4 1.2 What is a servlet container? 5 (cid:1) The big picture 5 Understanding servlet containers 6 Using Tomcat 7 1.3 Hello World servlet 8 (cid:1) (cid:1) (cid:1) Code 8 Compilation 9 Deployment 9 Execution 9 1.4 The relationship between a servlet container and the Servlet API 9 (cid:1) The javax.servlet package 10 The javax.servlet.http package 11 Advantages and disadvantages of the Servlet API 12 1.5 Summary 12 2 Understanding JavaServer Pages 14 2.1 What is a JSP page? 14 Server-side includes 15 2.2 Hello User 15 (cid:1) (cid:1) The HTML code 15 The servlet code 16 The JSP code 16 2.3 Servlet or JSP? 17 2.4 JSP architecture models 17 (cid:1) The Model 1 architecture 17 The Model 2 architecture 18 ix Sun Exam_for e-book June06.book Page x Tuesday, June 11, 2002 7:10 PM 2.5 A note about JSP syntax 19 2.6 Summary 20 3 Web application and HTTP basics 21 3.1 What is a web application? 22 Active and passive resources 22 Web applications and the web application server 22 3.2 Understanding the HTTP protocol 23 (cid:1) HTTP basics 24 The structure of an HTTP request 24 The structure of an HTTP response 26 3.3 Summary 27 Part 2 Servlets 29 4 The Servlet model 31 4.1 Sending requests: Web browsers and HTTP methods 32 Comparing HTTP methods 33 4.2 Handling HTTP requests in anHttpServlet 34 4.3 Analyzing the request 36 Understanding ServletRequest 36 Understanding HttpServletRequest 37 4.4 Sending the response 39 Understanding ServletResponse 39 Understanding HttpServletResponse 42 4.5 Servlet life cycle 45 Loading and instantiating a servlet 45 (cid:1) Initializing a servlet 46 Servicing client requests 47 (cid:1) Destroying a servlet 47 Unloading a servlet 47 Servlet state transition from the servlet container’s perspective 47 4.6 ServletConfig: a closer look 49 ServletConfig methods 49 Example: a servlet and its deployment descriptor 50 4.7 ServletContext: a closer look 52 4.8 Beyond servlet basics 54 Sharing the data (attribute scopes) 55 Coordinating servlets using RequestDispatcher 56 Putting it all together: A simple banking application 58 4.9 Summary 61 4.10 Review questions 62 x CONTENTS Sun Exam_for e-book June06.book Page xi Tuesday, June 11, 2002 7:10 PM 5 Structure and deployment 66 5.1 Directory structure of awebapplication 67 Understanding the document root directory 68 Understanding the WEB-INF directory 68 (cid:1) The web archive (WAR) file 69 The default web application 69 5.2 The deployment descriptor: an overview 70 Example: A simple deployment descriptor 71 Using the <servlet> element 72 Using the <servlet-mapping> element 73 Mapping a URL to a servlet 74 5.3 Summary 78 5.4 Review questions 78 6 The servlet container model 81 6.1 Initializing ServletContext 82 6.2 Understanding application eventsandlisteners 83 javax.servlet.ServletContextListener 84 javax.servlet.ServletContextAttributeListener 85 javax.servlet.http.HttpSessionAttributeListener 86 6.3 Configuring a web application 86 6.4 Web applications in a distributedenvironment 88 (cid:1) Behavior of a ServletContext 89 Behavior of an HttpSession 90 6.5 Summary 90 6.6 Review questions 91 7 Handling server-side exceptions 94 7.1 Handling exceptions programmatically 95 Handling business logic exceptions 97 7.2 Handling exceptions declaratively 99 Using declarative exception handling 99 Using servlets and JSP pages as exception handlers 101 7.3 Using RequestDispatcher to handleexceptions 105 Handling exceptions thrown by RequestDispatcher 106 7.4 Logging 107 7.5 Summary 108 7.6 Review questions 109 CONTENTS xi Sun Exam_for e-book June06.book Page xii Tuesday, June 11, 2002 7:10 PM 8 Session management 113 8.1 Understanding state and sessions 114 8.2 Using HttpSession 115 Working with an HttpSession 116 Handling session events with listener interfaces 118 Expunging a session 123 8.3 Understanding session timeout 124 8.4 Implementing session support 125 Supporting sessions using cookies 126 Supporting sessions using URL rewriting 127 8.5 Summary 129 8.6 Review questions 130 9 Developing secure web applications 133 9.1 Basic concepts 134 (cid:1) Authentication 134 Authorization 134 (cid:1) Data integrity 135 Confidentiality or data privacy 135 (cid:1) (cid:1) Auditing 135 Malicious code 135 Web site attacks 135 9.2 Understanding authentication mechanisms 136 (cid:1) HTTP Basic authentication 137 HTTP Digest authentication 139 (cid:1) HTTPS Client authentication 139 FORM-based authentication 139 Defining authentication mechanisms for web applications 140 9.3 Securing web applications declaratively 142 (cid:1) display-name 143 web-resource-collection 143 (cid:1) auth-constraint 144 user-data-constraint 145 Putting it all together 145 9.4 Securing web applications programmatically 149 9.5 Summary 151 9.6 Review questions 152 10 Developing thread-safe servlets 156 10.1 Understanding the multithreaded servletmodel 157 10.2 Understanding the single-threaded model 159 The javax.servlet.SingleThreadModel interface 160 10.3 Variable scopes and thread safety 162 (cid:1) Local variables 163 Instance variables 164 Class (or static) variables 166 10.4 Attribute scopes and thread safety 166 (cid:1) (cid:1) Context scope 167 Session scope 168 Request scope 170 10.5 Summary 170 10.6 Review questions 171 xii CONTENTS