Frameworks

advertisement
CSC 2720
Building Web Applications
Frameworks for Building Web Applications
Model-1 Architecture
 A web application is made up of many JSP pages
 Each JSP page is regarded as the focal point for
the entire application.
 Each JSP page
 Contains code for extracting HTTP request parameters,
call the business logic (implemented in JavaBeans, if not
directly in the JSP)
 Contains the presentation logics
 Suitable for small/simple application
Model-2 Architecture
 Also called Model-View-Controller (MVC)
architecture
 Model: The data
 View: Representation of the model
 Controller: Processes and responds to events (typically
user actions) and update the model accordingly
 MVC aims at separating data (model) from user interface
(view) so that changes to the user interface will not affect
data handling, and that the data can be reorganized
without changing the user interface.
Model-2 Architecture in Web Application
 One servlet serves as controller (Can be more than one
servlet serving as controllers)
 The controller handles requests, decides what logic to
invoke, and decides what JSP page to apply.
 Results are placed in beans (or to objects that can be
accessed by the JSP pages.)
 Request is forwarded to a JSP page to format result
 Different JSP pages can be used to handle different types
of presentation
 Suitable for large or complicated application
MVC Flow of Control
Source:
http://courses.coreservlets.com/Course-Materials/pdf/msajsp/0B-MVC-Review.pdf
Implementing MVC with RequestDispatcher
1. Define beans to represent the data
2. Use a servlet to handle requests
 Servlet reads request parameters, checks for missing
and malformed data, etc.
3. Populate the beans
 The servlet invokes business logic (application-specific
code) or data-access code to obtain the results. Results
are placed in the beans that were defined in step 1.
Implementing MVC with RequestDispatcher
4. Store the bean in the request, session, or servlet
context

The servlet calls setAttribute on the request, session, or
servlet context objects to store a reference to the beans
that represent the results of the request.
5. Forward the request to a JSP page

The servlet determines which JSP page is appropriate
to the situation and uses the forward method of
RequestDispatcher to transfer control to that page.
Implementing MVC with RequestDispatcher
6. Extract the data from the beans.


The JSP page accesses beans with jsp:useBean and a
scope matching the location of step 4. The page then
uses jsp:getProperty to output the bean properties.
The JSP page does not create or modify the bean; it
merely extracts and displays data that the servlet
created.
Example of a Controller
…
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String operation = request.getParameter("operation");
if (operation == null)
operation = "unknown";
String address;
if (operation.equals("order"))
address = "/Order.jsp";
else if (operation.equals("cancel"))
address = "/Cancel.jsp";
else
address = "/UnknownOperation.jsp";
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}
Web Application Framework
 A web application framework is a software
framework that is designed to support the
development of dynamic websites and Web
applications.
 Aims




Improve productivity
Promote code reuse
Minimize errors
Reduce cost of maintenance
 Typically based on an MVC architecture and
include additional libraries.
 e.g.: JavaServer Faces (JSF), Struts
References
 Wikipedia
 http://courses.coreservlets.com/Course-Materials/
Download