Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Exercises of the Tutorial on Advanced Web Programming Authors: Miroslava Mitrovic Dragan Milicev Nino."— Presentation transcript:

1 Exercises of the Tutorial on Advanced Web Programming Authors: Miroslava Mitrovic (mirka@galeb.etf.bg.ac.yu) Dragan Milicev (emiliced@etf.bg.ac.yu) Nino Stojcic (nstojcic@yubc.net) Veljko Milutinovic (vm@etf.bg.ac.yu)

2 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

3

4 < ! Exercise1.html Exercise1 Exercise1: Name:  Address:

5 Age: Mr. Mrs. Miss

6 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

7

8

9

10 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/10/2818089/slides/slide_10.jpg", "name": "<. Exercise2.html …………….", "description": "

11 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 } //--> ………………………….

12 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

13

14 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/10/2818089/slides/slide_14.jpg", "name": "

15 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 e-mailing to the address:. “

16

17 Name: ……………..

18

19 // 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();

20 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 e-mailing to the + "address: “+address + " "); out.close(); }

21 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.

22

23

24 // 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");

25 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); }

26 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){} }

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

28

29 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,

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

31

32

33

34 Exercise6 Unable to load applet

35

36 Name: Address: Age:

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

38 // 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");

39 //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()); }

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

41

42 //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;

43 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()); }

44 //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 e-mailing to the address: ”+ address+"." + " "); out.println(); }

45

46 Conclusion: What you have learned?


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

Similar presentations


Ads by Google