110 likes | 195 Views
CSCI 6962: Server-side Design and Programming. Support Classes and the Model-View-Control Architecture. Model-View-Control Architecture. Model = software classes that store/manipulate information gathered by application Usually separate from servlets/JSPs, which interact with them
E N D
CSCI 6962: Server-side Design and Programming Support Classes and the Model-View-Control Architecture
Model-View-Control Architecture • Model = software classes that store/manipulate information gathered by application • Usually separate from servlets/JSPs, which interact with them • Often interact with databases Classes that model business process Store data Control servlet request View JSP View JSP Access and display data View JSP Database response
Business Model Objects Key idea: • Methods in model objects should implement business model • Methods to validate values • Methods to perform computations • Methods to store information in database Goal: • Separate web programming and business knowledge as much as possible
Model Class Example • Model class for widget application • Contains quantity, name, and email • Method to get total cost of order • Method to get widgets remaining in stock (for validation) • Method to place order in database Constructor Order class request PlaceOrderservlet getAvailable placeOrder QuantityError JSP getAvailable getCost WidgetReceipt JSP Database
Creating Support Classes • File New File • Choose category Java, type class • Support classes must be in a package • “Library” of related classes • Can define one at this point (added to drop down in future)
Using Model Classes • Other classes that use class must import the package • Servlets use import statement • JSPs use <@ page import tag
Order Support Class • Class includes name of package • Stores data related to model • Constructor stores those values
Order Support Class Would actually query database for these values
Using Model Classes In servlet: • Construct new instance of Order object to store data • Use getQuantityto check whether quantity legal • Use placeOrder to store order information
Using Model Classes Can pass object to JSP as attribute in request (can also store in session for access by all pages)
Using Model Classes • JSP can access object as attribute • Can then access object to extract data