Java学习笔记9-2——JavaWeb
生活随笔
收集整理的這篇文章主要介紹了
Java学习笔记9-2——JavaWeb
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- JavaBean
- MVC三層架構
- Filter過濾器
- Listener監聽器
- JDBC
- 文件上傳Servlet
- 郵件發送Servlet
JavaBean
實體類
JavaBean有特定的寫法:
- 必須要有一個無參構造;
- 屬性必須私有化;
- 必須有對應的get/set方法;
一般用來和數據庫的字段做映射 ORM
ORM :對象關系映射
表—>類
字段—>屬性
行記錄—>對象
1.建POJO實體類(一般與數據庫中的表結構一一對應),略
2.JSP頁面
MVC三層架構
什么是MVC: Model View Controller 模型、視圖、控制器
Model
- 業務處理 :業務邏輯(Service)
- 數據持久層:CRUD (Dao - 數據持久化對象)
View
- 展示數據
- 提供鏈接發起Servlet請求 (a,form,img…)
Controller (Servlet)
-
接收用戶的請求 :(req:請求參數、Session信息….)
-
交給業務層處理對應的代碼
-
控制視圖的跳轉
Filter過濾器
比如 Shiro安全框架技術就是用Filter來實現的
Filter:過濾器 ,用來過濾網站的數據;
- 處理中文亂碼
- 登錄驗證….
Filter開發步驟:
1.導包
注意導入的是javax.servlet下的Filter
2.實現Filter接口,重寫對應的三個方法
3.在web.xml中配置 Filter
<filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>com.cheng.filter.CharacterEncodingFilter</filter-class></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><!--只要是 /servlet的任何請求,會經過這個過濾器--><url-pattern>/servlet/*</url-pattern><!--<url-pattern>/*</url-pattern>--><!-- 別偷懶寫個 /* --></filter-mapping><servlet><servlet-name>ShowServlet</servlet-name><servlet-class>com.cheng.servlet.ShowServlet</servlet-class></servlet><servlet-mapping><servlet-name>ShowServlet</servlet-name><url-pattern>/servlet/show</url-pattern></servlet-mapping><servlet><servlet-name>Servlet</servlet-name><servlet-class>com.cheng.servlet.ShowServlet</servlet-class></servlet><servlet-mapping><servlet-name>Servlet</servlet-name><url-pattern>/show</url-pattern></servlet-mapping>輸入http://localhost:8080/show得到亂碼
輸入http://localhost:8080/servlet/show得到
你好呀世界
Listener監聽器
1.實現一個監聽器的接口(有很多種監聽器)
import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener;//統計網站在線人數 : 統計session public class OnlineCountListener implements HttpSessionListener {//創建session監聽: 看你的一舉一動//一旦創建Session就會觸發一次這個事件!public void sessionCreated(HttpSessionEvent se) {ServletContext ctx = se.getSession().getServletContext();System.out.println(se.getSession().getId());Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");if (onlineCount==null){onlineCount = new Integer(1);}else {int count = onlineCount.intValue();onlineCount = new Integer(count+1);}ctx.setAttribute("OnlineCount",onlineCount);}//銷毀session監聽//一旦銷毀Session就會觸發一次這個事件!public void sessionDestroyed(HttpSessionEvent se) {ServletContext ctx = se.getSession().getServletContext();Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");if (onlineCount==null){onlineCount = new Integer(0);}else {int count = onlineCount.intValue();onlineCount = new Integer(count-1);}ctx.setAttribute("OnlineCount",onlineCount);}/*Session銷毀:1. 手動銷毀 getSession().invalidate();2. 自動銷毀*/ }2.web.xml中注冊監聽器
<!--注冊監聽器--><listener><listener-class>com.cheng.listener.OnlineCountListener</listener-class></listener>3.JSP
<h2>當前有<%=this.getServletConfig().getServletContext().getAttribute("OnlineCount")%>人在線</h2>JDBC
(參考MySQL筆記3——JDBC)
文件上傳Servlet
郵件發送Servlet
總結
以上是生活随笔為你收集整理的Java学习笔记9-2——JavaWeb的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为Tiny4412设备驱动在proc目录
- 下一篇: 脚本安装smokeping