CS3220 Web and Internet Programming Introduction to Java Servlets

Slides:



Advertisements
Similar presentations
Apache Tomcat as a container for Servlets and JSP
Advertisements

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/
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,
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
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.
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.
Objectives Java Servlet Web Components
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.
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.
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Distributed Web Systems Java Servlets Lecturer Department University.
Introduction to Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Net-centric Computing
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Servlets Hits Counter 20-Jul-18.
Pre-assessment Questions
CS320 Web and Internet Programming MVC Architecture
Objectives In this lesson you will learn about: Need for servlets
COP 4610L: Applications in the Enterprise Spring 2005
COP 4610L: Applications in the Enterprise Spring 2005
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.
Introduction to Servlet
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:

CS3220 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 JSPs and static resources WEB-INF web.xml classes Compiled Java classes lib Additional Java libraries

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

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

Servlet HelloWorld import java.io.*; import javax.servlet.*; import javax.servlet.http.*; @WebServlet( “/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 http://docs.oracle.com/javaee/7/api/javax/servlet/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” Generate an HTML page getWriter().println() <html>, <head>, <body> ...

Servlet Mapping @WebServlet(<URL Pattern(s)>)

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 detection Suppress warning @Override protected void doGet() Suppress warning @SuppressWarnings(“unchecked”) public List<User> getAllUsers() { return (List<User>) new ArrayList(); }

… Annotation Examples Servlet mapping in Sevelet 3.x Specification @WebServlet(“/HelloServlet”) public class HelloServlet extends HttpServlet Web service @WebService public class HashService { @WebMethod public String md5( String text ) }

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

@WebServlet http://docs.oracle.com/javaee/7/api/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 Examples @WebServlet( “/HelloServlet” ) @WebServlet( {“/HelloServlet”, “/member/*”} ) @WebServlet( name=“Hello”, urlPatterns={“/HelloServlet”, “/*.html”} ) @WebServlet( urlPatterns=”/MyPattern”, initParams={@WebInitParam(name="ccc", value="333")} )

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

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

Example: RequestCounter Display the number of times a servlet is requested

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 http://csns.calstatela.edu/wiki/content/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 of @WebServlet to have a servlet created during application startup

loadOnStartup Example @WebServlet( 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 <welcome-file-list> More about web.xml in Java Servlet Specification

Versions The version attribute of <web-app> in web.xml Servlet/JSP Spec Tomcat Java 3.1/2.3 8.0.x, 8.5.x 1.7 3.0/2.2 7.0.x 1.6 2.5/2.1 6.0.x 1.5 2.4/2.0 5.5.x 1.4 The version attribute of <web-app> in web.xml

Debugging Servlets 404 Errors: check URL and URL mapping Display Problems: check the source of the generated HTML View Source in browser, or Use an HTML Validator Logical errors: use the Eclipse debugger Set break points Debug As  Debug on Server