CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,

Slides:



Advertisements
Similar presentations
Java Servlets Java Server Pages (JSP)
Advertisements

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 Stoney Jackson
10-Jun-15 Servlets. 2 Servers A server is a computer that responds to requests from a client Typical requests: provide a web page, upload or download.
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:
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.
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.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
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.
Server Side Programming Web Information Systems 2012.
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.
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.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
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.
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.
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.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
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.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
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.
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.
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 Servlets Java Server Pages (JSP)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
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.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Java Servlets References: Karen Anewalt, Mary Washington College.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Course Outcomes of Advanced Java Programming AJP (17625, C603)
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.
Java Servlets and JSP.
CS122B: Projects in Databases and Web Applications Winter 2019
Introduction to Servlet
Basic servlet structure
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications, Tomcat and Servlets - Lab 3 -

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 2 The Basic Web Server

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 3 CGI  CGI – Common Gateway Interface  Processing forms & Generating dynamic content (early solution )  Spawning an external program, pass the data from the HTML form to the program  Limitation: An expensive process (Spawning an external program)

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 4 CGI: Illustration CGI Program Browser Web Server CGI Program Browser Web Server Start form.cgi Browser Web Server CGI Program Browser Web Server CGI Program form.cgi terminates GET Form.cgi Spawn an external program Read environment variables Response time

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 5 ASP  ASP – Active Server Pages  Microsoft’s answers to CGI programming  ASP page - a Web page with code embedded inside, interpreted by the Web server  Using special HTML tags, VBScript/JavaScript code can be inserted

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 6 Servlets  Java technology's answer to CGI programming  A Java class handling forms on Java Web servers  Applet -- a little piece of program (client side)  Servlet -- a little piece of program (server side)  A servlet can do everything that a CGI program can  Running inside the JVM along with the Web server itself

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 7 Servlets: Illustration Browser GET /servlet/Form Create an Instance of the servlet if it is not loaded Java Web Server Form Servlet Request Java Web Server Form Servlet Response Form Servlet Java Web Server Remains loaded time

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 8 Servlets Support in Existing Servers  Apache Web Server & some other Java-enabled Web servers  Servlet engine Usually a separate process, not within the same process at the Web server e.g. Tomcat

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 9 Typical Servlet Engine Implementation  Tomcat A Servlet Engine for Apache Web Server dispatcher Servlet 1 Servlet 2 Servlet N Custom TCP/IP Protocol API Web Server Browser HTTP traffic Servlet Engine

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 10 JSP  JSP – Java Server Pages - Java Version of ASP - A Web page with Java code embedded inside it and running on the Web server  Java’s answer to both CGI and ASP  Compiled into servlets  JSP & servlets can easily interact

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 11 Tomcat  Servlet &JSP engine for Apache Web Server  An open-source package part of the Apache Software Foundation’s Jakarta project  The Tomcat Web site: –

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 12 Installing Tomcat  Setting the CLASSPATH: the same classpath settings as your JVM  Change the CLASSPATH environment variable from your login shell –Follow the instructions in:  Test your installation

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 13 Installation Tips  Use “echo $SHELL” to double check your shell  When editing catalina/conf/server.xml, is Port 2 and is Port 1  Use “Port 1” when you input the URL  Take advantage of the alias to start and stop Tomcat (commands are the same in Bourne shell and C shell)

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 14 URL Mapping  Unlike ASP, JSP, etc., servlets have no extension:  A special URL mapping for the servlet directory, which signals that you want to run a servlet.  In our lab, the special URL is like: /proj1/servlet Machine Name …/proj1/servlet/FirstServlet

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 15 Mapping URL to Machine Directory classes WEB-INF proj1 Webapps Catalina Put your plain HTML files here Put your servlets here <form name=“FirstServlet" method="GET" action="servlet/FirstServlet">

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 16 Your First Servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web server that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println(" "); out.println(" Welcome to CMPUT391 Lab! "); out.println(" "); }

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 17 Compiling the Servlet  Edit the file “FirstServlet.java”  javac FirstServlet.java  Start your Tomcat  Launch a browser, input the URL like:

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 18 What Does Your First Servlet Look Like?

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 19 The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println(" "); out.println(" Welcome to CMPUT391 Lab! "); out.println(" "); } The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets.

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 20 The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println(" "); out.println(" Welcome to CMPUT391 Lab! "); out.println(" "); } Implement the Servlet interface: subclassing the existing GenericServlet class that implements Servlet

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 21 The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println(" "); out.println(" Welcome to CMPUT391 Lab! "); out.println(" "); } Invoke the servlet’s service method The service method takes two arguments: ServletRequest: containing info about the request from the browser. ServletRespone: containing info about the response going back.

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 22 The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println(" "); out.println(" Welcome to CMPUT391 Lab! "); out.println(" "); } Tell the Web browser what will be returned is HTML. Required!

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 23 The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println(" "); out.println(" Welcome to CMPUT391 Lab! "); out.println(" "); } The response object has the methods necessary to get an output stream for writing a response. A general rule of thumb: Writing out text: use a PrintWriter object by calling getWriter() Sending binary data to the browser: use a ServletOutputStream by calling getOutputStream() Some content types require a slightly different character set, and the PrintWriter object automatically adjusts the character set based on the content type.

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 24 The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws IOException { // Tell the Web browser that the response is HTML response.setContentType("text/html"); // Get the Printwriter for writing out the response PrintWriter out = response.getWriter(); // Write the HTML back to the browser out.println(" "); out.println(" Welcome to CMPUT391 Lab! "); out.println(" "); } You don’t need to worry about closing the output stream when you have done.

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 25 Servlet Life Cycle Initializes the servlet instance -run init method Servlets accept request from Clients and return data back - run service method If the engine needs to remove the servlet, it finalizes the servlet - run destroy method Loads the servlet (if it has not yet been loaded)

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 26 The Servlet Interface Hierarchy Servlet Interface GenericServlet Class HttpServlet Class Your servlet class Definition of The Servlet Interface Package javax.servlet; Public interface Servlet { public void destroy(); public ServletConfig getServletConfig(); public String getServletInfo(); public void init (ServletConfig config) throws ServletException; public void service (ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException; }

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 27 The HttpServlet Class  It has extra methods and special request- and-response objects that are geared toward the HTTP protocol  The HttpServlet provides separate methods for handling the different type of HTTP requests. The two most common ones are GET and POST: –public void doGet(HTTPServletRequest request, HTTPServletResponse response) throws ServletException, java.io.IOException; –public void doPost(HTTPServletRequest request, HTTPServletResponse response) throws ServletException, java.io.IOException;

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 28 The HttpServlet Class service doGet doPost doPut doDelete Web Browser Get Post Put Delete The doGet, doPost, doPut, doDelete methods are of the same general form as the service method. The service method of HttpServlet looks at the type of the HTTP request and then calls the appropriate handler methods. HTTP request

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 29 The Second Servlet Example  Refer to Asn2Sample.java and Asn2Sample.html in  Acces servlet using the HTML form  “Subclass” the HttpServlet class rather than the GenericServlet class

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 30 Servlet Resources  Mark Wutka, Using Java Server Pages and Servlets, 2000, Que.  Servlets/Fundamentals/contents.html  rial/doc/Servlets.html  Other resources: –

Lab 3 Tomcat and Servlets CMPUT 391 – Database Management Systems 31 Servlet Exercise  create and populate a table in your Oracle account (At least 2 columns & 4 records)  extend Example 3.3 in servletbasics.html –servlet should connect to Oracle database –execute the SQLStatement received from the browser (e.g. select * from “YourTable”;) –display ResultSet on the browser(Tip: use ResultSetMetaData presented in JDBC tutorial) –display an error if the SQL statement was not executed correctly in Oracle (e.g. select * from “Wrong table”)