tomcat啓動過程:
容器產生的事件有兩大類:
生命週期相關的事件
ServletRequestListener request對象的監聽器
HttpSessionListener session對象的監聽器
sessionCreate方法, 當服務器創建session對象時就調用此方法
sessionDestroyed方法,當session對象銷燬時調用此方法
ServletContextListener application對象的監聽器
綁定數據相關的事件
當調用了request對象,session對象,
servletContext(application)對象的setAttribute方法
和removeAttribute方法時產生的事件
ServletRequestAttributeListener
attributeAdded方法,當給調用request.setAttribute()方法時觸發此方法
attributeRemoved方法,當requset中的對象被移除的時候調用此方法
attributeReplaced方法,當request中的對象被替換的時候調用此方法
HttpSessionAttributeListener
attributeAdded方法
attributeRemoved方法
attributeReplaced方法
ServletContextAttributeListener
attributeAdded方法
attributeRemoved方法
attributeReplaced方法
實現監聽器的步驟:
1.新建一個java類要實現上面對應監聽器接口
2.編寫對應方法的監聽邏輯
3.在web.xml中,註冊監聽器<listener></listener>
a、什麼是過濾器:是servlet2.3規範中定義的一種小型的,可插入的
web組件,用來攔截servlet容器的請求和響應過程
以便查看,提取或以某種方式操作正在客戶端和服務器
之間交換的數據,
過濾器通常是封裝了一些web功能的組件,這些功能很重要
但對於處理客戶端後發送響應來說不是決定性的
b、 實現過濾器的步驟:
1.創建一個java類,實現javax.servlet.Filter;接口
2.在實現類中doFilter方法添加過濾或攔截的算法
3.在web.xml中添加<filter></filter><filter-mapping></filter-mapping>
c、 過濾器初始化參數:
容器啓動之後,會創建過濾器對象,然後會調用init方法,在init方法中
讀取web.xml中的初始化參數(包括局部和全局),在執行doFilter方法前
做數據初始化工作。
d、 過濾器的優點:
實現代碼的“可插拔性”,即增加或減少某個功能模塊,不會影響程序的
正常運行,可以將多個相同處理邏輯的模塊集中寫在過濾器裏,方便維護
e、只要:在需求說明中有這樣的詞語,“過濾”,“攔截”,“代理”,
“在原有功能基礎上添加新功能”,“在主業務之前,之後添加功能”
那麼,第一反應用代理模式實現,或(servlet filter)
注意:經過測試,5.0.4版本的spring mvcd jar包不需要使用<mvc:annotation-driven></mvc:annotation-driven> ,因爲<context:component-scan/>已經包含了它,而4.3.7版本的卻不行,必須寫<mvc:annotation-driven>才能掃描到類上方法的url。
@Controller("userController_form")
@RequestMapping("user_form/")
public class UserController_Form {
/**
*
* @param userName 對應頁面中的表單元素中的input name="userName"
* @param userPassword對應頁面中的表單元素中的input name="userPassword"
* @return ModelAndView對象給InternalResourceViewResolver 解析
*/
@RequestMapping(value="login1.do",method=RequestMethod.POST)
public ModelAndView login1(String userName,
String userPassword) {
ModelAndView mv=new ModelAndView();
//獲取頁面的數據,springmvc幫程序員已經做了
System.out.println("userName="+userName+
" userPassword="+userPassword);
//由程序員自己實現具體的登錄業務
boolean flag=true;
//根據業務的返回結果做跳轉和綁定數據
//根request.setAttribute("msg","登錄成功")
mv.addObject("msg", "登錄成功");
mv.setViewName("success");
return mv;
}