CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.

Slides:



Advertisements
Similar presentations
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Advertisements

Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
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.
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 Stoney Jackson
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
2/16/2004 Dynamic Content February 16, /16/2004 Assignments Due – Message of the Day Part 1 Due – Reading and Warmup Work on Message of the Day.
27-Jun-15 Directories and DDs. 2 Web apps A web application is basically a web site that: “Knows who you are”--it doesn’t just give you static pages,
Server Side Programming Web Information Systems 2012.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Java Servlets and JSP.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Java Servlet Technology. Introduction Servlets are Java programs that run on a Web server, handle HTTP requests and build Web pages Servlet specification.
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.
Java Servlets and Java Server Pages Carol Wolf Computer Science.
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.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
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.
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.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
Java Servlets and Java Server Pages Norman White Stern School of Business.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
J2EE T ECHNOLOGIES These are the technologies required to build large scale distributed applications, can be divided into – Component Technologies eg.
Configuration Web Server Tomcat - Install JDK Install Tom cat Configure Tom cat for running Servlet C:\Program Files\Apache Software Foundation\Tomcat.
Java Servlets and Java Server Pages
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
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.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Distributed Web Systems Java Servlets Lecturer Department University.
Introduction to Servlets
CS3220 Web and Internet Programming Introduction to Java Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Servlets Hits Counter 20-Jul-18.
Pre-assessment Questions
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
CS320 Web and Internet Programming MVC Architecture
Objectives In this lesson you will learn about: Need for servlets
Java Servlets and JSP.
CS3220 Web and Internet Programming Introduction to Java Servlets
CS520 Web Programming Java Annotations
Servlets.
Introduction to Java Servlets
Directories and DDs 25-Apr-19.
Basic servlet structure
Directories and DDs 21-Jul-19.
Directories and DDs 14-Sep-19.
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles

Java Web Application Components Compiled Java classes (.class files) Servlets, beans, filters,... Addtional Java libraries (.jar files) JavaServer Pages (JSPs) Static resources HTML, CSS, images,... Metadata files web.xml,...

Directory Structure of a Java Web Application Application Root Directory WEB-INF JSPs and static resources classes web.xml lib Compiled Java classes Additional Java libraries

Directory Structure on CS3 Application Root Directory WEB-INF JSPs and static resources classes web.xml lib Compiled Java classes Additional Java libraries www

Directory Structure of an Eclipse Dynamic Web Project Application Root Directory WEB-INF JSPs and static resources classes web.xml lib Compiled Java classes Additional Java libraries WebContent build/classes

Servlet HelloWorld import java.io.*; import javax.servlet.*; import “/HelloWorld” ) public class HelloWorld extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletExceptoin, IOException { PrintWriter out = response.getWriter(); out.println( “Hello World” ); }

Some Simple Observations Inherits from HttpServlet rvlet/http/HttpServlet.html rvlet/http/HttpServlet.html There’s no main() method doGet() Input: HttpServletRequest Output: HttpServletResponse  sent back to the client browser

Example: HelloWorld in HTML Modify the HelloWorld servlet to output in HTML

Generating HTML HttpServletResponse Set content type to “text/html” setContentType() Generate an HTML page getWriter().println() ,,...

Servlet ) Request URL / / Servlet mapping

Java Annotations Available since JDK 1.5 (Java 5) Data about a program that is not part of the program itself Can be used by compiler, VM, and other software tools for various purposes

Annotation Examples … Error protected void doGet() Suppress public List getAllUsers() { return (List ) new ArrayList(); }

… Annotation Examples Servlet mapping in Sevelet 3.x public class HelloServlet extends HttpServlet Web public class HashService public String md5( String text ) }

About Annotations An annotation may have elements The default element is value An element has a type (like a variable in Java) {} can be omitted for array values if there’s only one value in the array

@WebServlet pi/javax/servlet/annotation/WebServlet. html

@WebServlet Elements for URL Patterns value URL pattern(s) of the servlet The default element urlPatterns Same purpose as value Usually used when more than one element is specified Only one of value and urlPatterns can be specified

@WebServlet “/HelloServlet” {“/HelloServlet”, “/member/*”} name=“Hello”, urlPatterns={“/HelloServlet”, “/*.html”} urlPatterns=”/MyPattern”, value="333")} )

Be Careful with URL Patterns Invalid patterns E.g. member/index.html, or /member/*.html Conflicting patterns E.g. two /HelloServlet Overlapping patterns E.g. *.html and /member/*

Wildcard in Servlet Mapping A string beginning with a / and ending with a /* E.g. /*, /content/* A string beginning with a *. E.g. *.html, *.do See Servlet Specification 3.0, Section 12

Example: RequestCounter Display the number of times a servlet is requested You are visitor #1001.

Servlet Life Cycle When the servlet is loaded – init() Executed only once Don’t forget super.init(config) Per request – service() dispatch to doXxx() When the servlet is unloaded – destroy()

Why Use init() Instead of Constructor Historical reasons – see cysun/notes/servlet_data_init cysun/notes/servlet_data_init ServletContext cannot be accessed in a constructor

Example: SharedRequestCounter Use one servlet to count the number of requests, and another servlet to display the count

Application Scope A “storage area” where data can stored and accessed Data in application scope will remain there as long as the application is running Data in application scope is shared by all servlets

Access Application Scope HttpServlet getServletContext() HttpServletContext setAttribute(String name, Object value)  Give any object a name and save it to application scope getAttribute(String name)  Retrieve the object from application scope

loadOnStartup By default, a servlet is not created until it is accessed for the first time Could cause problem if one servlet must run before another servlet Use the loadOnStartup element to have a servlet created during application startup

loadOnStartup name=“Hello”, urlPatterns={“/HelloServlet”, “/*.html”}, loadOnStartup=1 ) The value for loadOnStartup is the order in which the application server will start the servlets.

About web.xml Web application deployment descriptor  version More about web.xml in Java Servlet Specification

Versions Servlet/JSP SpecTomcatJava 3.0/ x / x / x1.4 The version attribute of in web.xml

Debugging Servlets Read error message carefully View the source of the generated HTML View Source in browser Validation   Use the Web Developer addon of Firefox Using the Eclipse debugger Set break points Debug As  Debug on Server

Summary Standard directory structure Servlet basics Generate HTML response URL mapping Lifecyle Data sharing using application scope loadOnStartup web.xml Debugging tips