Java Servlets - Tutorialspoint

 Java Servlets

About the Tutorial

Servlets provide a component-based, platform-independent method for building Webbased applications, without the performance limitations of CGI programs. Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

Audience

This tutorial is designed for Java programmers with a need to understand the Java Servlets framework and its APIs. After completing this tutorial you will find yourself at a moderate level of expertise in using Java Servlets from where you can take yourself to next levels.

Prerequisites

We assume you have good understanding of the Java programming language. It will be great if you have a basic understanding of web application and how internet works.

Copyright & Disclaimer

? Copyright 2015 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of the contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness, or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@

i

Java Servlets

Table of Contents

About the Tutorial ..................................................................................................................................i Audience ................................................................................................................................................i Prerequisites ..........................................................................................................................................i Table of Content....................................................................................................................................ii

1. SERVLETS ? OVERVIEW ......................................................................................................1

Servlets Tasks ........................................................................................................................................2 Servlets Packages ..................................................................................................................................2 What is Next? ........................................................................................................................................2

2. SERVLETS ? ENVIRONMENT SETUP ....................................................................................3

Setting up Java Development Kit ...........................................................................................................3 Setting up Web Server: Tomcat .............................................................................................................3 Setting Up the CLASSPATH.....................................................................................................................5

3. SERVLETS ? LIFE CYCLE.......................................................................................................7

The init() Method ..................................................................................................................................7 The service() Method.............................................................................................................................7 The doGet() Method..............................................................................................................................8 The doPost() Method.............................................................................................................................8 The destroy() Method............................................................................................................................8 Architecture Diagram ............................................................................................................................9

4. SERVLETS ? EXAMPLES.....................................................................................................10

Sample Code........................................................................................................................................10 Compiling a Servlet..............................................................................................................................11 Servlet Deployment.............................................................................................................................11

ii

Java Servlets

5. SERVLETS ? FORM DATA ..................................................................................................13

GET Method ........................................................................................................................................13 POST Method ......................................................................................................................................13 Reading Form Data using Servlet .........................................................................................................13 GET Method Example using URL ..........................................................................................................14 GET Method Example Using Form .......................................................................................................16 POST Method Example Using Form .....................................................................................................16 Passing Checkbox Data to Servlet Program..........................................................................................18 Reading All Form Parameters ..............................................................................................................20

6. SERVLETS ? CLIENT HTTP REQUEST..................................................................................24

Methods to read HTTP Header ............................................................................................................25 HTTP Header Request Example............................................................................................................27

7. SERVLETS ? SERVER HTTP RESPONSE ...............................................................................30

Methods to Set HTTP Response Header...............................................................................................32 HTTP Header Response ? Example.......................................................................................................33

8. SERVLETS ? HTTP STATUS CODES.....................................................................................36

Methods to Set HTTP Status Code .......................................................................................................39 HTTP Status Code Example ..................................................................................................................39

9. SERVLETS ? WRITING FILTERS .......................................................................................... 41

Servlet Filter Methods .........................................................................................................................41 Servlet Filter ? Example .......................................................................................................................42 Servlet Filter Mapping in Web.xml ......................................................................................................43 Using Multiple Filters...........................................................................................................................43 Filters Application Order .....................................................................................................................44

iii

Java Servlets

10. SERVLETS ? EXCEPTION HANDLING..................................................................................46

web.xml Configuration ........................................................................................................................46 Request Attributes ? Errors/Exceptions...............................................................................................47 Error Handler Servlet ? Example ..........................................................................................................48

11. SERVLETS ? COOKIES HANDLING......................................................................................52

The Anatomy of a Cookie.....................................................................................................................52 Servlet Cookies Methods .....................................................................................................................53 Setting Cookies with Servlet ................................................................................................................54 Reading Cookies with Servlet ..............................................................................................................56 Delete Cookies with Servlet.................................................................................................................58

12. SERVLETS ? SESSION TRACKING .......................................................................................61

Cookies ................................................................................................................................................ 61 Hidden Form Fields..............................................................................................................................61 URL Rewriting......................................................................................................................................61 The HttpSession Object .......................................................................................................................62 Session Tracking Example ....................................................................................................................63 Deleting Session Data ..........................................................................................................................66

13. SERVLETS ? DATABASE ACCESS ........................................................................................68

Create Table ........................................................................................................................................68 Create Data Records ............................................................................................................................69 Accessing a Database...........................................................................................................................69

14. SERVLETS ? FILE UPLOADING ........................................................................................... 73

Creating a File Upload Form ................................................................................................................73 Writing Backend Servlet ......................................................................................................................74 Compile and Running Servlet...............................................................................................................78

iv

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

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

Google Online Preview   Download