Java 101
1. Java Web App
1.1 Architecture
base on JSP (MVC 1)
- request -> jsp handle -> java bean handle logic -> return view
base on Servlet (MVC 2)
- request -> controller/filter -> java bean handle logic -> jsp view -> return view
1.2 Servlet
- life circle of servlet
mỗi servlet sẽ extends HttpServlet, và life circle cơ bản sẽ bao gồm:
init() -> do sth before serve the request. Ex: get connection from db, init obj, …
doMethod(doGet, doPost, doPut, …) -> serve request
destroy() -> do sth after end request. Ex: close connection from db, …
- config trong web.xml. Ví dụ:
1.3 Filter
filter giống như servlet
là 1 chain nhiều servlet. Ex:
user request -> filter A -> filter B -> servlet C dành cho admin
filter A: check trong request xem thử session đã được mark là authen hay chưa, nếu đã authen thì pass qua filter B ko thì drop
filter B: check xem trong request xem thử session của user đó có phải là admin hay ko, có thì pass vào servlet C, ko thì drop
Ex trong web.xml
=> thứ tự filter trong web.xml cũng là thứ tự của filter sẽ đc java web app handle
1.4 Dispatcher