Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.

Slides:



Advertisements
Similar presentations
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
Java Servlet & JSP © copyright 2005 SNU OOPSLA Lab.
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
Servlets and JDBC. Servlets A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password:
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
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password:
Introduction to Servlet & JSP
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
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.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
Java Servlet. What is Java Servlet? A Servlet is a java based server side web technology. It is a java class that serves a client request and receives.
CSCI 6962: Server-side Design and Programming History and Background.
Servlet and JSP Programming: An Introduction Spiros Papadimitriou
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Mark Dixon Page 1 5 – Persistent data storage: relational databases.
Examples of Using Servlets and JSP Representation and Management of Data on the Internet.
Session Beans Overview EJB container Remote Invocation Represent client's access to app Local / Remote speed / flexibility trade-off when to choose local.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
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.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Mark Dixon 1 12 – Java Beans. Mark Dixon 2 Session Aims & Objectives Aims –To cover the use of Java Beans Objectives, by end of this week’s sessions,
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 2.
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.
MDCFUG Is Java in Your Future? Tyler Williams Principal dataTerrace
Java Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
The Simplest Possible Tool - Iteration 1 Vancouver Bootcamp Mike Osterman
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
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.
Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet.
Top right corner for field-mark, customer or partner logotypes. See Best practice for example. Slide title 40 pt Slide subtitle 24 pt Text 24 pt Bullets.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
Hints for Assignment #8. Initial Screen Hints for the initial page You need a DOCTYPE with the proper namespaces defined You need to import the facebook.
 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business.
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.
Vakgroep Informatietechnologie – Onderzoeksgroep (naam) Web Centric Design of Distributed Software.
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java and the Web CSE 3330 Southern Methodist University.
ClaRA web services V. Gyurjyan Clas12 Software Meeting
How CGI and Java Servlets are Run By David Stein 14 November 2006.
OOSD Using Java CBTS Framework. 11/2/04CBTS2 Servlet  A servlet is a Java program that can extends Web server’s functionality.  Servlets interact with.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Introduction to Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
In Class Assg 4 - Solution
In Class Assg 3 - Solution
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
In Class Assg 2 - solution
COP 4610L: Applications in the Enterprise Spring 2005
Introduction to Java Servlets
Presentation transcript:

Mark Dixon 1 11 – Java Servlets

Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end of this week’s sessions, you should be able to: –create a servlet –use a class to gather code common to different pages

Mark Dixon 3 most application programs – 3 major layers –Top (Presentation) layer: human/machine interaction (the user interface) –input from the keyboard / mouse –output in the form of screen displays / sound –Middle (Application or business logic) layer: core functionality – gives application program its character contains business rules -> drive an organisation e.g. order entry system vs. inventory control system –Bottom layer general services needed by other layers e.g. file, print, communications, and database services 3 Application Layers

Mark Dixon 4 2-Tier Architecture Presentation and Application layer located on client machine –could be implemented using Applet interacting server Known as a ‘fat client’

Mark Dixon 5 3-Tier Architecture 3-tier architecture, –only presentation layer on client –application layer on server –Database on server or third machine Known as a ‘thin-client’ –very little (application) code / processing on client e.g. use of Java Servlets (JSP pages)

Mark Dixon 6 Example: AddNum (JSP) <% double N1; double N2; String Res = ""; if (request.getParameter("btnAdd") != null){ N1 = Double.parseDouble( request.getParameter("txtN1") ); N2 = Double.parseDouble(request.getParameter("txtN2")); Res = Double.toString(N1 + N2); } %> Add Numbers AddNum.jsp Java - functionality HTML – user interface

Mark Dixon 7 JSP pages & Servlets all JSP pages converted to servlet Servlet –Java program running in web server –Special type of Java class (.java file) Can get servlet error – caused by error in JSP page (usually missing } ), but difficult to see the connection

Mark Dixon 8 AddNum: Servlet (.html file) Split –User interface (html) –Functionality (Java) Add Numbers Points to Servlet (.java)

Mark Dixon 9 AddNum: Servlet (.java file) import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AddNum extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { double N1; double N2; String Res = ""; response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { N1 = Double.parseDouble(request.getParameter("txtN1")); N2 = Double.parseDouble(request.getParameter("txtN2")); Res = Double.toString(N1 + N2); out.println(" "); out.println(" Add Numbers "); out.println(" "); out.println(Res); out.println(" "); } finally { out.close(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); public String getServletInfo() { return "Short description"; } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { double N1; double N2; String Res = ""; response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { N1 = Double.parseDouble(request.getParameter("txtN1")); N2 = Double.parseDouble(request.getParameter("txtN2")); Res = Double.toString(N1 + N2); out.println(" "); out.println(" Add Numbers "); out.println(" "); out.println(Res); out.println(" "); } finally { out.close(); } Calculation code Also, write html

Mark Dixon 10 Example: PeopleList.jsp v2 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; String id; while(r.next()){ id = Integer.toString(r.getInt("PersonID")); html += " "; html += r.getString("Surname") + " "; } cn.close(); %> Connect to db

Mark Dixon 11 Example: Person.jsp v2 <% String id = request.getParameter("id"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person WHERE PersonID = " + id + ";"); String surname = ""; if(r.next()){ surname = r.getString("Surname"); } cn.close(); %> Person Surname: " /> Connect to DB

Mark Dixon 12 Person & PeoplList v2 both JSP page duplicate common code <% String id = request.getParameter("id"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person WHERE PersonID = " + id + ";"); String surname = ""; if(r.next()){ surname = r.getString("Surname"); } cn.close(); %> Person Surname: " /> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; String id; while(r.next()){ id = Integer.toString(r.getInt("PersonID")); html += " "; html += r.getString("Surname") + " "; } cn.close(); %>

Mark Dixon 13 Class People Contains common code for both pages People cn st r Open Select Close

Mark Dixon 14 Class: People.java 1 Common code package Main; import java.sql.*; public class People{ private Connection cn; private Statement st; private ResultSet r; public void Open(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); } catch (Exception e){ //e.printStackTrace(); } public void Select(String sql){ try{ st = cn.createStatement(); r = st.executeQuery(sql); } catch (Exception e){ //e.printStackTrace(); } public boolean Next(){ boolean found = false; try{ found = r.next(); } catch (Exception e){ //e.printStackTrace(); } return found; } public String get(String id){ String s = ""; try{ s = r.getString(id); } catch (Exception e){ //e.printStackTrace(); } return s; } public void Close(){ try{ cn.close(); } catch (Exception e){ //e.printStackTrace(); } package Main; import java.sql.*; public class People { private Connection cn; private Statement st; private ResultSet r; public void Open (){ try{ Class.forName(" sun.jdbc.odbc.JdbcOdbcDriver "); cn = DriverManager.getConnection(" jdbc:odbc:PeopleDB ", "", ""); } catch (Exception e){ //e.printStackTrace(); } public void Select (String sql ){ try{ st = cn.createStatement (); r = st.executeQuery(sql ); } catch (Exception e){ //e.printStackTrace(); }

Mark Dixon 15 Class: People.java 2 Common code package Main; import java.sql.*; public class People{ private Connection cn; private Statement st; private ResultSet r; public void Open(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); } catch (Exception e){ //e.printStackTrace(); } public void Select(String sql){ try{ st = cn.createStatement(); r = st.executeQuery(sql); } catch (Exception e){ //e.printStackTrace(); } public boolean Next(){ boolean found = false; try{ found = r.next(); } catch (Exception e){ //e.printStackTrace(); } return found; } public String get(String id){ String s = ""; try{ s = r.getString(id); } catch (Exception e){ //e.printStackTrace(); } return s; } public void Close(){ try{ cn.close(); } catch (Exception e){ //e.printStackTrace(); } public boolean Next (){ boolean found = false; try{ found = r.next (); } catch (Exception e){ //e.printStackTrace(); } return found; } public String get(String id){ String s = ""; try{ s = r.getString (id); } catch (Exception e){ //e.printStackTrace(); } return s; } public void Close (){ try{ cn.close() ; } catch (Exception e){ //e.printStackTrace(); }

Mark Dixon 16 PersonList.jsp Class complex Pages simpler <%! People p = new People(); %> <% String html = ""; p.Open (); p.Select ("SELECT * FROM Person;"); while( p.Next ()){ html += p.get ("Surname") + " "; } p.Close (); %> People Import Package Use methods Create Instance