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.

Slides:



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

4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
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)
Java Servlets Java Server Pages (JSP)
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 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.
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
JSP Java Server Pages Reference:
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
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.
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.
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.
CSC 2720 Building Web Applications
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
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.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
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.
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.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
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.
Introduction to Servlets Allen Day. Notes This is a training NOT a presentation Please ask questions Prerequisites.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Chapter 4 Request and Response. Servlets are controlled by the container.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Distributed Web Systems Java Servlets Lecturer Department University.
CS3220 Web and Internet Programming Introduction to Java Servlets
Servlets.
Java Servlets.
Chapter 26 Servlets.
CS3220 Web and Internet Programming Introduction to Java Servlets
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Basic servlet structure
Presentation transcript:

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 HTML 5.Servlets and packages 6.Some utilities that help build HTML 7.The servlet life cycle 8.Servlet debugging strategies

1. Recall the Servlet Role Read the explicit data sent by the client Read the implicit HTTP request data sent by the browser

1. Recall the Servlet Role Generate the results Send the explicit data (i.e., the document) to the client Send the implicit HTTP response data

2. Basic Servlet Structure Listing 3.1 outlines a basic servlet that handles GET requests

2. Basic Servlet Structure GET requests are the usual type of browser requests for Web pages A browser generates this request when the user enters a URL on the address line, follows a link from a Web page Or submits an HTML form that either does not specify a METHOD or specifies METHOD="GET" Post method: Submit button

2. Basic Servlet Structure Servlets extend HttpServlet override doGet or doPost, depending on whether the data is being sent by GET or by POST Servlet can take the same action for both GET and POST requests, simply have doGet call doPost.

2. Basic Servlet Structure Both doGet and doPost take two arguments : HttpServletRequest HttpServletResponse The HttpServletRequest gets at all of the incoming data. The HttpServletResponse specifies outgoing information such as HTTP status codes (200, 404, etc.) and response headers (Content-Type, Set-Cookie, etc.). HttpServletResponse obtains a PrintWriter, use to send document content back to the client.

2. Basic Servlet Structure doGet and doPost throw two exceptions: ServletException IOException  Require to include them in the method declaration. Import classes: java.io (for PrintWriter, etc.) javax.servlet (for HttpServlet, etc.) javax.servlet.http (for HttpServletRequest and HttpServletResponse).

3. A Servlet That Generates Plain Text 3.1. Hello World Problem 3.2. Hello World Solution 3.3. A Servlet That Generates Plain Text

3.1. Hello World Problem Write a program that shows “Hello World” as below:

3.2. Hello World Solution

3.3. A Servlet That Generates Plain Text First, reviewing the process of installing, compiling, and running this simple servlet server is set up properly CLASSPATH refers to the necessary three entries execute successfully

3.3. A Servlet That Generates Plain Text Second type "javac HelloWorld.java" ->create HelloWorld.class Third move HelloWorld.class to the directory that server uses to store servlets that are in the default Web application install_dir/.../WEB-INF/classes Last Invoke servlet. Using either the default URL of

4. A Servlet That Generates HTML 4.1. Hello Problem 4.2 Hello Solution 4.3. A servlet that generates HTML

4.1. Hello Problem Write a program that shows an HTML with content “Hello” as below:

4.2. Hello Solution

4.3. A Servlet That Generates HTML To generate HTML, add three steps to the process just shown: Tell the browser that you're sending it HTML. Modify the println statements to build a legal Web page. Check your HTML with a formal syntax validator.

4.3. A Servlet That Generates HTML response.setContentType("text/html"); Servlets to generate Excel spreadsheets (content type application/vnd.ms-excel—see Section 7.3), Section 7.3 JPEG images (content type image/jpeg—see Section 7.5), Section 7.5 and XML documents (content type text/xml) println statements output HTML

5. Servlet Packaging 5.1. Hello Problem with Packaging 5.2. Hello Packaging Solution 5.3. Servlet Packaging

5.1. Hello Problem with Packaging Shows the servlet accessed by means of the default URL

5.2. Hello Packaging Solution

5.3. Servlet Packaging Since even a single Web application can be large, need the standard Java solution for avoiding name conflicts: packages Step Place the files in a subdirectory that matches the intended package name Insert a package statement in the class file package somePackage install_dir/webapps/ROOT/WEB-INF/classes/coreservlets Listing 3.4 coreservlets/HelloServlet2.java

6. Simple HTML-Building Utilities An HTML document is structured as follows: Listing 3.5 coreservlets/ServletUtilities.java Listing 3.6 coreservlets/HelloServlet3.java

6. Simple HTML-Building Utilities

Result of oServlet3

7. The Servlet Life Cycle When the servlet is first created, its init method is invoked, so init is where you put one-time setup code. After this, each user request results in a thread that calls the service method of the previously created instance. The service method then calls doGet, doPost, or another doXxx method, depending on the type of HTTP request it received. Finally, if the server decides to unload a servlet, it first calls the servlet's destroy method.

7. The Servlet Life Cycle The service Method Each time the server receives a request for a servlet, the server spawns a new thread and calls service The service method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc., as appropriate

7. The Servlet Life Cycle The init method definition looks like this: public void init() throws ServletException { // Initialization code... } The init method performs two varieties of initializations: general initializations and initializations controlled by initialization parameters.

General Initializations Init: creates or loads some data that will be used throughout the life of the servlet, or it performs some one-time computation. Listing 3.7 shows a servlet that uses init to do two things. First, it builds an array of 10 integers, complex calculations. So, doGet looks up the values that init computed, instead of generating them each time. The results of this technique are shown in Figure 3-6.Figure 3-6

General Initializations Book The destroy Method : asked by the server administrator Or the servlet is idle for a long time It closes database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities

8. Servlet Debugging Strategies Use print statements Use an integrated debugger in your IDE. Use the log file. Write separate classes Look at the HTML source. Look at the request data separately Look at the response data separately Stop and restart the server.

Practice Exercise Problem 1: Create a servlet page that view all the month and day of month of Problem 2: Create a servlet page that view all of lucky numbers from 1 to A lucky number is an integer number that have sum of digits mod 10 equal 7 For example: 7 is lucky number 16 is lucky number ( = 7 and 7 % 10 = 7) 250 is lucky number ( = 7 and 7 % 10 = 7) 962 is lucky number ( = 17 and 17 % 10 = 7)