2. Previous Work Review
• http://pirate.shu.edu/~marinom6/work.html
• Please Note only previously due HW assignments are posted on my
pirate.shu.edu web space
• Begin organizing your creating files for this course into an easy to find folder
on your desktop for easy FTP later on
4. Model/View/Controller Cont.
A view is seen by user and the user uses the controller – this is
the role a user plays
The view is considered to be output of data for user to view
Commands are what the controller sends to the model and view
5. MVC in Simpler Terms
• Model – consists of the application’s model or business model. For example,
in an accounting system – account, invoice, cost center
• View – output of the data for the user to view
• Controller – brings together the model and view by sending commands to
both
6. Java MVC Example - Students
• Step 1
• Create Model.
• Student.java
7. Student.java
public class Student {
private String rollNo;
private String name;
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
8. Java MVC Example - Students
• Step 2
• Create View.
• StudentView.java
9. StudentView.java
public class StudentView {
public void printStudentDetails(String studentName, String studentRollNo){
System.out.println("Student: ");
System.out.println("Name: " + studentName);
System.out.println("Roll No: " + studentRollNo);
}
}
10. Java MVC Example - Students
• Step 3
• Create Controller.
• StudentController.java
11. StudentController.java
public class StudentController {
private Student model;
private StudentView view;
public StudentController(Student model, StudentView view){
this.model = model;
this.view = view;
}
public void setStudentName(String name){
model.setName(name);
}
public String getStudentName(){
return model.getName();
}
public void setStudentRollNo(String rollNo){
model.setRollNo(rollNo);
}
public String getStudentRollNo(){
return model.getRollNo();
}
public void updateView(){
view.printStudentDetails(model.getName(), model.getRollNo());
}
}
12. Java MVC Example - Students
• Step 4
• Use the StudentController methods to demonstrate
MVC design pattern usage.
• MVCPatternDemo.java
13. MVCPatternDemo.java
public class MVCPatternDemo {
public static void main(String[] args) {
//fetch student record based on his roll no from the database
Student model = retriveStudentFromDatabase();
//Create a view : to write student details on console
StudentView view = new StudentView();
StudentController controller = new StudentController(model,
view);
controller.updateView();
//update model data
controller.setStudentName("John");
controller.updateView();
}
private static Student retriveStudentFromDatabase(){
Student student = new Student();
student.setName("Robert");
student.setRollNo("10");
return student;
}
}
14. Java MVC Example - Students
• Step 5
• Verify the output.
• Student:
• Name: Robert
• Roll No: 10
• Student:
• Name: John
• Roll No: 10
16. Pretty Boring Right?
• Next week you will see MVC can be used with JSP (Java Server Pages) to do
things behind the scenes
• You have previously seen with JavaScript that the internet (a web server) can
interpret the Java programming language
21. Why No One Does It That Way?
• Basic, static HTML form – limited in functionality
• Does not work unless you create a csv file and put it somewhere on your
server
• Not visually appealing
23. Building Project 4 Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Contact Form</title>
</head>
<body id="top">
<div class="wrapper">
<div id="container">
<h2><span>Contact Form</span></h2>
<p>
24. Building Project 4 Code
<SCRIPT LANGUAGE="JAVASCRIPT">
<!-- HIDE script
var validForm
validForm=0
function confirmReset(myForm) {
var resetForm = confirm("Are you sure you want to Clear all the Information Entered?");
if (resetForm == true)
return true;
return false;
}
25. Building Project 4 Code
function validateForm(myForm) {
var x
x=1
//name
if (myForm.name.value == "") {
alert("Please Enter your Full Name.");
myForm.name.focus()
return false
}
26. Building Project 4 Code
//email
if (myForm.email.value == "") {
alert("Please Enter your Email Address.");
myForm.email.focus()
return false
}
//address
if (myForm.address.value == "") {
alert("Please Enter your Address.");
myForm.address.focus()
return false
}
27. Building Project 4 Code
//city
if (myForm.city.value == "") {
alert("Please Enter your City.");
myForm.city.focus()
return false
}
//state
if (myForm.state.value == "") {
alert("Please Enter you State");
myForm.state.focus()
return false
}
28. Building Project 4 Code
//zip code
if (myForm.zip.value == "") {
alert("Please Enter your Zip Code");
myForm.zip.focus()
return false
}
//alert("Thank You. The Information You Entered Will Be E-Mailed To Us. We Will Get Back To You Shortly.");
return true
}
// -- end hiding here -->
</SCRIPT>
29. Building Project 4 Code
<center>
<td width="595px" height="340">
</center>
<div width="560px">
<p align="left">
<b><i><font face="Calibri" size="3">Please Enter The Requested
Info Below
& Click Submit</font><font face="Arial" size="3"><br>
</font></i></b>
</p>
<center>
30. Building Project 4 Code
<form name="webForm" onreset="return confirmReset();" onsubmit="return validateForm(webForm);" action="http://pirate.shu.edu/~marinom6/sendinfo.php" target="_top" method="post">
<p align="left"><b><font face="Calibri" size="4">Please Enter Your Full Name:<br>
</font><font color="#FFFFFF" face="Calibri" size="4">
<input size="30" name="name"></font></b></p>
<p align="left"><b><font face="Calibri" size="4">Please Enter Your Email Address:<br>
</font><font color="#FFFFFF" face="Calibri" size="4">
<input size="30" name="email"></font></b></p>
<p align="left"><b><font face="Calibri" size="4">Please Enter Your Address: <br>
</font><font color="#FFFFFF" face="Calibri" size="4">
<input size="30" name="address"></font></b></p>
<p align="left"><font size="4" face="Calibri"><b>Please Enter Your City:<br></b></font>
<font color="#FFFFFF" face="Calibri" size="2"> <input size="58" name="city"></font><font face="Calibri" size="2"></b></font></p>
<p align="left"><font size="4" face="Calibri"><b>Please Enter Your State:<br></b></font>
<font color="#FFFFFF" face="Calibri" size="2"> <input size="58" name="state"></font><font face="Calibri" size="2"></b></font></p>
</center>
32. Building Project 4 Code
• That’s just the HTML file code!
• Now we need to build our sendinfo.php file to make the form email the
information put in the form.
34. Building Project 4 Code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; ">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Your Site Name....>> Contact Us via Email</title>
</head>
<body>
<center>
<B>
<font face="Verdana" size="4">
<br>
<br>
Attempting To Send E-Mail...
<br>
35. Building Project 4 Code
<br>
<?php
extract( $_REQUEST );
// take a given email address and split it into the username and domain.
list($userName, $mailDomain) = split("@", $email);
if (checkdnsrr($mailDomain, "MX")) {
// this is a valid email domain!
$info = sprintf("Name: %snEmail Address: %snAddress #: %snCity: %snState: %snZip
Code: %s",$name,$email,$address,$city,$state,$zip);
36. Building Project 4 Code
$check=mail("matt.marino@shu.edu","PHP",$info,"From: $email");
if ($check != true) {
print "Sorry... Error Sending E-Mail. E-Mail NOT Sent.";
}
else {
print "Thank You. Your E-Mail Has Been Sent... We Will Get Back To You Shortly...";
}
}
else {
// this email domain doesn't exist!
print "Sorry... Invalid Return E-Mail Address. E-Mail NOT Sent. Please Enter a Valid E-Mail Address So That We Can Reply To Your Request.";
}
?>
38. Building Project 4 Code
PHP must be enabled on the web server
Or we get a hang or infinite loop where our form
is always attempting to be sent to our email