SECTION II: JAVA SERVLETS

Similar documents
Distributed Multitiered Application

Developing the First Servlet

Life Without NetBeans

Web Application Architecture (based J2EE 1.4 Tutorial)

JBoss SOAP Web Services User Guide. Version: M5

Java EE Application Assembly & Deployment Packaging Applications, Java EE modules. Model View Controller (MVC)2 Architecture & Packaging EJB Module

Vision of J2EE. Why J2EE? Need for. J2EE Suite. J2EE Based Distributed Application Architecture Overview. Umair Javed 1

Contents. 1. JSF overview. 2. JSF example

CHAPTER 6. Organizing Your Development Project. All right, guys! It s time to clean up this town!

web.xml Deployment Descriptor Elements

J2EE Development. Course Detail: Audience. Duration. Course Abstract. Course Objectives. Course Topics. Class Format.

JDK-WildFly-NetBeans Setup Local

Writing Servlets and JSPs p. 1 Writing a Servlet p. 1 Writing a JSP p. 7 Compiling a Servlet p. 10 Packaging Servlets and JSPs p.

How to use J2EE default server

Designing a Distributed System

SUN Enterprise Development with iplanet Application Server

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

Module 3 Web Component

Chapter 2 FEATURES AND FACILITIES. SYS-ED/ Computer Education Techniques, Inc.

Classloader J2EE rakendusserveris (Bea Weblogic Server, IBM WebSphere)

Session 8. Reading and Reference. en.wikipedia.org/wiki/list_of_http_headers. en.wikipedia.org/wiki/http_status_codes

Anno Accademico Laboratorio di Tecnologie Web Introduzione ad Eclipse e Tomcat

servlets and Java JSP murach s (Chapter 2) TRAINING & REFERENCE Mike Murach & Associates Andrea Steelman Joel Murach

Developing Applications with Java EE 6 on WebLogic Server 12c

5.1 Registration and Configuration

Java- EE Web Application Development with Enterprise JavaBeans and Web Services

J2EE Interview Questions

Enterprise Java Unit 1-Chapter 2 Prof. Sujata Rizal Java EE 6 Architecture, Server and Containers

Introduction to JSP and Servlets Training 5-days

JBoss WS User Guide. Version: GA

SECTION I: ALL ABOUT THE SPRING FRAMEWORK

CSC 8205 Advanced Java

For live Java EE training, please see training courses at

Java Programming Language

Contents at a Glance

Ch04 JavaServer Pages (JSP)

Session 9. Deployment Descriptor Http. Reading and Reference. en.wikipedia.org/wiki/http. en.wikipedia.org/wiki/list_of_http_headers

Author - Ashfaque Ahmed

Java Programming Course Overview. Duration: 35 hours. Price: $900

SSC - Web applications and development Introduction and Java Servlet (I)

CS506 Web Design & Development Final Term Solved MCQs with Reference

SYLLABUS JAVA COURSE DETAILS. DURATION: 60 Hours. With Live Hands-on Sessions J P I N F O T E C H

JBoss WS User Guide. Version: CR1

Setup and Getting Startedt Customized Java EE Training:

WA2018 Programming REST Web Services with JAX-RS WebLogic 12c / Eclipse. Student Labs. Web Age Solutions Inc.

Tutorial: Developing a Simple Hello World Portlet

Jakarta Struts: An MVC Framework

Advanced Java Programming

NetBeans IDE Field Guide

CO Java EE 7: Back-End Server Application Development

J2EE - Version: 25. Developing Enterprise Applications with J2EE Enterprise Technologies

The team that wrote this redbook

Deccansoft Software Services. J2EE Syllabus

CMP 436/774. Introduction to Java Enterprise Edition. Java Enterprise Edition

AppDev StudioTM 3.2 SAS. Migration Guide

Distributed Systems Project 1 Assigned: Friday, January 26, 2018 Due: Friday, February 9, 11:59 PM

Module 5 Developing with JavaServer Pages Technology

Structure of a webapplication

Setting Up the Development Environment

Configuring Tomcat for a Web Application

PHP Development for ClearPath. Session 3028, Tuesday, May 15, 2012, 10:30AM Ron Neubauer, Principal Engineer, Unisys Corporation

UIMA Simple Server User Guide

Eclipse Java Ejb 3.0 Tutorial For Beginners With Examples Pdf

Java SE7 Fundamentals

ByggSøk plan Project Structure And Build Process

Java EE 6: Develop Web Applications with JSF

S imilar to JavaBeans, custom tags provide a way for

COPYRIGHTED MATERIAL

Struts: A Quick Installation Guide

IBM. IBM WebSphere Application Server Migration Toolkit. WebSphere Application Server. Version 9.0 Release

WAS: WebSphere Appl Server Admin Rel 6

Introduction. Enterprise Java Instructor: Please introduce yourself Name Experience in Java Enterprise Edition Goals you hope to achieve

SAS Web Infrastructure Kit 1.0. Overview

Fast Track to Java EE 5 with Servlets, JSP & JDBC

Y ou must have access to a JSP-compatible Web server

Oracle Enterprise Pack for Eclipse 11g Hands on Labs

Fast Track to Java EE

Solving Application Installation Issues During Migration

Oracle Fusion Middleware

Unraveling the Mysteries of J2EE Web Application Communications

NetBeans 6.5.1, GlassFish v 2.1, Web Space Server 10 Creating a Healthcare Facility JSR286-compliant Portlet

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Java EE 5 Development for WebSphere Application Server V7

Sentences Installation Guide. Sentences Version 4.0

JavaServer Faces Programming By Budi Kurniawan READ ONLINE

Enhydra 6.2 Application Architecture. Tanja Jovanovic

Supplement IV.E: Tutorial for Tomcat For Introduction to Java Programming By Y. Daniel Liang

Java.. servlets and. murach's TRAINING & REFERENCE 2ND EDITION. Joel Murach Andrea Steelman. IlB MIKE MURACH & ASSOCIATES, INC.

Java Application Development On Linux Ebooks Free

Java with Eclipse: Setup & Getting Started

Components and Application Frameworks

Just Get it Written: Deploying Applications to WebLogic Server Using JDeveloper and WLS Console. Don t get it right, just get it written.

Web Application Services Practice Session #2

<put document name here> 1/13

Free Downloads Java Servlet & JSP Cookbook

GlassFish Survival Guide. Alexis Moussine-Pouchkine GlassFish Team Sun Microsystems

Workshop for WebLogic introduces new tools in support of Java EE 5.0 standards. The support for Java EE5 includes the following technologies:

Oracle WebLogic Server

Authentication of a WS Client Using a SAP Logon Ticket

Don t get it right, just get it written.

Transcription:

Chapter 7 SECTION II: JAVA SERVLETS Working With Servlets Working with Servlets is an important step in the process of application development and delivery through the Internet. A Servlet as explained earlier is a Java program that must be run by a Java Servlet engine on a Java enabled Web server. The Servlet's output is delivered to a Web browser. GlassFish Server 4.0 that comes bundled with the NetBeans IDE 7.4 has built-in support for running Java Servlets. This book uses the bundled server to demonstrate and run Servlets. Getting Started Working with Servlets requires the knowledge of: What is a web application The organization of a web application How to create a Servlet file Compiling and building the application

124 Java EE 7 For Beginners Deploying the application Executing the Servlet / web application What Is A Web Application? A Web application, sometimes called a Web app, is an application that is accessed using a Web browser over a network such as the Internet or an Intranet. While many Web applications are written directly in PHP or Perl, Java remains a commonly used programming language for writing Web applications. This is especially true for Web based enterprise applications [usually referred to as enterprise Web applications]. Java EE 7 provides several useful components such as JavaServer Pages, JavaServer Faces, Servlets, client-side applets, Enterprise JavaBeans, JDBC and several other Web service technologies for writing enterprise Web applications. This chapter focuses on writing Servlets as the Web application of choice. Basically, a Web application is defined as a hierarchy of directories and files in a standard layout. There are two ways in which such a hierarchy can be accessed: The first is where each directory and file exists in the file system separately. This is termed as an application in its unpacked form. It is used during application development The second is where all the sub directories are zipped together in a packed form known as a Web ARchive or WAR file. It is used when distributing the application to be installed Organization Of A Web Application Before beginning to work with Servlets, it is useful to examine the runtime organization of a Web application. Before the Servlet API Specification, version 2.2, across servers there was little consistency in the WAR file structure. However, Web servers that conform to the Servlet API version 2.2 [or later] the Web application ARchive must be in a standard format before it is accepted. These are discussed below.

This page is not part of the book preview. Working With Servlets 125

126 Java EE 7 For Beginners HINT In addition to the above specified files and directories, application specific directories can also be created either in the document root directory or the /WEB- INF/classes/ directory. For example, all HTML files can be placed under html directory, similarly all the images can be placed under a directory named pictures and so on. Deployment Descriptor Servlet API version 3.0 onwards, the deployment descriptor [web.xml] takes precedence over annotations. The deployment descriptor overrides configuration information specified through the annotation mechanism. Version 3.0 of the web deployment descriptor contains a new attribute called metadatacomplete on <web-app>. This attribute defines whether the web descriptor is complete or whether the class files of the web application should be examined for annotations that specify deployment information. If this attribute is set to true, the deployment tool must ignore any servlet annotations present in the class files and use only the configuration details mentioned in the descriptor. Otherwise, if the value is not specified or set to false, the container must scan all class files of the application for annotations. This provides a way to enable or disable scanning of the annotation and its processing during the startup of the application. Annotations introduced in Servlet 3.0 are explained later in this chapter. Context Path When the application is being deployed on a particular Web server, a context path to the application has to be assigned. This means if the application is assigned to the context path as /bookshop, then a request URI referring to /bookshop/index.html will retrieve the index.html file from that document root.

Creating A Web Application Using NetBeans Working With Servlets 127 Let's create a simple HTML form that accepts user's name and submits the same to the Servlet. The Servlet in response welcomes the user based on the user name it receives. Since NetBeans is the IDE of choice throughout this book, let's use it to create a new Web Application Project called MyFirstServlet. Run the NetBeans IDE and select File New Project to create a new Web Application project in it, as shown in diagram 7.2.1. Diagram 7.2.1: File New Project New Project dialog box appears, as shown in diagram 7.2.2. Select Java Web option available under Categories list and Web Application option available under Projects list, as shown in diagram 7.2.2.

128 Java EE 7 For Beginners This page is not part of the book preview.

This page is not part of the book preview. Working With Servlets 129

130 Java EE 7 For Beginners This page is not part of the book preview.

This page is not part of the book preview. Working With Servlets 131

132 Java EE 7 For Beginners This page is not part of the book preview.

This page is not part of the book preview. Working With Servlets 133

134 Java EE 7 For Beginners This page is not part of the book preview.

This page is not part of the book preview. Working With Servlets 135

136 Java EE 7 For Beginners This page is not part of the book preview.

This page is not part of the book preview. Working With Servlets 137

138 Java EE 7 For Beginners Explanation: web.xml is a deployment descriptor XML file that consists of several elements. REMINDER Servlet API version 3.0 onwards, the deployment descriptor [web.xml] takes precedence over annotations. This means it is not mandatory to have a deployment descriptor, the annotation mechanism can be used instead. Depending on the components in the Web application, there is no need to include all of the elements to configure and deploy the Web application. <servlet> is the most important element for describing the Servlets in an application. <servlet> consists of many sub elements among which the required ones are <servlet-name> and <servlet-class>. The <web-app> Element All the entries are wrapped within a pair of opening and closing <web-app> elements: The body of <web-app> itself contains additional elements that determine how the Web application will run in the GlassFish Server. REMINDER In XML, properties are defined by surrounding a property name or value with opening and closing tags. The opening tag, the body [the property name or value] and the closing tag are collectively called an element. Some of the elements do not use the surrounding tags, but instead use a single tag that contains attributes called an empty tag. The <servlet> Element Servlets are registered and configured as a part of Web application. To register a Servlet, add several entries to the Web Application deployment descriptor.

This page is not part of the book preview. Working With Servlets 139

140 Java EE 7 For Beginners This page is not part of the book preview.

Working With Servlets 141 Diagram 7.6.1: Clicking Build Once Build is clicked, the details about the compilation and building of the MyFirstServlet Web application appear in the Output - MyFirstServlet (dist) window, as shown in diagram 7.6.2. Diagram 7.6.2: The details of the Build been successful

142 Java EE 7 For Beginners This page is not part of the book preview.

This page is not part of the book preview. Working With Servlets 143

144 Java EE 7 For Beginners Diagram 7.7.4: Entering the name Click. WelcomeServlet is called, which displays the welcome message along with the name entered, as shown in diagram 7.7.5. Diagram 7.7.5: Welcome message displayed Click to return to the main page i.e. the index.jsp page. Using Annotations Instead Of Deployment Descriptor Servlet API 3.0 onwards, instead of making an entry in a deployment descriptor [web.xml], annotations can be used to mark a Servlet. With the introduction of annotations, the deployment descriptor web.xml becomes optional. All annotations introduced in Servlet 3.0 are available under the package javax.servlet.http.annotation.

This page is not part of the book preview. Working With Servlets 145

146 Java EE 7 For Beginners This page is not part of the book preview.

This page is not part of the book preview. Working With Servlets 147

148 Java EE 7 For Beginners Diagram 7.9: Servlet 3.1 specifications where deployment descriptor is optional Running The Web Application Since the Servlet i.e. WelcomeServlet.java and the JSP i.e. index.jsp files are in place, it's time to compile and build the Web application. Follow the steps shown in the section Compiling And Building The Web Application to compile and build the Web application named MySecondServlet. Follow the above steps shown in the section Running The Web Application to run the Web application named MySecondServlet. The Book CDROM holds the complete application source code built using the NetBeans IDE for the following applications: Codespecs \ Section02 \ Chapter07_CDS \ MyFirstServlet Codespecs \ Section02 \ Chapter07_CDS \ MySecondServlet The web application can be directly used by compiling, building and executing it.