Introduction to Servlets Allen Day. Notes This is a training NOT a presentation Please ask questions https://tech.lds.org/wiki/Java_Stack_Training Prerequisites.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Servlets & JSPs - Sharad Ballepu.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Java Server Pages (JSP)
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
Servlets and a little bit of Web Services Russell Beale.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Server Side Programming Web Information Systems 2012.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Servlets Life Cycle. The Servlet Life Cycle A servlet life cycle can be defined as the entire process from its creation till the destruction. The following.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
CSCI 6962: Server-side Design and Programming History and Background.
Java Servlets and JSP.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets - Compiled By Nitin Pai.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
21/12/20151 T h i s p r e s e n t a t i o n w i l l p r o b a b l y i n v o l v e a u d i e n c e d i s c u s s i o n, w h i c h w i l l c r e a t e a.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
Servlets.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
Java and the Web CSE 3330 Southern Methodist University.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Introduction To HTML Dr. Magdi AMER. HTML elements.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Chapter 4 Request and Response. Servlets are controlled by the container.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Distributed Web Systems Java Servlets Lecturer Department University.
Introduction to Servlets
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Java Servlets and JSP.
J2EE Lecture 1:Servlet and JSP
Introduction to Java Servlets
Presentation transcript:

Introduction to Servlets Allen Day

Notes This is a training NOT a presentation Please ask questions Prerequisites – Basic Java and HTML skills – Installed LDSTech IDE (or other equivalent)

Overview Basic Web App Architecture HTTP CGI Overview Understanding the role of servlets Maven Project Directory Structure Servlet Life Cycle Event Listeners Servlet Filters

Basic Web App Architecture Request WWW Browser Web Server Response

Basic Web App Architecture Request WWW Browser Web Server Response

HTTP Request WWW Browser Web Server Response HTTP

HTTP Request Methods GET POST HEAD TRACE PUT DELETE OPTIONS CONNECT

GET Method Simple The total amount of characters in a GET is limited. The data you send with the GET is appended to the URL, so whatever you send is exposed.

POST Method Used for complex requests, such as form submissions. Parameters are stored in the body.

CGI Overview 1. Submit Form WWW Browser Web Server Application Server 2. Call CGI 3. CGI Program’s response 4. CGI Program’s response

CGI Process Form use strict; main(); sub main () { my $query; read( STDIN, $query, $ENV{CONTENT_LENGTH} ); = split( /&/, $query ); my %pairs = (); foreach my $item ) { my ($key, $value) = split( /=/, $item ); $key =~ tr/+/ /; $value =~ tr/+/ /; $key =~ s/%([A-F\d]{2})/chr(hex($1))/ieg; $value =~ s/%([A-F\d]{2})/chr(hex($1))/ieg; $pairs{$key} = $value; } my $name = $pairs{name}; my $ = $pairs{ }; my $machine = $ENV{REMOTE_HOST}; print( STDOUT "Content-Type:text/html\r\n" ); print( STDOUT "Status: 200 Ok\r\n" ); print( STDOUT "\r\n" ); print( STDOUT <<HTML ); Form example output welcome Hi $name of $ from machine $machine HTML }

CGI Issues May intentionally or unintentionally leak information about the host system that will help hackers break in. Scripts may be vulnerable to attacks in which the remote user tricks them into executing commands. Susceptible to Buffer overflows. Insufficient input validation. Each call to a CGI script runs as a separate process. Simultaneous CGI requests cause the CGI script to be copied and loaded into memory as many times as there are requests.

Servlet Overview Client Servlet ContainerWeb Server Request Response

Advantages of Servlets Servlets stay loaded and client requests for a Servlet resource are handled as separate threads of a single running Servlet. A servlet can be run by a servlet engine in a restrictive environment, called a sandbox. This reduces security risks.

Maven Project Directory Structure

pom.xml <project xmlns=" xmlns:xsi=" xsi:schemaLocation=" org.lds.training MyServlet war 1.0 MyServlet Maven Webapp junit test

web.xml <web-app xmlns=" xmlns:xsi=" xsi:schemaLocation=" version="2.5"> Welcome to Java Stack Training Introduction to Servlets HelloWorldServlet org.lds.training.HelloWorldServlet HelloWorldServlet /HelloWorldServlet

Lab 1: Simple Servlet #Lab_1_Simple_Servlet

Servlet Life Cycle 1.Load class 2.Instantiate servlet 3.init() 4.service() 5.destroy()

Servlet Container Client Servlet ContainerWeb Server

Servlet Container 1.Loads the servlet class. 2.Creates an instance of the servlet class. 3.Initializes the servlet instance by calling the init method. 4.Handles client requests. 5.If the container needs to remove the servlet it finalizes the servlet by calling the servlet's destroy method.

Servlet Container Communications support Lifecycle Management Multithreading Support Declarative Security JSP Support

Servlet Container Web Server request response Servlet

Servlet Container requestresponse Servlet thread

Servlet Container request response Servlet thread Service()

Servlet Container response Servlet thread Service() doGet()

Servlet Container Web Server request response X

HttpServletRequest MethodDescription getCookies()Obtain array of cookies getMethod()RETURNS the HTTP method (GET or POST) getPathInfo()Returns any extra path information for the request URI getRemoteUser()Gets the name of the user making the request (provided by HTTP authentication) getSession()Returns the current valid session associated with this request or creates a new session

HttpServletResponse MethodDescription addCookie()Adds the specified cookie to the response encodeURL()Encodes the URL by including the session id in it if needed sendError()Sends an error response to the user with the specified error code sendRedirect()Sends a redirect request to the user

Servlet Class Extends java.servlet.http.HttpServlet init() service() doGet() doPost() destroy()

init() public void init() throws ServletException { // custom code goes here } public void init(ServletConfig config) throws ServletException { super.init(ServletConfig) // custom code goes here }

service() public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Custom code goes here }

doGet() public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Custom Code goes here }

doPost() public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Custom Code goes here }

destroy() public void destroy() { // custom code goes here }

Lab 2: Page Hit Counter #Lab_2_Page_Hit_Counter

Event Listeners

javax.servlet.ServletContextListener javax.servlet.ServletContextAttributeListener javax.servlet.http.HttpSessionListener javax.servlet.http.HttpSessionAttributeListener

Event Listeners javax.servlet.ServletContextListener javax.servlet.ServletContextAttributeListener javax.servlet.http.HttpSessionListener javax.servlet.http.HttpSessionAttributeListener

web.xml org.lds.training.HelloWorldSessionListener org.lds.training.HelloWorldContextListener

Servlet Filters

Client Servlet ContainerWeb Server Request Response Filter 1 Filter 2

web.xml timer filter.TimerFilter timer myservlet /mypath/*

Lab 3: Login Filter #Lab_3_Login_Filter

Credit where credit is due Head First Servlets & JSP Bryan Basham, Kathy Sierra & Bert Bates More Servlets and JavaServer Pages Marty Hall Images from the Microsoft Clip Art gallery