1 / 14

CS320 Web and Internet Programming Web Application and MVC

CS320 Web and Internet Programming Web Application and MVC. Chengyu Sun California State University, Los Angeles. Java Web Application. Servlets Beans JSPs Static resources HTML, CSS, images, … Metadata files web.xml,. Putting It All Together – The User Registration Example.

Download Presentation

CS320 Web and Internet Programming Web Application and MVC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS320 Web and Internet ProgrammingWeb Application and MVC Chengyu Sun California State University, Los Angeles

  2. Java Web Application • Servlets • Beans • JSPs • Static resources • HTML, CSS, images, … • Metadata files • web.xml, ...

  3. Putting It All Together – The User Registration Example • Register a new user • List all existing users

  4. Using JSP + Bean • Beans: User and UserManager • JSPs: Register.jsp and Users.jsp • Problems??

  5. Model 1 Architecture • JSP + Bean • JSP for presentation • Bean for business logic

  6. Problems of Model 1 Architecture • Using scripting elements mixes presentation and processing • Hard to debug, maintain, or reuse code • Not using scripting elements limits the interaction between presentation and processing to getters and setters • Tedious to program • Beans are no longer independent of the presentation layer, i.e. special getters/setters are needed

  7. Presentation Create UI Input and output JSP, JFC/Swing ... Data Models Independent of UI Bean (POJO) E.g. the User class Improve Model 1 Architecture Application Presentation ?? Data Models

  8. Model 2 Architecture • A.K.A. Model-View-Controller (MVC) Architecture Java Web Application View Controller Model JSP Servlet Bean

  9. About MVC • Originate from the work on Smalltalk • Widely used in GUI applications

  10. MVC in a Web Application ... model 2 3 controller 1 4 browser view 5 Client Server

  11. ... MVC in a Web Application • Process request • Create/update beans • Store beans in request, session, or application scope • Forward request to JSP page • Extract data from beans and display

  12. User Registration Example Using MVC • Model • User.java • View • RegisterUser.jsp, DisplayUsers.jsp • Controller • RegisterUser.java, DisplayUsers.java

  13. About the MVC Example • Servlets do NOT generate HTML directly • No out.println() • Redirect and Forward • JSPs are only used for display • Use of scopes • Application and session scopes are shared by all servlets and JSPs • Request scope is used for passing data from a servlet to a JSP

  14. Model 1 Requires less code Custom tags and EL Good for simple applications E.g. labs and exams MVC Requires more code Java is verbose Good for complex applications E.g. homeworks Model 1 vs. MVC

More Related