![Page 1: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/1.jpg)
CS320 Web and Internet ProgrammingWeb Application and MVC
Chengyu SunCalifornia State University, Los Angeles
![Page 2: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/2.jpg)
Java Web Application
ServletsBeansJSPsStatic resources HTML, CSS, images, …
Metadata files web.xml, ...
![Page 3: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/3.jpg)
Putting It All Together – The User Registration Example
Register a new userList all existing users
![Page 4: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/4.jpg)
Using JSP + Bean
Beans: User and UserManagerJSPs: Register.jsp and Users.jsp
Problems??
![Page 5: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/5.jpg)
Model 1 Architecture
JSP + Bean JSP for presentation Bean for business logic
![Page 6: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/6.jpg)
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
![Page 7: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/7.jpg)
Improve Model 1 Architecture
Presentation Create UI Input and output JSP, JFC/Swing ...
Data Models Independent of UI Bean (POJO) E.g. the User class
Presentation Data Models??
Application
![Page 8: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/8.jpg)
Model 2 Architecture
A.K.A. Model-View-Controller (MVC) Architecture
View
Java Web Application
Controller Model
JSP Servlet Bean
![Page 9: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/9.jpg)
About MVC
Originate from the work on SmalltalkWidely used in GUI applications
![Page 10: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/10.jpg)
MVC in a Web Application ...
controller
browser
model
view
1
2
3
4
5
Client Server
![Page 11: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/11.jpg)
... MVC in a Web Application
1. Process request2. Create/update beans3. Store beans in request, session,
or application scope4. Forward request to JSP page5. Extract data from beans and
display
![Page 12: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/12.jpg)
User Registration Example Using MVC
Model User.java
View RegisterUser.jsp, DisplayUsers.jsp
Controller RegisterUser.java, DisplayUsers.java
![Page 13: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/13.jpg)
About the MVC Example
Servlets do NOT generate HTML directly No out.println() Redirect and Forward
JSPs are only used for displayUse 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
![Page 14: CS320 Web and Internet Programming Web Application and MVC](https://reader036.vdocuments.us/reader036/viewer/2022082505/568134d1550346895d9bfa83/html5/thumbnails/14.jpg)
Model 1 vs. MVC
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