Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

Similar documents
JAVA SERVLET. Server-side Programming INTRODUCTION

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

Servlet Fudamentals. Celsina Bignoli

Enterprise Java Unit 1- Chapter 4 Prof. Sujata Rizal Servlet API and Lifecycle

The Basic Web Server CGI. CGI: Illustration. Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 Database Management Systems 4

Web based Applications, Tomcat and Servlets - Lab 3 -

Servlets and JSP (Java Server Pages)

Servlet Basics. Agenda

Advanced Web Technology

&' () - #-& -#-!& 2 - % (3" 3 !!! + #%!%,)& ! "# * +,

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

Session 8. Introduction to Servlets. Semester Project

Using Java servlets to generate dynamic WAP content

UNIT-V. Web Servers: Tomcat Server Installation:

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Enterprise Java Technologies (Part 1 of 3) Component Architecture. Overview of Java EE. Java Servlets

Module 4: SERVLET and JSP

Java Servlets. Preparing your System

Database Applications Recitation 6. Project 3: CMUQFlix CMUQ s Movies Recommendation System

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

4.1 The Life Cycle of a Servlet 4.2 The Java Servlet Development Kit 4.3 The Simple Servlet: Creating and compile servlet source code, start a web

Advanced Internet Technology Lab # 4 Servlets

Session 9. Introduction to Servlets. Lecture Objectives

Introdução a Servlets

HttpServlet ( Class ) -- we will extend this class to handle GET / PUT HTTP requests

AJP. CHAPTER 5: SERVLET -20 marks

SERVLETS INTERVIEW QUESTIONS

Chapter 29 Servlets: Bonus for Java Developers 1041

Servlets. An extension of a web server runs inside a servlet container

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Java servlets CSCI 470: Web Science Keith Vertanen Copyright 2013

The Servlet Life Cycle

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

Introduction to Servlets. After which you will doget it

Introduction to Java Servlets. SWE 432 Design and Implementation of Software for the Web

Servlets Pearson Education, Inc. All rights reserved.

Unit 4 - Servlet. Servlet. Advantage of Servlet

ServletConfig Interface

HTTP and the Dynamic Web

Servlets Basic Operations

a. Jdbc:ids://localhost:12/conn?dsn=dbsysdsn 21. What is the Type IV Driver URL? a. 22.

********************************************************************

Java4570: Session Tracking using Cookies *

Unit 5 JSP (Java Server Pages)

Stateless -Session Bean

How does the Web work? HTTP and the Dynamic Web. Naming and URLs. In Action. HTTP in a Nutshell. Protocols. The canonical example in your Web browser

Introduction to Web applications with Java Technology 3- Servlets

JAVA SERVLETS INTRODUCTION SERVLETS SHRI GURU RAM RAI INSTITUTE OF TECHNOLOGY & SCIENCE, DEHRADUN

Advanced Topics in Operating Systems. Manual for Lab Practices. Enterprise JavaBeans

CIS 455 / 555: Internet and Web Systems

Introduction. This course Software Architecture with Java will discuss the following topics:

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

Servlets by Example. Joe Howse 7 June 2011

This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

Chettinad College of Engineering and Technology CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND TECHNOLOGY

Chapter 10 Servlets and Java Server Pages

11.1 Introduction to Servlets

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

Questions and Answers

Servlet And JSP. Mr. Nilesh Vishwasrao Patil, Government Polytechnic, Ahmednagar. Mr. Nilesh Vishwasrao Patil

Table of Contents. Introduction... xxi

Chapter 17. Web-Application Development

Topics. Advanced Java Programming. Quick HTTP refresher. Quick HTTP refresher. Web server can return:

1Z Java EE 6 Web Component Developer Certified Expert Exam Summary Syllabus Questions

SERVLET AND JSP FILTERS

A.1 JSP A.2 JSP JSP JSP. MyDate.jsp page contenttype="text/html; charset=windows-31j" import="java.util.calendar" %>

LAB 1 PREPARED BY : DR. AJUNE WANIS ISMAIL FACULTY OF COMPUTING UNIVERSITI TEKNOLOGI MALAYSIA

Type of Classes Nested Classes Inner Classes Local and Anonymous Inner Classes

Lecture Notes On J2EE

2. Follow the installation directions and install the server on ccc. 3. We will call the root of your installation as $TOMCAT_DIR

sessionx Desarrollo de Aplicaciones en Red A few more words about CGI CGI Servlet & JSP José Rafael Rojano Cáceres

Java Technologies Web Filters

Servlet. Web Server. Servlets are modules of Java code that run in web server. Internet Explorer. Servlet. Fire Fox. Servlet.

Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

Chapter 2 How to structure a web application with the MVC pattern

JavaServer Pages (JSP)

CE212 Web Application Programming Part 3

CHAPTER 2: A FAST INTRODUCTION TO BASIC SERVLET PROGRAMMING

Chapter - 1 JAVA RMI

Module 3 Web Component

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

Session 8. JavaBeans. Reading & Reference. Reading. Reference. Session 8 Java Beans. 2/27/2013 Robert Kelly, Head First Chapter 3 (MVC)

Université du Québec à Montréal

CS506 Web Design & Development Final Term Solved MCQs with Reference

AN OVERVIEW OF SERVLET AND JSP TECHNOLOGY

Session 20 Data Sharing Session 20 Data Sharing & Cookies

INTERNET PROGRAMMING TEST-3 SCHEME OF EVALUATION 1.A 3 LIFE CYCLE METHODS - 3M 1.B HTML FORM CREATION - 2 M

PSD1B Advance Java Programming Unit : I-V. PSD1B- Advance Java Programming

Unit-4: Servlet Sessions:

Advance Java. Configuring and Getting Servlet Init Parameters per servlet

The Structure and Components of

First Servlets. Chapter. Topics in This Chapter

UNIT-VI. HttpServletResponse It extends the ServletResponse interface to provide HTTP-specific functionality in sending a response.

Introduction. This course Software Architecture with Java will discuss the following topics:

Web Technology for IE 20 November ISE 582: Information Technology for Industrial Engineering

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

Table of Contents Fast Track to JSF 2

Handout 31 Web Design & Development

Web Application Services Practice Session #2

Transcription:

Java Enterprise Edition Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

Java Beans Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 2

Java Bean POJO class : private Attributes public getters and setters Default constructor Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 3

Java Bean : example public class User { private String login; private String pass; public String getlogin() { return login; } public void setlogin(string login) { this.login = login; } public String getpass() { return pass; } } public void setpass(string pass) { this.pass = pass; } Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 4

Java EE APIs The big picture Focus on Servlets Database(s) Browser JSP HTTP WEB Container Servlets JSF rmi rmi EJBs public static void main( ) { EJB Container Legacy Java Application Application Server Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 5

Web Application Application software, that relies on web browser to render it Building blocks in Java EE: Web Container Servlet JSP or JSF Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 6

Web Container Manages component life cycles Accepts requests, sends responses Routes requests to applications Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 7

Web Container Multiple applications inside one container Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 8

Deployment descriptor : WEB.XML (1) Instructs the container how handle this application Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 9

WEB.XML (2) In Servlet API version 3.0 most components of web.xml are replaced by annotations that go directly to Java source code. Before Servlet 3.0 web.xml <servlet> <servlet-name>hello</servlet-name> <servletclass>example.helloservlet</servle t-class> </servlet> In Servlet 3.0 via annotations @WebServlet("/hello") public class HelloServlet extends HttpServlet {... <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 10

Servlets Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 11

Methods ServletConfig getservletconfig() The Servlet API Returns reference to object, gives access to config info void service ( ServletRequest request, ServletResponse response ) Key method in all servlets Provide access to input and output streams Read from and send to client void destroy() Cleanup method, called when servlet exiting Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 12

Life Cycle of Servlet servlet GenericServlet HttpServlet init(servletconfig); service(servletrequest, ServletResponse); destroy(); doget(httpservletrequest, HttpServletResponse); dopost(httpservletrequest, HttpServletResponse);. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 13

HttpServlet Class HttpServlet Base class for web-based servlets Overrides method service Request methods: GET - retrieve HTML documents or image POST - send server data from HTML form Methods doget and dopost respond to GET and POST Called by service Receive HttpServletRequest and HttpServletResponse (return void) objects Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 14

HttpServletRequest Interface HttpServletRequest interface Object passed to doget and dopost Extends ServletRequest Methods String getparameter( String name ) Returns value of parameter name (part of GET or POST) Enumeration getparameternames() Returns names of parameters (POST) String[] getparametervalues( String name ) Returns array of strings containing values of a parameter Cookie[] getcookies() Returns array of Cookie objects, can be used to identify client Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 15

HttpServletResponse Interface HttpServletResponse Object passed to doget and dopost Extends ServletResponse Methods void addcookie( Cookie cookie ) Add Cookie to header of response to client ServletOutputStream getoutputstream() Gets byte-based output stream, send binary data to client PrintWriter getwriter() Gets character-based output stream, send text to client void setcontenttype( String type ) Specify MIME type of the response (Multipurpose Internet Mail Extensions) MIME type text/html indicates that response is HTML document. Helps display data Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 16

Handling HTTP GET Requests HTTP GET requests Usually gets content of specified URL Usually HTML document (web page) Example servlet Handles HTTP GET requests User clicks Get Page button in HTML document GET request sent to servlet HTTPGetServlet Servlet dynamically creates HTML document displaying "Welcome to Servlets!" Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 17

Handling HTTP GET Requests 3 import javax.servlet.*; 4 import javax.servlet.http.*; Use data types from javax.servlet and javax.servlet.http 7 public class HTTPGetServlet extends HttpServlet { HttpServlet has useful methods, inherit from it 8 public void doget( HttpServletRequest request, 9 HttpServletResponse response ) 10 throws ServletException, IOException Method doget Responds to GET requests Default action: BAD_REQUEST error (file not found) Override for custom GET processing Arguments represent client request and server response Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 18

Handling HTTP GET Requests 14 response.setcontenttype( "text/html" ); // content type setcontenttype Specify content text/html for HTML documents 12 PrintWriter out; 15 out = response.getwriter(); // get writer getwriter Returns PrintWriter object, can send text to client getoutputstream to send binary data (returns ServletOutputStream object) Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 19

Handling HTTP GET Requests 19 out.println( "<HTML><HEAD><TITLE>\n" ); 20 out.println( "A Simple Servlet Example\n" ); 21 out.println( "</TITLE></HEAD><BODY>\n" ); 22 out.println( "<H1>Welcome to Servlets!</H1>\n" ); 23 out.println( "</BODY></HTML>" ); Lines 19-23 create HTML document println sends response to client Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 20

Handling HTTP GET Requests Running servlets Must be running on a server Either a full application server (Glassfish) Or 'just' a Web container (Tomcat) Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 21

Handling HTTP GET Requests Port number Where server waits for client (handshake point) Client must specify proper port number Integers 1-65535, 1024 and below usually reserved Well-known port numbers Web servers - port 80 default JSDK/Apache Tomcat 4.0 Webserver- port 8080 Change in default.cfg (server.port=8080) Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 22

Handling HTTP GET Requests HTML documents 1 <!-- HTTPGetServlet.html --> 2 <HTML> 3 <HEAD> 4 <TITLE> 5 Servlet HTTP GET Example 6 </TITLE> 7 </HEAD> Comments: <!-- text --> Tags: <TAG>... </TAG> <HTML>... <HTML> tags enclose document <HEAD>... </HEAD> - enclose header Includes <TITLE> Title </TITLE> tags Sets title of document Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 23

Handling HTTP GET Requests 9 <FORM 10 ACTION="http://localhost:8080/BasicReqHandlingServlet" 11 METHOD="GET"> 12 <P>Click the button to have the servlet send 13 an HTML document</p> 14 <INPUT TYPE="submit" VALUE="Get HTML Document"> 15 </FORM> 16 </BODY> Document body (<BODY> tags) Has literal text and tags for formatting Form (<FORM> tags ) ACTION - server-side form handler METHOD - request type Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 24

Handling HTTP GET Requests 10 ACTION="http://localhost:8080/BasicReqHandlingServlet" ACTION localhost - your computer :8080 - port /servlet - servlet name 14 <INPUT TYPE="submit" VALUE="Get HTML Document"> GUI component INPUT element TYPE - "submit" (button) VALUE - label When pressed, performs ACTION If parameters passed, separated by? in URL Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 25

1 2 // Creating and sending a page to the client 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 import java.io.*; 6 7 public class HTTPGetServlet extends HttpServlet { 8 public void doget( HttpServletRequest request, 9 HttpServletResponse response ) 10 throws ServletException, IOException 1. import 11 { 12 PrintWriter out; 13 14 response.setcontenttype( "text/html" ); // content type 15 out = response.getwriter(); // get writer 16 1.1 extends HttpServlet 17 // create and send HTML page to client 18 19 out.println( "<HTML><HEAD><TITLE>\n" ); 20 out.println( "A Simple Servlet Example\n" ); 2. doget 21 out.println( "</TITLE></HEAD><BODY>\n" ); 22 out.println( "<H1>Welcome to Servlets!</H1>\n" ); 23 out.println( "</BODY></HTML>" ); 24 } 25 } Import necessary classes and inherit methods from HttpServlet. Create PrintWriter object. Create HTML file and send to client. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 26

1 2 <HTML> 3 <HEAD> 4 <TITLE> 5 Servlet HTTP GET Example 6 </TITLE> 7 </HEAD> 8 <BODY> 9 <FORM 10 ACTION="http://localhost:8080/BasicReqHandlingServlet" HTML document 11 METHOD="GET"> 12 <P>Click the button to have the servlet send 13 an HTML document</p> 14 <INPUT TYPE="submit" VALUE="Get HTML Document"> 15 </FORM> 1. <TITLE> 16 </BODY> 17 </HTML> Creates submit button, performs ACTION when clicked. ACTION specifies form handler, METHOD specifies request type. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 27

Scopes (First look) Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 28