This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Exercises of the Tutorial on Exercises of the Tutorial on Advanced Web Advanced Web ProgrammingProgramming
Exercise 1:Exercise 1: Develop Your Own HTML Web FormDevelop 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
Exercise 2:Exercise 2:Validate Your Form’s DataValidate 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
…………….<BODY>
<BR><BR>
<HR>
<SCRIPT LANGUAGE="JavaScript"><!—
function checkData (theForm){var ReturnVal=falsevar name=theForm.Name.valuevar address=theForm.Address.valuevar 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")
Exercise 3:Exercise 3:Make Your Web Form LiveMake Your Web Form Live
Make your web form alive, by adding a simple applet to your web formthat will demonstrate the possibility of creating dynamic contents.Using a scrolling box
<! Exercise3.html
………………….
<BODY>
<BR>
<APPLET CODE="Ticker.class" WIDTH=200 HEIGHT=35>
<PARAM NAME="fontname" VALUE="Times New Roman">
<PARAM NAME="fontsize" VALUE=24>
<PARAM NAME="text" VALUE="Fill out this form!">
</APPLET>
<HR>
<SCRIPT LANGUAGE="JavaScript">
……………
Exercise 4:Exercise 4:Develop Your Own ServletDevelop 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 <Mr.|Mrs.|Miss> <Name>, glad to meet you. I’ll stay in contact with
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
out.print( "<HEAD><TITLE>Exercise4</TITLE>"+ "<FONT SIZE=\"6\">Exercise4:</FONT></HEAD>"+ "<BR><BR><HR>" + "<BR><FONT SIZE=\"5\">Servlet Response: <BR><BR><BR>"+ "</FONT>Hello "+mrMrsMiss+" “ + name1 + ", glad to meet you!<BR><BR>I'll contact you by e-mailing to
the + "address: “+address + "<BR><BR><BR><BR><HR></BODY>"); out.close(); }}
Exercise 5:Exercise 5:Make Your Own Make Your Own Application Access the DatabaseApplication 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.
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");
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"+
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+"<OPTION>" + rs.getString("Name"); } // end try catch(ClassNotFoundException e){ System.out.println("Couldn't load database driver: " + e.getMessage()); }
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);