日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java学习笔记9-2——JavaWeb

發布時間:2023/12/10 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java学习笔记9-2——JavaWeb 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • JavaBean
  • MVC三層架構
  • Filter過濾器
  • Listener監聽器
  • JDBC
  • 文件上傳Servlet
  • 郵件發送Servlet

JavaBean

實體類

JavaBean有特定的寫法:

  • 必須要有一個無參構造;
  • 屬性必須私有化;
  • 必須有對應的get/set方法;

一般用來和數據庫的字段做映射 ORM

ORM :對象關系映射

表—>類
字段—>屬性
行記錄—>對象

1.建POJO實體類(一般與數據庫中的表結構一一對應),略
2.JSP頁面

<jsp:useBean id="people" class="com.cheng.pojo.People" scope="page"/> <%--等價于People people = new People();--%> <jsp:setProperty name="people" property="id" value="1"/> <jsp:setProperty name="people" property="name" value="小明"/> <jsp:setProperty name="people" property="age" value="12"/> <jsp:setProperty name="people" property="address" value="廣州"/> <%--等價于people.setId("1");--%>id:<jsp:getProperty name="people" property="id"/> 姓名:<jsp:getProperty name="people" property="name"/> 年齡:<jsp:getProperty name="people" property="age"/> 地址:<jsp:getProperty name="people" property="address"/> <%--等價于people.getId();--%>

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接口,重寫對應的三個方法

public class CharacterEncodingFilter implements Filter {//初始化:web服務器啟動,就已經初始化了,隨時等待過濾對象出現!public void init(FilterConfig filterConfig) throws ServletException {System.out.println("CharacterEncodingFilter初始化");}//Chain : 鏈/*1. 過濾器中的所有代碼,在過濾特定請求的時候都會執行2. 必須要讓過濾器繼續同行即固定有這行代碼chain.doFilter(request,response);*/public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=UTF-8");System.out.println("CharacterEncodingFilter執行前....");chain.doFilter(request,response); //讓我們的請求繼續走,如果不寫,程序到這里就被攔截停止!System.out.println("CharacterEncodingFilter執行后....");}//銷毀:web服務器關閉的時候,過濾器會銷毀public void destroy() {System.out.println("CharacterEncodingFilter銷毀");} }

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。