Java Servlets: A QuickStart for CS Educators Dawn Wilkins and Kathy Gates The University of Mississippi.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Advertisements

4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
M-V-C for web applications. Model for Web Applications model consists of data and system state database tables –persistent data session information –current.
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)
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Servlets and a little bit of Web Services Russell Beale.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
1 CS6320 – Why Servlets? L. Grewe 2 What is a Servlet? Servlets are Java programs that can be run dynamically from a Web Server Servlets are Java programs.
Servlets CS-328 Dick Steflik. What is a servlet A Java application run on a thread of the webserver in response to an HTTP GET or POST request. The servlet.
Java Server Team 8. Overview What is a Java Server? History Architecture Advantages Disadvantages Current Technologies Conclusion.
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. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
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.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Java Servelets. What Is a Servlet? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed.
Chapter 17 - Deploying Java Applications on the Web1 Chapter 17 Deploying Java Applications on the Web.
Objectives Java Servlet Web Components
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
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.
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.
Webcommerce Computer Networks Webcommerce by Linnea Reppa Douglas Martindale Lev Shalevich.
Java Servlets & Java Server Pages Lecture July 2013.
Chapter 6 Server-side Programming: Java Servlets
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
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.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
L.MARIA MICHAEL VISUWASAM UNIT-4
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.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
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.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
WWW and HTTP King Fahd University of Petroleum & Minerals
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Chapter 26 Servlets.
CS122B: Projects in Databases and Web Applications Winter 2018
CS122B: Projects in Databases and Web Applications Spring 2018
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

Java Servlets: A QuickStart for CS Educators Dawn Wilkins and Kathy Gates The University of Mississippi

Workshop Description l This workshop is designed to give CS educators, who have a working knowledge of Java, a good introduction to Java servlets. The servlet architecture and life cycle will be explained, and examples will be provided to demonstrate the use of the Java Servlet API. Advanced features including accessing databases through a servlet and session tracking will be covered. Setting up Java servlet development environment for students will also be addressed.

Contents l Background l Java Servlet Basics l A Comparison of Servlets & CGI l The Servlet Architecture l A Few Example Servlets l IDE’s & Server Options l Servlet Basics: A Closer Look l Servlets & JDBC l Session Tracking l Java Server Pages l Practical Applications l Resources

Software Demonstration Setup l Windows NT l Internet Explorer l Borland Jbuilder l Apache with Tomcat l Mysql Database – l PHP & phpMyAdmin

A Historical Perspective l First efforts at dynamic Web content –Common Gateway Interface (CGI) –Fast CGI l Proprietary Solutions –Microsoft’s Active Server Pages –Netscape’s Javascript

CGI: What is it? l A standard interface between a Web server and an application. l One of the first practical ways for generating dynamic content. l Runs as a separate process from the Web server. Common Gateway Interface (CGI) programs can be written in many languages, including Java. Perl is the most common language for CGI's, due to its advanced text processing capabilities.

Interaction with CGIs (From Developing Java Servlets: The Authoritative Solution by James Goodwill, p. 8)

An Example CGI Program l Hello World Hello World –See demonstration.

Common Gateway Interface l Advantages –Easy to get started –Simple programming interface –Popular scripting languages such as Perl offer powerful way to quickly develop new applications –Widely used l Disadvantages –New process is started for each CGI call –Separate process means limited interaction with Web server and possible loss of communication –May hit operating system limits on number of processes

Java Servlet Basics l First there was the applet -- high hopes, but probably did not reach expected potential. –Slow to load –Not operable on all browsers –Security issues l So, what is a servlet? –Java embedded in a Web server to extend the Web server's capabilities. –Whereas applets run in the Web browser, servlets run in the Web server. l Terms to Note: –Container vs. Engine –Filtering vs. Chaining –Contexts vs. Zones

Interaction with Servlets (From Developing Java Servlets: The Authoritative Solution by James Goodwill, p. 6)

Advantages of Servlets l Adds Persistence to a Stateless Web –The servlet's thread doesn't have to terminate once it sends back a response. l Platform Independent –Servlets port easily to other systems with JVM. l Efficient –Servlets are loaded once instead of each time they are executed. –The servlet is first loaded via the init() method and can perform potentially costly start-up functions once.

More Advantages l Function as Direct Extensions of the Web Server –Supports interaction with the server itself l Gaining Wide-Spread Acceptance l Built-in Support for Many Server Features –The developer can be free of certain server details. l Extensible –Object-oriented approach facilitates new development.

Disadvantages of Servlets l Servlets can be more complicated to set up. l Servlets must be recompiled whenever a change is made. l On their own, servlets do not provide a way to embed code into HTML documents such as with ColdFusion or PHP; however, this is addressed with JSP.

Guidelines for Choosing When should you use Servlets instead of CGI? l Nature of the Application –What is the hardware/software platform? –How long-term is it? –How much development time is available? –Who will maintain the application? –What other information systems does it interface with? –How many hits do you anticipate?

A Review of HTTP l Simple & Stateless l The client makes a request and the Web server responds. l The client request includes: –an HTTP command or “method” that tells what action to perform –a URL –the version of the HTTP protocol it is using. Hypertext Transfer Protocol (HTTP)

HTTP Responses l The server processes the request and sends back a response. l The first line of the response specifies: –the version of the HTTP protocol the server is using, –a status code, and –a description of the status code. For example, 200 means successful. The server also sends the client info telling what kind of software it is running and the content of the response.

HTTP Methods l GET is generally used by a client to request a resource from the server. l POST is normally used to pass user input to the server. The two most popular HTTP methods are GET & POST.

The GenericServlet Request (From Developing Java Servlets: The Authoritative Solution by James Goodwill, p. 13)

Generic Servlet API Methods l The interface javax.servlet.Servlet provides the framework for all servlets through five methods. l * init() –Servlet is constructed and initialized. l getServletConfig() –Returns startup configuration and initialization parameters. l * service() –Receives two objects: ServletRequest and ServletResponse l getServletInfo() –Provides the servlet user with information about the servlet itself. l * destroy() –Signifies the end of the servlet’s life. * Life cycle methods

Servlet Requests & Responses l ServletRequest allows access to names of parameters passed by the client, the protocol, etc. l ServletResponse allows the servlet to set the content length & MIME type of the reply and provides a means by which the servlet can send the reply data. The service() method accepts two parameters: ServletRequest ServletResponse.

Generic vs. HTTP Servlets l The HttpServlet Class is extended from GenericServlet. l Generic servlets should override the service() method; however, service() is already implemented in HttpServlet. l HttpServlets override doGet() to handle GET requests and doPost() to handle POST requests. l Other HttpServlet methods are also available.

The HttpServlet Request (From Developing Java Servlets: The Authoritative Solution by James Goodwill, p. 14)

An Example Servlet l Hello World Hello World –See demonstration.

Skeleton Servlet public class skeletonServlet extends HttpServlet { // declaration of instance variables public void init (ServletConfig config) throws ServletException { super.init (config); // initialize resources here }

Skeleton Servlet, part 2 public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {... } public void doPost (HttpServletRequest request, HttpServletResponse response) { … }

Skeleton Servlet, part 3 public String getServletInfo () { return “The purpose of this servlet is …” } public void destroy () { { // free resources here } } // skeletonServlet

Servlets & HTML l Creating HTML Documents –setContentType() –getWriter() l Retrieving Form Data –getParameterNames() –getParameter() –getParameterValues() l Invoking the Servlet

More Servlet Examples l Vacation Example Vacation Example –See demonstration. l Quiz #1 Quiz #1 –See demonstration.

Java IDE’s & Servlets l IDE –Integrated Development Environment l Examples –Borland Jbuilder –IBM Visualage l Servlet Aids –Automatic Code Generation –Test Environment

Servlet-Enabled Web Servers l Apache with Tomcat l iPlanet l IBM Websphere l Other See for a more comprehensive list.

The Servlet Specification Created from a Sun- sponsored open process to develop and revise Java TM technology specifications l Newest servlet specification includes: –Concept of a Web Application (Servlets, JSP, HTML, Classes, other resources) with conventions for the directory structure –Web Application Archive (WAR) files –Filtering l Allows on-the-fly transformations of HTTP content (request or response); for example, to transform XML content –Other

Servlet Basics: A Closer Look l The ServletRequest Interface –Defines an object used to encapsulate information about the client’s request. –Includes information about parameter name/value pairs. l The ServletResponse Interface –Defines an object for sending MIME data back to the client from the servlet’s service() method.

HttpServletRequest Interface l A Few Methods –getAuthType() –getCookies() –getDateHeader() –getHeader() –getHeaderNames() –getIntHeader() –getMethod() –getPathInfo() –getPathTranslated() –getQueryString() –getRemoteUser() –getRequestedSessionId() –getRequestedURI() –getServletPath() –getSession()

l Static Final Variables –For example, SC_OK represents a status code of 200, and SC_NOT_FOUND represents a status code of 404. HttpServletResponse Interface l A Few Methods –addCookie() –encodeURL() –sendError() –sendRedirect() –setDateHeader() –setHeader() –setStatus()

Servlet Tips l Place costly functions in init(). Let doGet() call doPost() or vice versa: public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doGet(req, res); } l Be careful with multiple threads; SingleThreadModel may be necessary.

JDBC l Java Database Connectivity l Relational Databases l SQL Refresher –CREATE –INSERT –UPDATE –DELETE –SELECT

JDBC Features l Easy Object to Relational Mapping l Database Independence l Distributed Computing l A Java API for: –Opening a connection to a database –Executing a SQL statement –Processing the results –Closing the connection to the database

Basic JDBC Methods l Establishing a connection with getConnection() l Creating SQL Statements –CreateStatement() l executeQuery() l executeUpdate() l execute() l Getting the results with ResultSet l SQLExceptions

A Simple Servlet with JDBC l HowFar –See demonstration.

Session Tracking l HTTP is stateless, so no inherent way to determine that a series of requests all come from the same client. l Think about a shopping cart application example.

Possible Solutions l Traditional CGI Techniques –User-authorization –Hidden form fields –URL rewriting –Persistent cookies l Servlet API Built-in Support

Session-tracking API support l Uses persistent cookies where possible and reverts to URL rewriting when cookies do not work. l Each user is associated with a javax.servlet.http.HttpSession object. Information about the user can be stored in the session object, e.g. shopping cart contents or database connection.

Session Tracking Example l Quiz #2 Quiz #2 –See demonstration.

Java Server Pages l Supports embedded scripting and tags. These co-exist with HTML. l Provide a means for separating content and presentation. l Reuse and code sharing are enhanced when JSP is used with JavaBeans. l Steps of a JSP Request –Client requests a JSP page. –The JSP engine compiles the JSP into a servlet. –The generated servlet is compiled and loaded. –The compiled services the request and sends a response back to the client. from Pure JSP by Goodwill

JSP Directives l Page –A global definition that is sent to the JSP engine Ex: l Include –Allows the substitution of text or code Ex: l Declarations –Contains Java variables and methods called from an expression block. Syntax: l Scriptlets –Used to embed small code blocks. l Comments –Two forms: l Expressions –Scriptlet fragments whose results can be converted to String objects.

HowFar as a JSP file l HowFar.jsp HowFar.jsp –See demonstration.

JSP and JavaBeans l Supports the creation of dynamic content l JavaBeans –Reusable software components that can be manipulated visually in a builder tool –Minimum Requirements: l Supports JDK 1.1 and later Serialization Model l Uses Get/Set accessors to expose its properties l Tags –jsp:useBean l Declares the JavaBean object that you want to use within the JSP –jsp:getProperty l Inserts the String type of the object into the output stream –jsp:setProperty l Sets properties of the Bean

HowFar2 as a JSP file with a JavaBean l HowFar2.jsp HowFar2.jsp –See demonstration.

Practical Applications l Useful for serving as the “glue” to connect heterogeneous information systems that require a web interface. l XML makes this scenario even more attractive. l University of Mississippi IT Applications –E-forms l Requires data from Cold Fusion/SQL Server and SAP via Java Connector –Web-to-SAP l Business Connector l Java Connector

Servlet Resources l See accompanying handouts.

For More Information Dawn Wilkins Department of Computer Science University of Mississippi (662) Kathy Gates Office of Information Technology University of Mississippi (662)