Web based Applications, Tomcat and Servlets - Lab 3 -

Similar documents
The Basic Web Server CGI. CGI: Illustration. Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 Database Management Systems 4

JAVA SERVLET. Server-side Programming INTRODUCTION

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

LAB 1 PREPARED BY : DR. AJUNE WANIS ISMAIL FACULTY OF COMPUTING UNIVERSITI TEKNOLOGI MALAYSIA

UNIT-V. Web Servers: Tomcat Server Installation:

This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

Session 8. Introduction to Servlets. Semester Project

sessionx Desarrollo de Aplicaciones en Red A few more words about CGI CGI Servlet & JSP José Rafael Rojano Cáceres

Servlet Basics. Agenda

Servlet Fudamentals. Celsina Bignoli

Supplement IV.E: Tutorial for Tomcat For Introduction to Java Programming By Y. Daniel Liang

Advanced Internet Technology Lab # 4 Servlets

Session 9. Introduction to Servlets. Lecture Objectives

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

Introduction to Servlets. After which you will doget it

HttpServlet ( Class ) -- we will extend this class to handle GET / PUT HTTP requests

Java servlets CSCI 470: Web Science Keith Vertanen Copyright 2013

Java Servlets. Preparing your System

Database Applications Recitation 6. Project 3: CMUQFlix CMUQ s Movies Recommendation System

Module 4: SERVLET and JSP

ServletConfig Interface

Servlets and JSP (Java Server Pages)

2. Follow the installation directions and install the server on ccc. 3. We will call the root of your installation as $TOMCAT_DIR

Enterprise Java Technologies (Part 1 of 3) Component Architecture. Overview of Java EE. Java Servlets

Enterprise Java Unit 1- Chapter 4 Prof. Sujata Rizal Servlet API and Lifecycle

AJP. CHAPTER 5: SERVLET -20 marks

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

Chettinad College of Engineering and Technology CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND TECHNOLOGY

Advanced Web Technology

Unit 4 - Servlet. Servlet. Advantage of Servlet

JdbcResultSet.java. import java.sql.*;

Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

&' () - #-& -#-!& 2 - % (3" 3 !!! + #%!%,)& ! "# * +,

The Servlet Life Cycle

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Servlets by Example. Joe Howse 7 June 2011

JAVA SERVLETS INTRODUCTION SERVLETS SHRI GURU RAM RAI INSTITUTE OF TECHNOLOGY & SCIENCE, DEHRADUN

4.1 The Life Cycle of a Servlet 4.2 The Java Servlet Development Kit 4.3 The Simple Servlet: Creating and compile servlet source code, start a web

Servlet. 1.1 Web. 1.2 Servlet. HTML CGI Common Gateway Interface Web CGI CGI. Java Applet JavaScript Web. Java CGI Servlet. Java. Apache Tomcat Jetty

Introduction. This course Software Architecture with Java will discuss the following topics:

Introduction to Java Servlets. SWE 432 Design and Implementation of Software for the Web

Stateless -Session Bean

3. The pool should be added now. You can start Weblogic server and see if there s any error message.

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

SERVLETS INTERVIEW QUESTIONS

Using Java servlets to generate dynamic WAP content

JavaServer Pages (JSP)

a. Jdbc:ids://localhost:12/conn?dsn=dbsysdsn 21. What is the Type IV Driver URL? a. 22.

SSC - Web applications and development Introduction and Java Servlet (I)

SWE642 Oct. 22, 2003

Servlets. An extension of a web server runs inside a servlet container

Servlets Basic Operations

Handout 31 Web Design & Development

Getting started with Winstone. Minimal servlet container

Scheme G Sample Question Paper Unit Test 2

********************************************************************

ProgramvaruUtveckling för Stora System F2

Table of Contents. Introduction... xxi

Chapter 17. Web-Application Development

Introduction to Web applications with Java Technology 3- Servlets

INTERNET PROGRAMMING TEST-3 SCHEME OF EVALUATION 1.A 3 LIFE CYCLE METHODS - 3M 1.B HTML FORM CREATION - 2 M

Unit-4: Servlet Sessions:

Enterprise Java Unit 1- Chapter 3 Prof. Sujata Rizal Introduction to Servlets

A Servlet-Based Search Engine. Introduction

Advanced Topics in Operating Systems. Manual for Lab Practices. Enterprise JavaBeans

Java4570: Session Tracking using Cookies *

ICOM 5016 Database Systems. Database Users. User Interfaces and Tools. Chapter 8: Application Design and Development.

CIS 3952 [Part 2] Java Servlets and JSP tutorial

Lab session Google Application Engine - GAE. Navid Nikaein

Introduction. This course Software Architecture with Java will discuss the following topics:

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

Introdução a Servlets

SERVLET AND JSP FILTERS

Implementation Architecture

CSC309: Introduction to Web Programming. Lecture 10

HTTP and the Dynamic Web

Implementation Architecture

Servlet. Web Server. Servlets are modules of Java code that run in web server. Internet Explorer. Servlet. Fire Fox. Servlet.

Applet. 1. init (): called once by the applet containers when an applet is loaded for execution.

The Structure and Components of

Lecture Notes On J2EE

First Servlets. Chapter. Topics in This Chapter

How does the Web work? HTTP and the Dynamic Web. Naming and URLs. In Action. HTTP in a Nutshell. Protocols. The canonical example in your Web browser

Servlets Pearson Education, Inc. All rights reserved.

Web Based Solutions. Gerry Seidman. IAM Consulting

CS506 Web Design & Development Final Term Solved MCQs with Reference

Questions and Answers

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Servlet for Json or CSV (or XML) A servlet serving either Json or CSV (or XML) based on GET parameter - This version uses org.json

Chapter 10 Servlets and Java Server Pages

Allenhouse Institute of Technology (UPTU Code : 505) OOT Notes By Hammad Lari for B.Tech CSE V th Sem

Servlet 5.1 JDBC 5.2 JDBC

Backend. (Very) Simple server examples

Introduction to Server-Side Technologies

CE212 Web Application Programming Part 3

CREATE A SERVLET PROGRAM TO DISPLAY THE STUDENTS MARKS. To create a servlet program to display the students marks

CS433 Technology Overview

Accessing EJB in Web applications

Servlet and JSP Review

Combined Java Web Example: Servlets, JDBC and Graphics

Transcription:

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

The Basic Web Server CMPUT 391 Database Management Systems 2

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) CMPUT 391 Database Management Systems 3

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

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 CMPUT 391 Database Management Systems 5

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 CMPUT 391 Database Management Systems 6

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

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 CMPUT 391 Database Management Systems 8

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 CMPUT 391 Database Management Systems 9

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: http://jakarta.apache.org/tomcat/index.html CMPUT 391 Database Management Systems 10

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: http://ugweb.cs.ualberta.ca/~c391/tutorial/softwinstall.html Test your installation CMPUT 391 Database Management Systems 11

Installation Tips Use echo $SHELL to double check your shell When editing catalina/conf/server.xml, <Server port= > is Port 2 and <Connector port= > 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) CMPUT 391 Database Management Systems 12

URL Mapping Unlike ASP, JSP, etc., servlets have no extension: <form name= FirstServlet" method="get" action="servlet/firstservlet"> 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: http://<ui???>.cs.ualberta.ca:<port1>/proj1/servlet Machine Name /proj1/servlet/firstservlet CMPUT 391 Database Management Systems 13

Mapping URL to Machine Directory Catalina Put your plain HTML files here Webapps http://csu???.cs.ualberta.ca:portnum/proj1 proj1 <form name= FirstServlet" method="get" action="servlet/firstservlet"> WEB-INF classes Put your servlets here http://csu???.cs.ualberta.ca:portnum/proj1/servlet CMPUT 391 Database Management Systems 14

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("<html>"); out.println("<body>"); out.println("<h1>welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } } http://ugweb.cs.ualberta.ca/~c391/tutorial/examples/firstservlet.java CMPUT 391 Database Management Systems 15

Compiling the Servlet Edit the file FirstServlet.java javac FirstServlet.java Start your Tomcat Launch a browser, input the URL like: http://ui00.cs.ualberta.ca:16410/proj1/servlet/firstservlet Your Port 1 CMPUT 391 Database Management Systems 16

What Does Your First Servlet Look Like? CMPUT 391 Database Management Systems 17

The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. 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("<html>"); out.println("<body>"); out.println("<h1>welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } CMPUT 391 Database Management Systems 18

The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; Implement the Servlet interface: subclassing the existing GenericServlet class that implements Servlet 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("<html>"); out.println("<body>"); out.println("<h1>welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } CMPUT 391 Database Management Systems 19

The FirstServlet In-Depth import java.io.*; import javax.servlet.*; import javax.servlet.http.*; Invoke the servlet s service method 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(); The service method takes two arguments: ServletRequest: containing info about the request from the browser. ServletRespone: containing info about the response going back. } // Write the HTML back to the browser out.println("<html>"); out.println("<body>"); out.println("<h1>welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } 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(); Tell the Web browser what will be returned is HTML. Required! } // Write the HTML back to the browser out.println("<html>"); out.println("<body>"); out.println("<h1>welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } 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("<html>"); out.println("<body>"); out.println("<h1>welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } 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. 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("<html>"); out.println("<body>"); out.println("<h1>welcome to CMPUT391 Lab!</h1>"); out.println("</body>"); out.println("</html>"); } You don t need to worry about closing the output stream when you have done. CMPUT 391 Database Management Systems 23

The HttpServlet Class It has extra methods and special requestand-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; CMPUT 391 Database Management Systems 24

The HttpServlet Class Web Browser HTTP request service Get Delete Post Put doget dopost doput dodelete 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. CMPUT 391 Database Management Systems 25

The Second Servlet Example Refer to Asn2Sample.java and Asn2Sample.html in http://ugweb.cs.ualberta.ca/~c391/tutorial/servletbasics.html Acces servlet using the HTML form Subclass the HttpServlet class rather than the GenericServlet class CMPUT 391 Database Management Systems 26

Servlet Resources Mark Wutka, Using Java Server Pages and Servlets, 2000, Que. http://java.sun.com/developer/onlinetraining/ Servlets/Fundamentals/contents.html http://java.sun.com/webservices/docs/1.0/tuto rial/doc/servlets.html Other resources: http://ugweb.cs.ualberta.ca/~c391 CMPUT 391 Database Management Systems 27

Servlet Exercise create and populate a table called CMPUT in your Oracle account At least 2 columns(courseid and CourseNAME) & 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 CMPUT ;) 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 ) CMPUT 391 Database Management Systems 28