Server-Side Scripting with Java Server Page, JSP.

Slides:



Advertisements
Similar presentations
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai.
Server-Side Scripting with Java Server Page, JSP.
Java, Access, SQL, HTML. Three-tier architecture involves: Client - Browser Server - Tomcat Database - Access - Server-side language - JSP could just.
Apache Tomcat Server – installation & use Server-side language-- use Java Server Pages Contrast Client-side languages HTML Forms Servers & Server-side.
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
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.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Server-side Scripting Powering the webs favourite services.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Database Programming in Java Corresponds with Chapter 32, 33.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
Server-Side Scripting with JSP (2) ISYS 350. Java Array Examples of declaring an array: – int[] anArray = new int[10]; 10 elements index from 0 to 9 –
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Java Servlets and Java Server Pages Carol Wolf Computer Science.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Java Classes ISYS 350. Introduction to Classes Two basic uses of class: – 1. A class is a way to organize functions (methods) to perform calculations.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2.
Website Development with PHP and MySQL Saving Data.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
Jsp (Java Server Page) Is a server side program.
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Server-Side Scripting with JSP (2) ISYS 350. Post Back A postback is call to the same page that the form is on. In other words, the contents of the form.
Leman Akoglu 11/11/ Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Server-Side Scripting with JSP (2) ISYS 350. Java Array Examples of declaring an array: – int[] anArray = new int[10]; 10 elements index from 0 to 9 –
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Java Servlet ISYS 350. What is Java Servlet? It is a java class that serves a client request. Servlets are most often used to: Process or store data that.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Server-Side Scripting with Java Server Page, JSP ISYS 350.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
Java Class and Servlet ISYS 350.
Advanced Web Automation Using Selenium
AJAX and Java Servlet ISYS 350.
JSP Implicit Objects Implicit Objects in JSP are objects that are automatically available in JSP. request: The request object retrieves the values that.
Server-Side Scripting with Java Server Page, JSP
Server-Side Scripting with Java Server Page, JSP
Client Access, Queries, Stored Procedures, JDBC
Database Processing with JSP
Java Servlet ISYS 350.
JDBC Example.
Lecture 11 Database Connection
Presentation transcript:

Server-Side Scripting with Java Server Page, JSP

Hyper Text Transfer Protocol: Request & Response Web Server Browser HTTP Request HTTP Response Web Application

Data Sent with Request and Response Request: – requested URL, cookies, queryString, data from a form, etc. Response: – Web page content in HTML code – Cookies – Etc.

Methods of request Object * getCookies() * getHeader(String name) * getMethod() * getParameter(String name) * getParameterNames() * getParameterValues(String name) * getQueryString() * getRequestURI() etc.

Use JSP to Process a Form

FORM Tag Form attribute: – Method: Preferred method is: Post – Action: Specify the URL of a program on a server or an address to which a form’s data will be submitted. Example:

Methods of response Object * setContentType() * addCookie(Cookie cookie) * addHeader(String name, String value) * containsHeader(String name) * setHeader(String name, String value) * sendRedirect(String) * sendError(int status_code)

Slide 8 Declare Variables Type variableName ; Examples int myCounter; double interestRate;

Slide 9

Compute the Sum of 2 Numbers: Example of JSP scriptlet <% String value1, value2; double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; out.println("The sum is:" + sum); %> Note 1: Double is an object, not “double” data type. Note 2: “out” is a variable that does not need to be declared. It is already predefined.

Writing HTML code as output <% String value1, value2; double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; out.println(" Value1: "); out.println(" Value2: "); out.println("The sum is: " ); %>

Using JSP Expression: String value1, value2; double n1, n2, sum; value1=request.getParameter("num1"); value2=request.getParameter("num2"); n1= Double.parseDouble(value1); n2= Double.parseDouble(value2); sum=n1+n2; %> Value1: "> Value2: "> Sum is: ">

Compute Future Value: Process form with various controls

Code Example <% String myPV, myRate, myYear,myCheckbox; myPV=request.getParameter("PV"); myRate=request.getParameter("Rate"); myYear=request.getParameter("Year"); myCheckbox=request.getParameter("testCheckbox"); double FV, PV, Rate, Year; PV=Double.parseDouble(myPV); Rate=Double.parseDouble(myRate); Year=Double.parseDouble(myYear); FV=PV*Math.pow(1+Rate,Year); out.println("FutureValue is:"+ FV); %>

Database Processing with JSP Must import java.sql using page directive: – import="java.sql.*" % Need a driver: – JDBC-ODBC Bridge: The JDBC-ODBC Bridge allows applications written in the JavaTM programming language to use the JDBCTM API with many existing ODBC drivers. – Example: String DBUrl="jdbc:odbc:MySalesDB"; Define connection object: – connection = DriverManager.getConnection(DBUrl); Define Statement object: – Statement SQLStatement = connection.createStatement(); Run SQL to create resultset: – strSQL="select * from customer where cid='" + myCid + "'"; – ResultSet rs = SQLStatement.executeQuery(strSQL);

JDBC ResultSet The rows that satisfy a particular query are called the result set. The number of rows returned in a result set can be zero or more. A user can access the data in a result set using a cursor one row at a time from top to bottom. A cursor can be thought of as a pointer to the rows of the result set that has the ability to keep track of which row is currently being accessed.

ResultSet Methods Next() : Returns true if the cursor is now positioned on a row and false if the cursor is positioned after the last row. previous() first() last()

Example: Enter CID in a box and retrieve the customer record

<% Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; try { String myCid, strSQL, Cname, City, Rating; connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); myCid=request.getParameter("cid"); strSQL="select * from customer where cid='" + myCid + "'"; ResultSet rs = SQLStatement.executeQuery(strSQL); if (rs.next()) { Cname=rs.getString("CNAME"); City=rs.getString("CITY"); Rating=rs.getString("Rating"); rs.close(); out.println( Cname); out.println( City); out.println( Rating); } else { out.println("Customer not exist!"); rs.close(); } catch(SQLException e) { for (Throwable t :e) t.printStackTrace(); } finally { } %>

Create a CID ListBox

Select CID: <% Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; try { String myCid, strSQL; connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); strSQL="select cid from customer;"; ResultSet rs = SQLStatement.executeQuery(strSQL); while (rs.next()) { myCid=rs.getString("cid"); out.println(" " + myCid + " "); } catch(SQLException e) { for (Throwable t :e) t.printStackTrace(); } %>

Insert a New Record Statement object: – executeUpdate method

Adding a new customer <% Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; try { String myCid, strSQL; String CID, Cname, City, Rating; CID=request.getParameter("CID"); Cname=request.getParameter("Cname"); City=request.getParameter("City"); Rating=request.getParameter("Rating"); strSQL = "Insert into Customer values ('"; strSQL = strSQL + CID + "','" + Cname + "','"; strSQL = strSQL + City + "','" + Rating + "')"; connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); SQLStatement.executeUpdate(strSQL); out.write("Record added"); } catch(SQLException e) { out.write("Record not added"); out.write(e.getMessage()); for (Throwable t :e) t.printStackTrace(); } %>

<% Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; try { connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); strSQL="select * from customer;"; ResultSet rs = SQLStatement.executeQuery(strSQL); out.write(" "); while (rs.next()) { out.write(" "); out.write(" " + rs.getString("CID") + " "); out.write(" " + rs.getString("Cname") + " "); out.write(" " + rs.getString("City") + " "); out.write(" " + rs.getString("Rating") + " "); out.write(" "); } out.write(" "); } catch(SQLException e) { for (Throwable t :e) t.printStackTrace(); } %> Create a Table to Display Customer Records

Creating Class Classes used with a web application must be stored as a “Package”. Step 1: Create a new package – Right click src folder and select New/Java Package – Name the new package For example, myPackage Step 2: Creating new class in the package – Right click the package folder and select New/Java Class – Name the class

Customer Class Example Use a private variable to store a property’s value. private String pvCID, pvCname, pvCity, pvRating ; Use set and get method to define a property: public void setCname(String cname){ this.pvCname=cname; } public String getCname(){ return pvCname; } Note: “void” indicates a method will not return a value.

Code Example: Customer Class package myPackage; import java.sql.*; public class Customer { private String pvCID, pvCname, pvCity, pvRating ; public Customer(){} public void setCID(String cid){ this.pvCID=cid; } public String getCID(){ return pvCID; } public void setCname(String cname){ this.pvCname=cname; } public String getCname(){ return pvCname; } public void setCity(String city){ this.pvCity=city; } public String getCity(){ return pvCity; } public void setRating(String rating){ this.pvRating=rating; } public String getRating(){ return pvRating; }

public Boolean GetCustData(String cid) { Connection connection = null; String DBUrl="jdbc:odbc:MySalesDB"; Boolean RecExist=false; try { connection = DriverManager.getConnection(DBUrl); Statement SQLStatement = connection.createStatement(); String strSQL="select * from customer where cid='" + cid + "'"; ResultSet rs = SQLStatement.executeQuery(strSQL); if (rs.next()) { pvCID=rs.getString("CID"); pvCname=rs.getString("CNAME"); pvCity=rs.getString("CITY"); pvRating=rs.getString("Rating"); rs.close(); RecExist=true; } else { System.out.print("Customer not exist!"); rs.close(); RecExist=false; } catch(SQLException e) { for (Throwable t :e) t.printStackTrace(); } finally { return RecExist; }

Using Class Must import the package: import="myPackage.*" % Define a class variable: Customer C1 = new Customer();

JSP Using Class <% String custID = request.getParameter("cid"); String Cname, Rating; Customer C1 = new Customer(); if (C1.GetCustData(custID)) { Cname = C1.getCname(); Rating = C1.getRating(); } else { Cname = "NA"; Rating = "NA"; } %> CID: Cust name: Rating: