Exercises of the Tutorial on Advanced Web Programming Authors: Miroslava Mitrovic Dragan Milicev Nino.

Slides:



Advertisements
Similar presentations
Servlets. Servlets are modules that extend Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry.
Advertisements

4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
16 Copyright © 2005, Oracle. All rights reserved. Using JDBC to Access the Database.
JSP and Servelets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
JLab Lattice Portal – Data Grid Web Service Ying Chen, Chip Watson Thomas Jefferson National Accelerator Facility.
Java II--Copyright © Tom Hunter. Chapter 18 JDBC.
Database By: Greg 4/10/2017 Java 9 - GUI.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Apache Tomcat as a container for Servlets and JSP
Java Server Pages (JSP)
Java Servlets Java Server Pages (JSP)
Chapter 25 JavaServer Pages and Servlets. CHAPTER GOALS To implement dynamic web pages with JavaServer Pages technology To learn the syntactical elements.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
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 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
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.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
© Wang Bin 2004 JDBC ----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.
Examples of Using Servlets and JSP Representation and Management of Data on the Internet.
Java Servlets and Java Server Pages Carol Wolf Computer Science.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
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.
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:
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
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.
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.
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.
Chapter 8 Databases.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.
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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business.
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.
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.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
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.
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.
CS3220 Web and Internet Programming Database Access with JDBC
JDBC & Servlet CSE 4504/6504 Lab.
CS320 Web and Internet Programming Database Access with JDBC
HW#4 Making Simple BBS Using JDBC
Objectives In this lesson, you will learn about:
Chapter 26 Servlets.
Interacting with Database
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
Bolat Azamat, Kim Dongmin
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Exercises of the Tutorial on Advanced Web Programming Authors: Miroslava Mitrovic Dragan Milicev Nino Stojcic Veljko Milutinovic

Exercise 1: Develop Your Own HTML Web Form Design a web form that contains the following controls: - Name (Text box) - Address (Text box) - Age (Text box) - Mr. / Mrs. / Miss (Radio button group) - Reset and Submit buttons

< ! Exercise1.html Exercise1 Exercise1: Name:&nbsp Address:

Age: Mr. Mrs. Miss

Exercise 2: Validate Your Form’s Data Enhance the form from Exercise1 so that the user cannot submit the Form if the Name field is empty or the Age field contains a negative number (provide a message in these cases). Validation upon pressing the submit button

<! Exercise2.html ……………. <!— function checkData (theForm){ var ReturnVal=false var name=theForm.Name.value var address=theForm.Address.value var age=Number(theForm.Age.value)

if (name=="") alert("Name must not be empty!") else if (address=="") alert("Address must not be empty!") else if (isNaN(age)) alert("Age must be non negative number!") else if (age<0) alert("Age must be non negative number!") else ReturnVal=true if (ReturnVal) alert("Your order has been submitted") return ReturnVal } //--> ………………………….

Exercise 3: Make Your Web Form Live Make your web form alive, by adding a simple applet to your web form that will demonstrate the possibility of creating dynamic contents. Using a scrolling box

<! Exercise3.html …………………. ……………

Exercise 4: Develop Your Own Servlet Develop a servlet that accepts the submitted page from Exercise 3, and returns a page with the following contents to the user: “Hello, glad to meet you. I’ll stay in contact with you by ing to the address:. “

<! Exercise4.html ………… //--> Name: ……………..

// Exercise4Servlet. Java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class Exercise4Servlet extends HttpServlet{ //overloading the doPost() method inherited from HttpServlet class public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{ //setting the content type of response to "text/html" res.setContentType("text/html"); //PrintWriter converts Java's Unicode characters to locale-specific encoding //For an English locale, it behaves same as a PrintStream PrintWriter out = res.getWriter();

String name1=req.getParameter("Name"); String address= req.getParameter("Address"); String mrMrsMiss=req.getParameter("Group1"); out.print( " Exercise4 "+ " Exercise4: "+ " " + " Servlet Response: "+ " Hello "+mrMrsMiss+" “ + name1 + ", glad to meet you! I'll contact you by ing to the + "address: “+address + " "); out.close(); }

Exercise 5: Make Your Own Application Access the Database Enhance the servlet from Exercise 4, so that it inserts a new record into the database table of the users with the submitted data, before returning the “Hello…” confirmation page.

// Exercise5Servlet.java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import sun.jdbc.odbc.*; public class Exercise5Servlet extends HttpServlet{ public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { String status ="nix"; res.setContentType("text/html"); PrintWriter out = res.getWriter(); String name1=req.getParameter("Name"); String address= req.getParameter("Address");

String mrMrsMiss=req.getParameter("Group1"); String age=req.getParameter("Age"); Connection con=null; try{ //load the JdbcOdbcDriver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Exercise5Base"; //get a connection to the database con = DriverManager.getConnection(url,"Exercise5Base", "sql"); //create a statement object Statement stmt = con.createStatement(); //execute an sql query String sql = "Insert into Members (Name,Address,Age,Title) values"+ "('"+ name1 +"','"+ address +"','"+ age +"','"+ mrMrsMiss +"')" ; stmt.execute(sql); }

catch(ClassNotFoundException e){ System.out.println("Couldn't load database driver: " + e.getMessage()); } catch(SQLException e){ System.out.println("SQLException caught: " + e.getMessage()); } //close the database connection finally { try{ if (con!=null) con.close(); } catch (SQLException ignored){} }

out.print( " Exercise5 "+ " Exercise5: "+ " " + " Servlet Response: "+ " Hello "+mrMrsMiss+" "+name1+ ", glad to meet you! I'll contact you by ing to the” + address: "+ address + " ") ; out.close(); }

Exercise 6: Develop Your First Simple Web Application Using the given infrastructure, develop an application : Select a user from the database by his/her name in the list box, modify data for the selected user, using the page from Exercise 5,

Exercise 6: Develop Your First Simple Web Application and on the “submit” command go to the confirmation “Hello..” page.

Exercise6 Unable to load applet

<!-- function checkData (theForm){ var ReturnVal=false var address=theForm.Address.value var age=Number(theForm.Age.value) if(address=="") alert("Address must not be empty!") else if(isNaN(age)) alert("Age must be non negative number!") else if(age<0) alert("Age must be non negative number!") else ReturnVal=true return ReturnVal } //-->

Name: Address: Age:

Mr Mrs Miss ……………………..

// Exercise6Bean.java import java.beans.*; import java.io.*; import java.sql.*; public class Exercise6Bean{ private String name=""; public String getName(){ Connection con=null; ResultSet rs=null; try{ //load the JdbcOdbcDriver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Exercise5Base"; //get a connection to the database con = DriverManager.getConnection(url,"Exercise5Base", "sql");

//create a statement object Statement stmt = con.createStatement(); //execute an sql query String sql = "Select Name from Members" ; rs=stmt.executeQuery(sql); while (rs.next()) name= name+" " + rs.getString("Name"); } // end try catch(ClassNotFoundException e){ System.out.println("Couldn't load database driver: " + e.getMessage()); } catch(SQLException e){ System.out.println("SQLException caught: " + e.getMessage()); }

//close the database connection finally { try{ if (con!=null) con.close(); } catch (SQLException ignored){} } return name; }//end of function }// end of class

//Exercise6Servlet. Java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class Exercise6Servlet extends HttpServlet { public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { ServletOutputStream out = res.getOutputStream(); String mrMrsMiss=req.getParameter("Group1"); String name1=req.getParameter("Name"); String address= req.getParameter("Address"); String age=req.getParameter("Age"); Connection con=null;

try{ //load the JdbcOdbcDriver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Exercise5Base"; //get a connection to the database con = DriverManager.getConnection(url,"Exercise5Base", "sql"); PreparedStatement stmt = con.prepareStatement( "UPDATE Members SET Address = ?, Age=?, Title=? WHERE Name = ?"); stmt.setString(1, address); stmt.setString(2, age); stmt.setString(3, mrMrsMiss); stmt.setString(4,name1); stmt.executeUpdate(); } catch(ClassNotFoundException e){ System.out.println("Couldn't load database driver: " + e.getMessage()); } catch(SQLException e){ System.out.println("SQLException caught: " + e.getMessage()); }

//close the database connection finally { try{ if (con!=null) con.close(); } catch (SQLException ignored){} } out.print( " Exercise6 "+ " Exercise6: “+ " "+ " Servlet Response "+ " Hello "+mrMrsMiss+" "+name1+ ", glad to meet you! I'll contact you by ing to the address: ”+ address+"." + " "); out.println(); }

Conclusion: What you have learned?