1. 程式人生 > 實用技巧 >使用HMM進行分類識別(以語音識別為例)

使用HMM進行分類識別(以語音識別為例)

JavaWeb

Java Web

1、基本概念

1.1、前言

web開發:

  • web,網頁的意思 , www.baidu.com
  • 靜態web
    • html,css
    • 提供給所有人看的資料始終不會發生變化!
  • 動態web
    • 淘寶,幾乎是所有的網站;
    • 提供給所有人看的資料始終會發生變化,每個人在不同的時間,不同的地點看到的資訊各不相同!
    • 技術棧:Servlet/JSP,ASP,PHP

在Java中,動態web資源開發的技術統稱為JavaWeb;

1.2、web應用程式

web應用程式:可以提供瀏覽器訪問的程式;

  • a.html、b.html......多個web資源,這些web資源可以被外界訪問,對外界提供服務;
  • 你們能訪問到的任何一個頁面或者資源,都存在於這個世界的某一個角落的計算機上。
  • URL
  • 這個統一的web資源會被放在同一個資料夾下,web應用程式-->Tomcat:伺服器
  • 一個web應用由多部分組成 (靜態web,動態web)
    • html,css,js
    • jsp,servlet
    • Java程式
    • jar包
    • 配置檔案 (Properties)

web應用程式編寫完畢後,若想提供給外界訪問:需要一個伺服器來統一管理;

1.3、靜態web

  • *.htm, *.html,這些都是網頁的字尾,如果伺服器上一直存在這些東西,我們就可以直接進行讀取。通絡;

  • 靜態web存在的缺點
    • Web頁面無法動態更新,所有使用者看到都是同一個頁面
      • 輪播圖,點選特效:偽動態
      • JavaScript [實際開發中,它用的最多]
      • VBScript
    • 它無法和資料庫互動(資料無法持久化,使用者無法互動)

1.4、動態web

頁面會動態展示: “Web的頁面展示的效果因人而異”;

缺點:

  • 假如伺服器的動態web資源出現了錯誤,我們需要重新編寫我們的後臺程式,重新發布;
    • 停機維護

優點:

  • Web頁面可以動態更新,所有使用者看到都不是同一個頁面
  • 它可以與資料庫互動 (資料持久化:註冊,商品資訊,使用者資訊........)

新手村:--魔鬼訓練(分析原理,看原始碼)--> PK場

2、web伺服器

2.1、技術講解(動態網頁技術標準)

ASP:

  • 微軟:國內最早流行的就是ASP;

  • 在HTML中嵌入了VB的指令碼, ASP + COM;

  • 在ASP開發中,基本一個頁面都有幾千行的業務程式碼,頁面極其換亂

  • 維護成本高!

  • C#

  • IIS

    <h1>
        <h1><h1>
            <h1>
                <h1>
                    <h1>
            <h1>
                <%
                System.out.println("hello")
                %>
                <h1>
                    <h1>
       <h1><h1>
    <h1>
    

php:

  • PHP開發速度很快,功能很強大,跨平臺,程式碼很簡單 (70% , WP)
  • 無法承載大訪問量的情況(侷限性)

**JSP/Servlet : **

B/S:瀏覽和伺服器

C/S: 客戶端和伺服器

  • sun公司主推的B/S架構
  • 基於Java語言的 (所有的大公司,或者一些開源的元件,都是用Java寫的)
  • 可以承載三高問題帶來的影響;
  • 語法像ASP , ASP-->JSP , 加強市場強度;

.....

2.2、web伺服器

伺服器是一種被動的操作,用來處理使用者的一些請求和給使用者一些響應資訊;

IIS

微軟的; ASP...,Windows中自帶的

Tomcat

面向百度程式設計;

Tomcat是Apache 軟體基金會(Apache Software Foundation)的Jakarta 專案中的一個核心專案,最新的Servlet 和JSP 規範總是能在Tomcat 中得到體現,因為Tomcat 技術先進、效能穩定,而且免費,因而深受Java 愛好者的喜愛並得到了部分軟體開發商的認可,成為目前比較流行的Web 應用伺服器。

Tomcat 伺服器是一個免費的開放原始碼的Web 應用伺服器,屬於輕量級應用伺服器,在中小型系統和併發訪問使用者不是很多的場合下被普遍使用,是開發和除錯JSP 程式的首選。對於一個Java初學web的人來說,它是最佳的選擇

Tomcat 實際上執行JSP 頁面和Servlet。Tomcat最新版本為9.0。

....

工作3-5年之後,可以嘗試手寫Tomcat伺服器;

下載tomcat:

  1. 安裝 or 解壓
  2. 瞭解配置檔案及目錄結構
  3. 這個東西的作用

3、Tomcat

3.1、 安裝tomcat

tomcat官網:http://tomcat.apache.org/

3.2、Tomcat啟動和配置

資料夾作用:

啟動。關閉Tomcat

訪問測試:http://localhost:8080/

可能遇到的問題:

  1. Java環境變數沒有配置
  2. 閃退問題:需要配置相容性
  3. 亂碼問題:配置檔案中設定

3.3、配置

可以配置啟動的埠號

  • tomcat的預設埠號為:8080
  • mysql:3306
  • http:80
  • https:443
<Connector port="8081" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

可以配置主機的名稱

  • 預設的主機名為:localhost->127.0.0.1
  • 預設網站應用存放的位置為:webapps
  <Host name="www.qinjiang.com"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">

高難度面試題:

請你談談網站是如何進行訪問的!

  1. 輸入一個域名;回車

  2. 檢查本機的 C:\Windows\System32\drivers\etc\hosts配置檔案下有沒有這個域名對映;

    1. 有:直接返回對應的ip地址,這個地址中,有我們需要訪問的web程式,可以直接訪問

      127.0.0.1       www.qinjiang.com
      
    2. 沒有:去DNS伺服器找,找到的話就返回,找不到就返回找不到;

  3. 可以配置一下環境變數(可選性)

3.4、釋出一個web網站

不會就先模仿

  • 將自己寫的網站,放到伺服器(Tomcat)中指定的web應用的資料夾(webapps)下,就可以訪問了

網站應該有的結構

--webapps :Tomcat伺服器的web目錄
	-ROOT
	-kuangstudy :網站的目錄名
		- WEB-INF
			-classes : java程式
			-lib:web應用所依賴的jar包
			-web.xml :網站配置檔案
		- index.html 預設的首頁
		- static 
            -css
            	-style.css
            -js
            -img
         -.....

HTTP協議 : 面試

Maven:構建工具

  • Maven安裝包

Servlet 入門

  • HelloWorld!
  • Servlet配置
  • 原理

4、Http

4.1、什麼是HTTP

HTTP(超文字傳輸協議)是一個簡單的請求-響應協議,它通常執行在TCP之上。

  • 文字:html,字串,~ ….
  • 超文字:圖片,音樂,視訊,定位,地圖…….
  • 80

Https:安全的

  • 443

4.2、兩個時代

  • http1.0

    • HTTP/1.0:客戶端可以與web伺服器連線後,只能獲得一個web資源,斷開連線
  • http2.0

    • HTTP/1.1:客戶端可以與web伺服器連線後,可以獲得多個web資源。‘

4.3、Http請求

  • 客戶端---發請求(Request)---伺服器

百度:

Request URL:https://www.baidu.com/   請求地址
Request Method:GET    get方法/post方法
Status Code:200 OK    狀態碼:200
Remote(遠端) Address:14.215.177.39:443
Accept:text/html  
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.9    語言
Cache-Control:max-age=0
Connection:keep-alive

1、請求行

  • 請求行中的請求方式:GET
  • 請求方式:Get,Post,HEAD,DELETE,PUT,TRACT…
    • get:請求能夠攜帶的引數比較少,大小有限制,會在瀏覽器的URL位址列顯示資料內容,不安全,但高效
    • post:請求能夠攜帶的引數沒有限制,大小沒有限制,不會在瀏覽器的URL位址列顯示資料內容,安全,但不高效。

2、訊息頭

Accept:告訴瀏覽器,它所支援的資料型別
Accept-Encoding:支援哪種編碼格式  GBK   UTF-8   GB2312  ISO8859-1
Accept-Language:告訴瀏覽器,它的語言環境
Cache-Control:快取控制
Connection:告訴瀏覽器,請求完成是斷開還是保持連線
HOST:主機..../.

4.4、Http響應

  • 伺服器---響應-----客戶端

百度:

Cache-Control:private    快取控制
Connection:Keep-Alive    連線
Content-Encoding:gzip    編碼
Content-Type:text/html   型別

1.響應體

Accept:告訴瀏覽器,它所支援的資料型別
Accept-Encoding:支援哪種編碼格式  GBK   UTF-8   GB2312  ISO8859-1
Accept-Language:告訴瀏覽器,它的語言環境
Cache-Control:快取控制
Connection:告訴瀏覽器,請求完成是斷開還是保持連線
HOST:主機..../.
Refresh:告訴客戶端,多久重新整理一次;
Location:讓網頁重新定位;

2、響應狀態碼

200:請求響應成功 200

3xx:請求重定向

  • 重定向:你重新到我給你新位置去;

4xx:找不到資源 404

  • 資源不存在;

5xx:伺服器程式碼錯誤 500 502:閘道器錯誤

常見面試題:

當你的瀏覽器中位址列輸入地址並回車的一瞬間到頁面能夠展示回來,經歷了什麼?

5、Maven

我為什麼要學習這個技術?

  1. 在Javaweb開發中,需要使用大量的jar包,我們手動去匯入;

  2. 如何能夠讓一個東西自動幫我匯入和配置這個jar包。

    由此,Maven誕生了!

5.1 Maven專案架構管理工具

我們目前用來就是方便匯入jar包的!

Maven的核心思想:約定大於配置

  • 有約束,不要去違反。

Maven會規定好你該如何去編寫我們的Java程式碼,必須要按照這個規範來;

5.2 下載安裝Maven

官網;https://maven.apache.org/

下載完成後,解壓即可;

小狂神友情建議:電腦上的所有環境都放在一個資料夾下,方便管理;

5.3 配置環境變數

在我們的系統環境變數中

配置如下配置:

  • M2_HOME maven目錄下的bin目錄
  • MAVEN_HOME maven的目錄
  • 在系統的path中配置 %MAVEN_HOME%\bin

測試Maven是否安裝成功,保證必須配置完畢!

5.4 阿里雲映象

  • 映象:mirrors
    • 作用:加速我們的下載
  • 國內建議使用阿里雲的映象
<mirror>
    <id>nexus-aliyun</id>  
    <mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>  
    <name>Nexus aliyun</name>  
    <url>http://maven.aliyun.com/nexus/content/groups/public</url> 
</mirror>

5.5 本地倉庫

在本地的倉庫,遠端倉庫;

建立一個本地倉庫:localRepository

<localRepository>D:\Environment\apache-maven-3.6.2\maven-repo</localRepository>

5.6、在IDEA中使用Maven

  1. 啟動IDEA

  2. 建立一個MavenWeb專案

  3. 等待專案初始化完畢

  4. 觀察maven倉庫中多了什麼東西?

  5. IDEA中的Maven設定

    注意:IDEA專案建立成功後,看一眼Maven的配置

  6. 到這裡,Maven在IDEA中的配置和使用就OK了!

5.7、建立一個普通的Maven專案

這個只有在Web應用下才會有!

5.8 標記資料夾功能

5.9 在 IDEA中配置Tomcat

解決警告問題

必須要的配置:為什麼會有這個問題:我們訪問一個網站,需要指定一個資料夾名字;

5.10 pom檔案

pom.xml 是Maven的核心配置檔案

<?xml version="1.0" encoding="UTF-8"?>

<!--Maven版本和標頭檔案-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <!--這裡就是我們剛才配置的GAV-->
  <groupId>com.kuang</groupId>
  <artifactId>javaweb-01-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--Package:專案的打包方式
  jar:java應用
  war:JavaWeb應用
  -->
  <packaging>war</packaging>


  <!--配置-->
  <properties>
    <!--專案的預設構建編碼-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!--編碼版本-->
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <!--專案依賴-->
  <dependencies>
    <!--具體依賴的jar包配置檔案-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
  </dependencies>

  <!--專案構建用的東西-->
  <build>
    <finalName>javaweb-01-maven</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

maven由於他的約定大於配置,我們之後可以能遇到我們寫的配置檔案,無法被匯出或者生效的問題,解決方案:

<!--在build中配置resources,來防止我們資源匯出失敗的問題-->
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

5.12 IDEA操作

5.13 解決遇到的問題

  1. Maven 3.6.2

    解決方法:降級為3.6.1

  2. Tomcat閃退

  3. IDEA中每次都要重複配置Maven
    在IDEA中的全域性預設配置中去配置

  4. Maven專案中Tomcat無法配置

  5. maven預設web專案中的web.xml版本問題

  6. 替換為webapp4.0版本和tomcat一致

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                          http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0"
             metadata-complete="true">
    
    
    
    </web-app>
    
  7. Maven倉庫的使用

    地址:https://mvnrepository.com/

6、Servlet

6.1、Servlet簡介

  • Servlet就是sun公司開發動態web的一門技術
  • Sun在這些API中提供一個介面叫做:Servlet,如果你想開發一個Servlet程式,只需要完成兩個小步驟:
    • 編寫一個類,實現Servlet介面
    • 把開發好的Java類部署到web伺服器中。

把實現了Servlet介面的Java程式叫做,Servlet

6.2、HelloServlet

Serlvet介面Sun公司有兩個預設的實現類:HttpServlet,GenericServlet

  1. 構建一個普通的Maven專案,刪掉裡面的src目錄,以後我們的學習就在這個專案裡面建立Moudel;這個空的工程就是Maven主工程;

  2. 關於Maven父子工程的理解:

    父專案中會有

        <modules>
            <module>servlet-01</module>
        </modules>
    

    子專案會有

        <parent>
            <artifactId>javaweb-02-servlet</artifactId>
            <groupId>com.kuang</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    

    父專案中的java子專案可以直接使用

    son extends father
    
  3. Maven環境優化

    1. 修改web.xml為最新的:

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                            http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
               version="4.0"
               metadata-complete="true">
        
      
    2. 將maven的結構搭建完整

  4. 編寫一個Servlet程式

    1. 編寫一個普通類

    2. 實現Servlet介面,這裡我們直接繼承HttpServlet

      public class HelloServlet extends HttpServlet {
          
          //由於get或者post只是請求實現的不同的方式,可以相互呼叫,業務邏輯都一樣;
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              //ServletOutputStream outputStream = resp.getOutputStream();
              PrintWriter writer = resp.getWriter(); //響應流
              writer.print("Hello,Serlvet");
          }
      
          @Override
          protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              doGet(req, resp);
          }
      }
      
      
  5. 編寫Servlet的對映

    為什麼需要對映:我們寫的是JAVA程式,但是要通過瀏覽器訪問,而瀏覽器需要連線web伺服器,所以我們需要再web服務中註冊我們寫的Servlet,還需給他一個瀏覽器能夠訪問的路徑;

        <!--註冊Servlet-->
        <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
        </servlet>
        <!--Servlet的請求路徑-->
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
    
    
  6. 配置Tomcat

    注意:配置專案釋出的路徑就可以了

  7. 啟動測試,OK!

6.3、Servlet原理

Servlet是由Web伺服器呼叫,web伺服器在收到瀏覽器請求之後,會:

6.4、Mapping問題

  1. 一個Servlet可以指定一個對映路徑

        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
    
  2. 一個Servlet可以指定多個對映路徑

        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello2</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello3</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello4</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello5</url-pattern>
        </servlet-mapping>
    
    
  3. 一個Servlet可以指定通用對映路徑

        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello/*</url-pattern>
        </servlet-mapping>
    
  4. 預設請求路徑

        <!--預設請求路徑-->
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
  5. 指定一些字尾或者字首等等….

    <!--可以自定義字尾實現請求對映
        注意點,*前面不能加專案對映的路徑
        hello/sajdlkajda.qinjiang
        -->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.qinjiang</url-pattern>
    </servlet-mapping>
    
  6. 優先順序問題
    指定了固有的對映路徑優先順序最高,如果找不到就會走預設的處理請求;

    <!--404-->
    <servlet>
        <servlet-name>error</servlet-name>
        <servlet-class>com.kuang.servlet.ErrorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>error</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    
    

6.5、ServletContext

web容器在啟動的時候,它會為每個web程式都建立一個對應的ServletContext物件,它代表了當前的web應用;

1、共享資料

我在這個Servlet中儲存的資料,可以在另外一個servlet中拿到;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        //this.getInitParameter()   初始化引數
        //this.getServletConfig()   Servlet配置
        //this.getServletContext()  Servlet上下文
        ServletContext context = this.getServletContext();

        String username = "秦疆"; //資料
        context.setAttribute("username",username); //將一個數據儲存在了ServletContext中,名字為:username 。值 username

    }

}

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String username = (String) context.getAttribute("username");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("名字"+username);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>getc</servlet-name>
        <servlet-class>com.kuang.servlet.GetServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>getc</servlet-name>
        <url-pattern>/getc</url-pattern>
    </servlet-mapping>

測試訪問結果;

2、獲取初始化引數

    <!--配置一些web應用初始化引數-->
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
    </context-param>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    String url = context.getInitParameter("url");
    resp.getWriter().print(url);
}

3、請求轉發

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    System.out.println("進入了ServletDemo04");
    //RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp"); //轉發的請求路徑
    //requestDispatcher.forward(req,resp); //呼叫forward實現請求轉發;
    context.getRequestDispatcher("/gp").forward(req,resp);
}

4、讀取資原始檔

Properties

  • 在java目錄下新建properties
  • 在resources目錄下新建properties

發現:都被打包到了同一個路徑下:classes,我們俗稱這個路徑為classpath:

思路:需要一個檔案流;

username=root12312
password=zxczxczxc
public class ServletDemo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/kuang/servlet/aa.properties");

        Properties prop = new Properties();
        prop.load(is);
        String user = prop.getProperty("username");
        String pwd = prop.getProperty("password");

        resp.getWriter().print(user+":"+pwd);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

訪問測試即可ok;

6.6、HttpServletResponse

web伺服器接收到客戶端的http請求,針對這個請求,分別建立一個代表請求的HttpServletRequest物件,代表響應的一個HttpServletResponse;

  • 如果要獲取客戶端請求過來的引數:找HttpServletRequest
  • 如果要給客戶端響應一些資訊:找HttpServletResponse

1、簡單分類

負責向瀏覽器傳送資料的方法

ServletOutputStream getOutputStream() throws IOException;
PrintWriter getWriter() throws IOException;

負責向瀏覽器傳送響應頭的方法

    void setCharacterEncoding(String var1);

    void setContentLength(int var1);

    void setContentLengthLong(long var1);

    void setContentType(String var1);

    void setDateHeader(String var1, long var2);

    void addDateHeader(String var1, long var2);

    void setHeader(String var1, String var2);

    void addHeader(String var1, String var2);

    void setIntHeader(String var1, int var2);

    void addIntHeader(String var1, int var2);

響應的狀態碼

    int SC_CONTINUE = 100;
    int SC_SWITCHING_PROTOCOLS = 101;
    int SC_OK = 200;
    int SC_CREATED = 201;
    int SC_ACCEPTED = 202;
    int SC_NON_AUTHORITATIVE_INFORMATION = 203;
    int SC_NO_CONTENT = 204;
    int SC_RESET_CONTENT = 205;
    int SC_PARTIAL_CONTENT = 206;
    int SC_MULTIPLE_CHOICES = 300;
    int SC_MOVED_PERMANENTLY = 301;
    int SC_MOVED_TEMPORARILY = 302;
    int SC_FOUND = 302;
    int SC_SEE_OTHER = 303;
    int SC_NOT_MODIFIED = 304;
    int SC_USE_PROXY = 305;
    int SC_TEMPORARY_REDIRECT = 307;
    int SC_BAD_REQUEST = 400;
    int SC_UNAUTHORIZED = 401;
    int SC_PAYMENT_REQUIRED = 402;
    int SC_FORBIDDEN = 403;
    int SC_NOT_FOUND = 404;
    int SC_METHOD_NOT_ALLOWED = 405;
    int SC_NOT_ACCEPTABLE = 406;
    int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
    int SC_REQUEST_TIMEOUT = 408;
    int SC_CONFLICT = 409;
    int SC_GONE = 410;
    int SC_LENGTH_REQUIRED = 411;
    int SC_PRECONDITION_FAILED = 412;
    int SC_REQUEST_ENTITY_TOO_LARGE = 413;
    int SC_REQUEST_URI_TOO_LONG = 414;
    int SC_UNSUPPORTED_MEDIA_TYPE = 415;
    int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
    int SC_EXPECTATION_FAILED = 417;
    int SC_INTERNAL_SERVER_ERROR = 500;
    int SC_NOT_IMPLEMENTED = 501;
    int SC_BAD_GATEWAY = 502;
    int SC_SERVICE_UNAVAILABLE = 503;
    int SC_GATEWAY_TIMEOUT = 504;
    int SC_HTTP_VERSION_NOT_SUPPORTED = 505;

2、下載檔案

  1. 向瀏覽器輸出訊息 (一直在講,就不說了)
  2. 下載檔案
    1. 要獲取下載檔案的路徑
    2. 下載的檔名是啥?
    3. 設定想辦法讓瀏覽器能夠支援下載我們需要的東西
    4. 獲取下載檔案的輸入流
    5. 建立緩衝區
    6. 獲取OutputStream物件
    7. 將FileOutputStream流寫入到buffer緩衝區
    8. 使用OutputStream將緩衝區中的資料輸出到客戶端!
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 1. 要獲取下載檔案的路徑
    String realPath = "F:\\班級管理\\西開【19525】\\2、程式碼\\JavaWeb\\javaweb-02-servlet\\response\\target\\classes\\秦疆.png";
    System.out.println("下載檔案的路徑:"+realPath);
    // 2. 下載的檔名是啥?
    String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
    // 3. 設定想辦法讓瀏覽器能夠支援(Content-Disposition)下載我們需要的東西,中文檔名URLEncoder.encode編碼,否則有可能亂碼
    resp.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));
    // 4. 獲取下載檔案的輸入流
    FileInputStream in = new FileInputStream(realPath);
    // 5. 建立緩衝區
    int len = 0;
    byte[] buffer = new byte[1024];
    // 6. 獲取OutputStream物件
    ServletOutputStream out = resp.getOutputStream();
    // 7. 將FileOutputStream流寫入到buffer緩衝區,使用OutputStream將緩衝區中的資料輸出到客戶端!
    while ((len=in.read(buffer))>0){
        out.write(buffer,0,len);
    }

    in.close();
    out.close();
}

3、驗證碼功能

驗證怎麼來的?

  • 前端實現
  • 後端實現,需要用到 Java 的圖片類,生產一個圖片
package com.kuang.servlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class ImageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //如何讓瀏覽器3秒自動重新整理一次;
        resp.setHeader("refresh","3");
        
        //在記憶體中建立一個圖片
        BufferedImage image = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);
        //得到圖片
        Graphics2D g = (Graphics2D) image.getGraphics(); //筆
        //設定圖片的背景顏色
        g.setColor(Color.white);
        g.fillRect(0,0,80,20);
        //給圖片寫資料
        g.setColor(Color.BLUE);
        g.setFont(new Font(null,Font.BOLD,20));
        g.drawString(makeNum(),0,20);

        //告訴瀏覽器,這個請求用圖片的方式開啟
        resp.setContentType("image/jpeg");
        //網站存在快取,不讓瀏覽器快取
        resp.setDateHeader("expires",-1);
        resp.setHeader("Cache-Control","no-cache");
        resp.setHeader("Pragma","no-cache");

        //把圖片寫給瀏覽器
        ImageIO.write(image,"jpg", resp.getOutputStream());

    }

    //生成隨機數
    private String makeNum(){
        Random random = new Random();
        String num = random.nextInt(9999999) + "";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 7-num.length() ; i++) {
            sb.append("0");
        }
        num = sb.toString() + num;
        return num;
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

4、實現重定向

B一個web資源收到客戶端A請求後,B他會通知A客戶端去訪問另外一個web資源C,這個過程叫重定向

常見場景:

  • 使用者登入
void sendRedirect(String var1) throws IOException;

測試:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    /*
        resp.setHeader("Location","/r/img");
        resp.setStatus(302);
         */
    resp.sendRedirect("/r/img");//重定向
}

面試題:請你聊聊重定向和轉發的區別?

相同點

  • 頁面都會實現跳轉

不同點

  • 請求轉發的時候,url不會產生變化
  • 重定向時候,url位址列會發生變化;

5、簡單實現登入重定向

<%--這裡提交的路徑,需要尋找到專案的路徑--%>
<%--${pageContext.request.contextPath}代表當前的專案--%>

<form action="${pageContext.request.contextPath}/login" method="get">
    使用者名稱:<input type="text" name="username"> <br>
    密碼:<input type="password" name="password"> <br>
    <input type="submit">
</form>

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //處理請求
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println(username+":"+password);

        //重定向時候一定要注意,路徑問題,否則404;
        resp.sendRedirect("/r/success.jsp");
    }

  <servlet>
    <servlet-name>requset</servlet-name>
    <servlet-class>com.kuang.servlet.RequestTest</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>requset</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h1>Success</h1>

</body>
</html>

6.7、HttpServletRequest

HttpServletRequest代表客戶端的請求,使用者通過Http協議訪問伺服器,HTTP請求中的所有資訊會被封裝到HttpServletRequest,通過這個HttpServletRequest的方法,獲得客戶端的所有資訊;

獲取引數,請求轉發

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    req.setCharacterEncoding("utf-8");
    resp.setCharacterEncoding("utf-8");

    String username = req.getParameter("username");
    String password = req.getParameter("password");
    String[] hobbys = req.getParameterValues("hobbys");
    System.out.println("=============================");
    //後臺接收中文亂碼問題
    System.out.println(username);
    System.out.println(password);
    System.out.println(Arrays.toString(hobbys));
    System.out.println("=============================");


    System.out.println(req.getContextPath());
    //通過請求轉發
    //這裡的 / 代表當前的web應用
    req.getRequestDispatcher("/success.jsp").forward(req,resp);

}

面試題:請你聊聊重定向和轉發的區別?

相同點

  • 頁面都會實現跳轉

不同點

  • 請求轉發的時候,url不會產生變化 307
  • 重定向時候,url位址列會發生變化; 302

6.8、request和response的總結

1、什麼是request、response

​ request物件是伺服器對瀏覽器請求的封裝,而response是伺服器對伺服器響應的封裝。

request用來取出請求資訊,而response則用來新增要返回給瀏覽器的資訊。

2、response用途

​ request用來取出請求資訊,而response則用來新增要返回給瀏覽器的資訊。

​ 實現請求重定向

​ 通過訪問Servlet向瀏覽器傳遞一個圖片

​ 通過訪問Servlet下載檔案

3、request用途

​ 通過request物件獲取http請求協議中的內容

​ request 請求轉發實現和request域

4、亂碼問題

##### 4.1、亂碼原因

由於瀏覽器預設使用UTF-8碼錶進行編碼,而servlet使用ISO-8859-1碼錶進行編碼,傳輸和接收方編碼不一致導致亂碼的產生。

4.2、位元組流的方式響應中文
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        /**
         * 使用位元組流響應中文
         */
        //ServletOutputStream outputStream = response.getOutputStream();
        //outputStream.write("中文".getBytes());
        /**
         * 以上這種方式是否能夠避免不出現中文亂碼問題呢?
         * 不一定:
         * 中文亂碼的產生與中文轉化成位元組陣列和瀏覽器開啟方式(開啟時採用的預設字符集)有關
         * 
         * 解決方案:
         * 將中文轉化成陣列的字符集和瀏覽器開啟時候的字符集一致就行
         */
        
        ServletOutputStream outputStream = response.getOutputStream();
        //設定瀏覽器開啟時預設的字符集
        response.setHeader("Conten-Type", "text/html;charset=UTF-8");
        //設定中文轉換成位元組組的字符集編碼
        outputStream.write("中文大師".getBytes("UTF-8"));
4.3、字元流響應中文
/**
         * 字元流的方式響應中文
         */
        response.getWriter().print("中文大俠");
        /**
         * 使用上述程式碼向頁面輸出中文是否會產生亂碼?  
         * 一定亂碼  
         * 原因: 
         * 字元流是有緩衝區的,response獲得字元流,response設計預設的緩衝區編碼是ISO-8859-1。 
         * 解決:  設定response獲得字元流緩衝區的編碼字符集(這句話體現在:response.getWriter(),詳情請看API)和
         *                 設定瀏覽器預設開啟時候採用的字符集一致即可。
         * 
         */
        
        // 設定瀏覽器開啟時預設的字符集
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        //設定response獲得字元流緩衝區的編碼字符集
        response.setCharacterEncoding("UTF-8");
        //響應到瀏覽器
        response.getWriter().print("胡春春和阿珠");

        //下面是一種簡化的方式(推薦記住和掌握)
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print("胡春春和阿珠");
4.4、post方式
/**
 *  演示Post方式提交,解決中文亂碼問題
 */
public class RequestDemo extends HttpServlet {
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 產生亂碼的原因:
         * post提交的資料在請求體中,request物件接收到資料之後,會將資料放入到request緩衝區,緩衝區有預設編碼:ISO-8859-1:不支援中文;
         * 
         * 解決方案:
         * 將request緩衝區的編碼更改就行了
         * 
         */
        request.setCharacterEncoding("UTF-8");
        String parameter = request.getParameter("name");
        System.out.println("姓名:"+parameter);
        
    }
4.4、get方式
/**
         * 產生亂碼的原因:
         * get提交的資料在請求行url後面,在位址列其實已經進行了一次編碼(預設:ISO-8859-1:對中文不友好)
         * 
         * 解決方案:
         * 將存入request緩衝區的資料以預設的方式接收,但以UTF-8的方式解碼
         */
        String name=request.getParameter("name");
        String param=new String(name.getBytes("ISO-8859-1"),"UTF-8");
        System.out.println("姓名:"+parameter);

7、Cookie、Session

7.1、會話

會話:使用者開啟一個瀏覽器,點選了很多超連結,訪問多個web資源,關閉瀏覽器,這個過程可以稱之為會話;

有狀態會話:一個同學來過教室,下次再來教室,我們會知道這個同學,曾經來過,稱之為有狀態會話;

你能怎麼證明你是西開的學生?

你 西開

  1. 發票 西開給你發票
  2. 學校登記 西開標記你來過了

一個網站,怎麼證明你來過?

客戶端 服務端

  1. 服務端給客戶端一個 信件,客戶端下次訪問服務端帶上信件就可以了; cookie
  2. 伺服器登記你來過了,下次你來的時候我來匹配你; seesion

7.2、儲存會話的兩種技術

cookie

  • 客戶端技術 (響應,請求)

session

  • 伺服器技術,利用這個技術,可以儲存使用者的會話資訊? 我們可以把資訊或者資料放在Session中!

常見常見:網站登入之後,你下次不用再登入了,第二次訪問直接就上去了!

7.3、Cookie

  1. 從請求中拿到cookie資訊
  2. 伺服器響應給客戶端cookie
Cookie[] cookies = req.getCookies(); //獲得Cookie
cookie.getName(); //獲得cookie中的key
cookie.getValue(); //獲得cookie中的vlaue
new Cookie("lastLoginTime", System.currentTimeMillis()+""); //新建一個cookie
cookie.setMaxAge(24*60*60); //設定cookie的有效期
resp.addCookie(cookie); //響應給客戶端一個cookie

cookie:一般會儲存在本地的 使用者目錄下 appdata;

一個網站cookie是否存在上限!聊聊細節問題

  • 一個Cookie只能儲存一個資訊;
  • 一個web站點可以給瀏覽器傳送多個cookie,最多存放20個cookie;
  • Cookie大小有限制4kb;
  • 300個cookie瀏覽器上限

刪除Cookie;

  • 不設定有效期,關閉瀏覽器,自動失效;
  • 設定有效期時間為 0 ;

編碼解碼:

URLEncoder.encode("秦疆","utf-8")
URLDecoder.decode(cookie.getValue(),"UTF-8")

7.4、Session(重點)

什麼是Session:

  • 伺服器會給每一個使用者(瀏覽器)建立一個Seesion物件;
  • 一個Seesion獨佔一個瀏覽器,只要瀏覽器沒有關閉,這個Session就存在;
  • 使用者登入之後,整個網站它都可以訪問!--> 儲存使用者的資訊;儲存購物車的資訊…..

Session和cookie的區別:

  • Cookie是把使用者的資料寫給使用者的瀏覽器,瀏覽器儲存 (可以儲存多個)
  • Session把使用者的資料寫到使用者獨佔Session中,伺服器端儲存 (儲存重要的資訊,減少伺服器資源的浪費)
  • Session物件由服務建立;

使用場景:

  • 儲存一個登入使用者的資訊;
  • 購物車資訊;
  • 在整個網站中經常會使用的資料,我們將它儲存在Session中;

使用Session:

package com.kuang.servlet;

import com.kuang.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class SessionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        //解決亂碼問題
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        
        //得到Session
        HttpSession session = req.getSession();
        //給Session中存東西
        session.setAttribute("name",new Person("秦疆",1));
        //獲取Session的ID
        String sessionId = session.getId();

        //判斷Session是不是新建立
        if (session.isNew()){
            resp.getWriter().write("session建立成功,ID:"+sessionId);
        }else {
            resp.getWriter().write("session以及在伺服器中存在了,ID:"+sessionId);
        }

        //Session建立的時候做了什麼事情;
//        Cookie cookie = new Cookie("JSESSIONID",sessionId);
//        resp.addCookie(cookie);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

//得到Session
HttpSession session = req.getSession();

Person person = (Person) session.getAttribute("name");

System.out.println(person.toString());

HttpSession session = req.getSession();
session.removeAttribute("name");
//手動登出Session
session.invalidate();

會話自動過期:web.xml配置

<!--設定Session預設的失效時間-->
<session-config>
    <!--15分鐘後Session自動失效,以分鐘為單位-->
    <session-timeout>15</session-timeout>
</session-config>

8、JSP

8.1、什麼是JSP

Java Server Pages : Java伺服器端頁面,也和Servlet一樣,用於動態Web技術!

最大的特點:

  • 寫JSP就像在寫HTML
  • 區別:
    • HTML只給使用者提供靜態的資料
    • JSP頁面中可以嵌入JAVA程式碼,為使用者提供動態資料;

8.2、JSP原理

思路:JSP到底怎麼執行的!

  • 程式碼層面沒有任何問題

  • 伺服器內部工作

    tomcat中有一個work目錄;

    IDEA中使用Tomcat的會在IDEA的tomcat中生產一個work目錄

    我電腦的地址:

    C:\Users\Administrator\.IntelliJIdea2018.1\system\tomcat\Unnamed_javaweb-session-cookie\work\Catalina\localhost\ROOT\org\apache\jsp
    

    發現頁面轉變成了Java程式!

瀏覽器向伺服器傳送請求,不管訪問什麼資源,其實都是在訪問Servlet!

JSP最終也會被轉換成為一個Java類!

JSP 本質上就是一個Servlet

//初始化
  public void _jspInit() {
      
  }
//銷燬
  public void _jspDestroy() {
  }
//JSPService
  public void _jspService(.HttpServletRequest request,HttpServletResponse response)
      
  1. 判斷請求

  2. 內建一些物件

    final javax.servlet.jsp.PageContext pageContext;  //頁面上下文
    javax.servlet.http.HttpSession session = null;    //session
    final javax.servlet.ServletContext application;   //applicationContext
    final javax.servlet.ServletConfig config;         //config
    javax.servlet.jsp.JspWriter out = null;           //out
    final java.lang.Object page = this;               //page:當前
    HttpServletRequest request                        //請求
    HttpServletResponse response                      //響應
    
  3. 輸出頁面前增加的程式碼

    response.setContentType("text/html");       //設定響應的頁面型別
    pageContext = _jspxFactory.getPageContext(this, request, response,
                                              null, true, 8192, true);
    _jspx_page_context = pageContext;
    application = pageContext.getServletContext();
    config = pageContext.getServletConfig();
    session = pageContext.getSession();
    out = pageContext.getOut();
    _jspx_out = out;
    
  4. 以上的這些個物件我們可以在JSP頁面中直接使用!

在JSP頁面中;

只要是 JAVA程式碼就會原封不動的輸出;

如果是HTML程式碼,就會被轉換為:

out.write("<html>\r\n");

這樣的格式,輸出到前端!

8.3、JSP基礎語法

任何語言都有自己的語法,JAVA中有,。 JSP 作為java技術的一種應用,它擁有一些自己擴充的語法(瞭解,知道即可!),Java所有語法都支援!

JSP表示式

  <%--JSP表示式
  作用:用來將程式的輸出,輸出到客戶端
  <%= 變數或者表示式%>
  --%>
  <%= new java.util.Date()%>

jsp指令碼片段

  <%--jsp指令碼片段--%>
  <%
    int sum = 0;
    for (int i = 1; i <=100 ; i++) {
      sum+=i;
    }
    out.println("<h1>Sum="+sum+"</h1>");
  %>

指令碼片段的再實現

  <%
    int x = 10;
    out.println(x);
  %>
  <p>這是一個JSP文件</p>
  <%
    int y = 2;
    out.println(y);
  %>

  <hr>


  <%--在程式碼嵌入HTML元素--%>
  <%
    for (int i = 0; i < 5; i++) {
  %>
    <h1>Hello,World  <%=i%> </h1>
  <%
    }
  %>

JSP宣告

  <%!
    static {
      System.out.println("Loading Servlet!");
    }

    private int globalVar = 0;

    public void kuang(){
      System.out.println("進入了方法Kuang!");
    }
  %>

JSP宣告:會被編譯到JSP生成Java的類中!其他的,就會被生成到_jspService方法中!

在JSP,嵌入Java程式碼即可!

<%! %>:是jsp中的宣告標籤,通常宣告全域性變數,常量,方法等。

<% %>:<% java程式碼 %>,其中可以包含區域性變數,java語句等。

<%= %>:<%=java 程式碼%>,可以在客戶端輸出。

<%--註釋--%>

JSP的註釋,不會在客戶端顯示,HTML就會!

8.4、JSP指令

<%@page args.... %>
<%@include file=""%>

<%--@include會將兩個頁面合二為一--%>

<%@include file="common/header.jsp"%>
<h1>網頁主體</h1>

<%@include file="common/footer.jsp"%>

<hr>


<%--jSP標籤
    jsp:include:拼接頁面,本質還是三個
    --%>
<jsp:include page="/common/header.jsp"/>
<h1>網頁主體</h1>
<jsp:include page="/common/footer.jsp"/>

8.5、9大內建物件

  • PageContext 存東西
  • Request 存東西
  • Response
  • Session 存東西
  • Application 【SerlvetContext】 存東西
  • config 【SerlvetConfig】
  • out
  • page ,不用瞭解
  • exception
pageContext.setAttribute("name1","秦疆1號"); //儲存的資料只在一個頁面中有效
request.setAttribute("name2","秦疆2號"); //儲存的資料只在一次請求中有效,請求轉發會攜帶這個資料
session.setAttribute("name3","秦疆3號"); //儲存的資料只在一次會話中有效,從開啟瀏覽器到關閉瀏覽器
application.setAttribute("name4","秦疆4號");  //儲存的資料只在伺服器中有效,從開啟伺服器到關閉伺服器

request:客戶端向伺服器傳送請求,產生的資料,使用者看完就沒用了,比如:新聞,使用者看完沒用的!

session:客戶端向伺服器傳送請求,產生的資料,使用者用完一會還有用,比如:購物車;

application:客戶端向伺服器傳送請求,產生的資料,一個使用者用完了,其他使用者還可能使用,比如:聊天資料;

8.6、JSP標籤、JSTL標籤、EL表示式

<!-- JSTL表示式的依賴 -->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl-api</artifactId>
    <version>1.2</version>
</dependency>
<!-- standard標籤庫 -->
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

EL表示式: ${ }

  • 獲取資料
  • 執行運算
  • 獲取web開發的常用物件
  • EL輸出一個常量的話,字串要加雙引號,不然的話EL會預設把你認為的常量當做一個變數來處理,
  • 要存取的屬性名稱中包含一些特殊字元,如. 或 – 等並非字母或數字的符號,就一定要使用 [ ],
  • EL表示式只能通過內建物件取值,也就是隻讀操作,如果想進行寫操作的話就讓後臺程式碼去完成,畢竟EL表示式僅僅是檢視上的輸出標籤罷了。

JSP標籤

<%--jsp:include--%>

<%--
http://localhost:8080/jsptag.jsp?name=kuangshen&age=12
--%>

<jsp:forward page="/jsptag2.jsp">
    <jsp:param name="name" value="kuangshen"></jsp:param>
    <jsp:param name="age" value="12"></jsp:param>
</jsp:forward>

JSTL表示式

JSTL標籤庫的使用就是為了彌補HTML標籤的不足;它自定義許多標籤,可以供我們使用,標籤的功能和Java程式碼一樣!

格式化標籤

SQL標籤

XML 標籤

核心標籤 (掌握部分)

JSTL標籤庫使用步驟

  • 引入對應的 taglib
  • 使用其中的方法
  • 在Tomcat 也需要引入 jstl的包,否則會報錯:JSTL解析錯誤

c:if

<head>
    <title>Title</title>
</head>
<body>


<h4>if測試</h4>

<hr>

<form action="coreif.jsp" method="get">
    <%--
    EL表示式獲取表單中的資料
    ${param.引數名}
    --%>
    <input type="text" name="username" value="${param.username}">
    <input type="submit" value="登入">
</form>

<%--判斷如果提交的使用者名稱是管理員,則登入成功--%>
<c:if test="${param.username=='admin'}" var="isAdmin">
    <c:out value="管理員歡迎您!"/>
</c:if>

<%--自閉合標籤--%>
<c:out value="${isAdmin}"/>

</body>

c:choose c:when

<body>

<%--定義一個變數score,值為85--%>
<c:set var="score" value="55"/>

<c:choose>
    <c:when test="${score>=90}">
        你的成績為優秀
    </c:when>
    <c:when test="${score>=80}">
        你的成績為一般
    </c:when>
    <c:when test="${score>=70}">
        你的成績為良好
    </c:when>
    <c:when test="${score<=60}">
        你的成績為不及格
    </c:when>
</c:choose>

</body>

c:forEach

<%

    ArrayList<String> people = new ArrayList<>();
    people.add(0,"張三");
    people.add(1,"李四");
    people.add(2,"王五");
    people.add(3,"趙六");
    people.add(4,"田六");
    request.setAttribute("list",people);
%>


<%--
var , 每一次遍歷出來的變數
items, 要遍歷的物件
begin,   哪裡開始
end,     到哪裡
step,   步長
--%>
<c:forEach var="people" items="${list}">
    <c:out value="${people}"/> <br>
</c:forEach>

<hr>

<c:forEach var="people" items="${list}" begin="1" end="3" step="1" >
    <c:out value="${people}"/> <br>
</c:forEach>

9、JavaBean

實體類

JavaBean有特定的寫法:

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

一般用來和資料庫的欄位做對映 ORM;

ORM :物件關係對映

  • 表--->類
  • 欄位-->屬性
  • 行記錄---->物件

people表

id name age address
1 秦疆1號 3 西安
2 秦疆2號 18 西安
3 秦疆3號 100 西安
class People{
    private int id;
    private String name;
    private int id;
    private String address;
}

class A{
    new People(1,"秦疆1號",3,"西安");
    new People(2,"秦疆2號",3,"西安");
    new People(3,"秦疆3號",3,"西安");
}
  • 過濾器
  • 檔案上傳
  • 郵件傳送
  • JDBC 複習 : 如何使用JDBC , JDBC crud, jdbc 事務

10、MVC三層架構

什麼是MVC: Model view Controller 模型、檢視、控制器

10.1、早些年

使用者直接訪問控制層,控制層就可以直接操作資料庫;

servlet--CRUD-->資料庫
弊端:程式十分臃腫,不利於維護  
servlet的程式碼中:處理請求、響應、檢視跳轉、處理JDBC、處理業務程式碼、處理邏輯程式碼

架構:沒有什麼是加一層解決不了的!
程式猿呼叫
|
JDBC
|
Mysql Oracle SqlServer ....

10.2、MVC三層架構

Model

  • 業務處理 :業務邏輯(Service)
  • 資料持久層:CRUD (Dao)

View

  • 展示資料
  • 提供連結發起Servlet請求 (a,form,img…)

Controller (Servlet)

  • 接收使用者的請求 :(req:請求引數、Session資訊….)

  • 交給業務層處理對應的程式碼

  • 控制檢視的跳轉

    登入--->接收使用者的登入請求--->處理使用者的請求(獲取使用者登入的引數,username,password)---->交給業務層處理登入業務(判斷使用者名稱密碼是否正確:事務)--->Dao層查詢使用者名稱和密碼是否正確-->資料庫
    

11、Filter (重點)

Filter:過濾器 ,用來過濾網站的資料;

  • 處理中文亂碼
  • 登入驗證….

Filter開發步驟:

  1. 導包

  2. 編寫過濾器

    1. 導包不要錯

      實現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.kuang.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <!--只要是 /servlet的任何請求,會經過這個過濾器-->
        <url-pattern>/servlet/*</url-pattern>
        <!--<url-pattern>/*</url-pattern>-->
    </filter-mapping>
    

12、監聽器

實現一個監聽器的介面;(有N種)

  1. 編寫一個監聽器

    實現監聽器的介面…

    //統計網站線上人數 : 統計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.kuang.listener.OnlineCountListener</listener-class>
    </listener>
    
  3. 看情況是否使用!

13、過濾器、監聽器常見應用

監聽器:GUI程式設計中經常使用;

public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame("中秋節快樂");  //新建一個窗體
        Panel panel = new Panel(null); //面板
        frame.setLayout(null); //設定窗體的佈局

        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(0,0,255)); //設定背景顏色

        panel.setBounds(50,50,300,300);
        panel.setBackground(new Color(0,255,0)); //設定背景顏色

        frame.add(panel);

        frame.setVisible(true);

        //監聽事件,監聽關閉事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
            }
        });


    }
}

使用者登入之後才能進入主頁!使用者登出後就不能進入主頁了!

  1. 使用者登入之後,向Sesison中放入使用者的資料

  2. 進入主頁的時候要判斷使用者是否已經登入;要求:在過濾器中實現!

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;
    
    if (request.getSession().getAttribute(Constant.USER_SESSION)==null){
        response.sendRedirect("/error.jsp");
    }
    
    chain.doFilter(request,response);
    

14、JDBC

什麼是JDBC : Java連線資料庫!

需要jar包的支援:

  • java.sql
  • javax.sql
  • mysql-conneter-java… 連線驅動(必須要匯入)

實驗環境搭建

CREATE TABLE users(
    id INT PRIMARY KEY,
    `name` VARCHAR(40),
    `password` VARCHAR(40),
    email VARCHAR(60),
    birthday DATE
);

INSERT INTO users(id,`name`,`password`,email,birthday)
VALUES(1,'張三','123456','[email protected]','2000-01-01');
INSERT INTO users(id,`name`,`password`,email,birthday)
VALUES(2,'李四','123456','[email protected]','2000-01-01');
INSERT INTO users(id,`name`,`password`,email,birthday)
VALUES(3,'王五','123456','[email protected]','2000-01-01');


SELECT	* FROM users;

匯入資料庫依賴

<!--mysql的驅動-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

IDEA中連線資料庫:

JDBC 固定步驟:

  1. 載入驅動
  2. 連線資料庫,代表資料庫
  3. 向資料庫傳送SQL的物件Statement : CRUD
  4. 編寫SQL (根據業務,不同的SQL)
  5. 執行SQL
  6. 關閉連線
public class TestJdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置資訊
        //useUnicode=true&characterEncoding=utf-8 解決中文亂碼
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123456";

        //1.載入驅動
        Class.forName("com.mysql.jdbc.Driver");
        //2.連線資料庫,代表資料庫
        Connection connection = DriverManager.getConnection(url, username, password);

        //3.向資料庫傳送SQL的物件Statement,PreparedStatement : CRUD
        Statement statement = connection.createStatement();

        //4.編寫SQL
        String sql = "select * from users";

        //5.執行查詢SQL,返回一個 ResultSet  : 結果集
        ResultSet rs = statement.executeQuery(sql);

        while (rs.next()){
            System.out.println("id="+rs.getObject("id"));
            System.out.println("name="+rs.getObject("name"));
            System.out.println("password="+rs.getObject("password"));
            System.out.println("email="+rs.getObject("email"));
            System.out.println("birthday="+rs.getObject("birthday"));
        }

        //6.關閉連線,釋放資源(一定要做) 先開後關
        rs.close();
        statement.close();
        connection.close();
    }
}

預編譯SQL

public class TestJDBC2 {
    public static void main(String[] args) throws Exception {
        //配置資訊
        //useUnicode=true&characterEncoding=utf-8 解決中文亂碼
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123456";

        //1.載入驅動
        Class.forName("com.mysql.jdbc.Driver");
        //2.連線資料庫,代表資料庫
        Connection connection = DriverManager.getConnection(url, username, password);

        //3.編寫SQL
        String sql = "insert into  users(id, name, password, email, birthday) values (?,?,?,?,?);";
 
        //4.預編譯
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setInt(1,2);//給第一個佔位符? 的值賦值為1;
        preparedStatement.setString(2,"狂神說Java");//給第二個佔位符? 的值賦值為狂神說Java;
        preparedStatement.setString(3,"123456");//給第三個佔位符? 的值賦值為123456;
        preparedStatement.setString(4,"[email protected]");//給第四個佔位符? 的值賦值為1;
        preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));//給第五個佔位符? 的值賦值為new Date(new java.util.Date().getTime());

        //5.執行SQL
        int i = preparedStatement.executeUpdate();

        if (i>0){
            System.out.println("插入成功@");
        }

        //6.關閉連線,釋放資源(一定要做) 先開後關
        preparedStatement.close();
        connection.close();
    }
}

事務

要麼都成功,要麼都失敗!

ACID原則:保證資料的安全。

開啟事務  connection.setAutoCommit(false)
事務提交  commit()
事務回滾  rollback()
關閉事務

轉賬:
A:1000
B:1000
    
A(900)   --100-->   B(1100) 

Junit單元測試

依賴

<!--單元測試-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

簡單使用

@Test註解只有在方法上有效,只要加了這個註解的方法,就可以直接執行!

@Test
public void test(){
    System.out.println("Hello");
}

失敗的時候是紅色:

搭建一個環境

CREATE TABLE account(
   id INT PRIMARY KEY AUTO_INCREMENT,
   `name` VARCHAR(40),
   money FLOAT
);

INSERT INTO account(`name`,money) VALUES('A',1000);
INSERT INTO account(`name`,money) VALUES('B',1000);
INSERT INTO account(`name`,money) VALUES('C',1000);
    @Test
    public void test() {
        //配置資訊
        //useUnicode=true&characterEncoding=utf-8 解決中文亂碼
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
        String username = "root";
        String password = "123456";

        Connection connection = null;

        //1.載入驅動
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.連線資料庫,代表資料庫
             connection = DriverManager.getConnection(url, username, password);

            //3.通知資料庫開啟事務,false 開啟
            connection.setAutoCommit(false);

            String sql = "update account set money = money-100 where name = 'A'";
            connection.prepareStatement(sql).executeUpdate();

            //製造錯誤
            //int i = 1/0;

            String sql2 = "update account set money = money+100 where name = 'B'";
            connection.prepareStatement(sql2).executeUpdate();

            connection.commit();//以上兩條SQL都執行成功了,就提交事務!
            System.out.println("success");
        } catch (Exception e) {
            try {
                //如果出現異常,就通知資料庫回滾事務
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

9、上傳檔案

package un.sdu.servlet;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

public class FileServlet extends javax.servlet.http.HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判斷檔案是普通表單還是帶檔案的表單
        if(!ServletFileUpload.isMultipartContent(req)){
            return;//終止方法執行;說明這是一個普通的表單,直接返回
        }
        //建立上傳檔案的儲存路徑,建議在WEB-INF路徑下,安全,使用者無法直接訪問上傳的檔案;
        String uploadPath = this.getServletContext().getRealPath("WEB_INF/upload");
        File uploadfile= new File(uploadPath) ;
        if(!uploadfile.exists()){
            uploadfile.mkdir();
        }

        //快取;臨時檔案
        //臨時路徑,假如檔案超過了預期的大小,我們就把它放到一個臨時檔案中,過幾天自動刪除,或者提醒使用者轉存為永久
        String tepPath = this.getServletContext().getRealPath("WEB_INF/upload");
        File file= new File(tepPath) ;
        if(!file.exists()){
            file.mkdir();
        }

        //處理上傳的檔案,一般都需要通過流來獲取,我們可以使用request.getInputStream(),原生態的檔案上傳流來獲取,十分麻煩
        //但是我們都建議使用Apache的檔案上傳元件來實現,common-fileupload,它需要依賴於commons-io元件;

        //1、建立DiskFileItemFactory物件,處理檔案上傳路徑或者大小限制的;
       DiskFileItemFactory factory=getDiskFileItemFactory(file);

        //2、獲取ServletFileUpload
       ServletFileUpload upload=getServletFileUpload(factory);

        //3、處理上傳檔案
        //把前端請求解析,封裝成一個FileItem物件,需要從ServletFileUpload物件的獲取
        String msg=uploadParseRequest(upload,req,uploadPath);
        req.setAttribute("msg",msg);
        req.getRequestDispatcher("info.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    public static DiskFileItemFactory getDiskFileItemFactory(File file){
        //1、建立DiskFileItemFactory物件,處理檔案上傳路徑或者大小限制的;
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //通過這個工廠設定一個緩衝區,當上傳的檔案大於這個緩衝區的時候,將他放到臨時檔案中;
        factory.setSizeThreshold(1024*1024);//緩衝區大小為1M
        factory.setRepository(file);
        return  factory;
    }
    public static ServletFileUpload getServletFileUpload(DiskFileItemFactory factory){
        ServletFileUpload upload=new ServletFileUpload(factory);

        //監聽檔案上傳進度
        upload.setProgressListener(new ProgressListener() {
            @Override
            public void update(long pBytesRead, long pContentLength, int PItems) {
                System.out.println("總大小:"+pContentLength+"已上傳:"+pBytesRead);
            }
        });
        //處理亂碼問題
        upload.setHeaderEncoding("UTF-8");
        //設定單個檔案的最大值
        upload.setFileSizeMax(1024*1024*10);
        //設定總共能夠上傳檔案的大小
        upload.setSizeMax(1024*1024*10);
        return upload;
    }
    public static String uploadParseRequest(ServletFileUpload upload,HttpServletRequest request,String uploadPath) {
        String  msg="檔案上傳失敗";
        try {

            List<FileItem> fileItems = upload.parseRequest(request);
            for (FileItem fileItem : fileItems) {
                //判斷上傳的檔案是普通的表單還是帶檔案的表單
                if (fileItem.isFormField()) {//不是檔案
                    String name = fileItem.getFieldName();
                    String value = fileItem.getString("UTF-8");
                    System.out.println(name + ":" + value);
                } else {//是檔案
                    //-------------處理檔案-----------------------
                    String uploadFileName = fileItem.getName();
                    //排除可能存在的檔名不合法的情況
                    if (uploadFileName.trim().equals("") || uploadFileName == null) {
                        continue;
                    }
                    //獲得上傳的檔名
                    String fileName = uploadFileName.substring(uploadFileName.lastIndexOf("/") + 1);
                    //獲得字尾名
                    String fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);

                    //可以使用UUID(唯一識別的通用碼),保證檔名唯一;
                    //UUID.randomUUID(),隨機生一個唯一識別的通用碼
                    String uuidpath = UUID.randomUUID().toString();
                    //-------------存放地址-----------------------
                    //存在哪裡?uploadPath
                    //檔案真實存在的路徑:realPath
                    String realPath = uploadPath + "/" + uuidpath;
                    File realPathFile = new File(realPath);
                    if (!realPathFile.exists()) {
                        realPathFile.mkdir();
                    }
                    //-------------檔案傳輸-----------------------
                    //獲得檔案上傳的流
                    InputStream inputStream = fileItem.getInputStream();

                    //建立一個檔案輸出流
                    //realPath=真實資料夾;
                    //差了一個檔案;加上輸出檔案的名字+"/"+uuidFileName
                    FileOutputStream fos = new FileOutputStream(realPath + "/" + fileName);

                    //建立一個緩衝區
                    byte[] buffer = new byte[1024 * 1024];
                    //判斷是否讀取完畢
                    int len = 0;
                    //如果大於零,說明還有資料
                    while ((len = inputStream.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                    //關閉流
                    fos.close();
                    inputStream.close();

                    msg = "檔案上傳成功";
                    fileItem.delete();//上傳成功,清除臨時檔案
                }
            }

        } catch (FileUploadException | IOException e) {
            e.printStackTrace();
        }
        return  msg;
    }
}

10、傳送郵件

1、傳送簡單郵件

package un.sdu;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

//傳送一封簡單的郵件
public class MailDemo01 {
    public static void main(String[] args) throws Exception {

        Properties prop=new Properties();
        prop.setProperty("mail.host","smtp.qq.com");//設定QQ郵箱伺服器
        prop.setProperty("mail.transport.protocol","smtp");//郵箱傳送協議
        prop.setProperty("mail.smtp.auth","true");//需要驗證使用者名稱密碼

        //關於QQ郵箱,還要設定SSL加密
        MailSSLSocketFactory sf=new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable","true");
        prop.put("mail.smtp.ssl.socketFactory",sf);

        //使用javaMail傳送簡單郵件的五個步驟
        //1、建立定義整個應用程式所需的環境資訊的Session物件
        Session session=Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //發件人郵箱使用者名稱、授權碼
                return new PasswordAuthentication("發件人郵箱","授權碼");
            }
        });
        //開啟Session的debug模式,這樣就可以檢視到程式傳送Email的執行狀態
        session.setDebug(true);
        //2、通過session得到transport物件
        Transport ts=session.getTransport();
        //3、使用郵件的使用者名稱和授權碼連上郵件伺服器
        ts.connect("郵箱伺服器","發件人郵箱","授權碼");
        //4、建立郵件
            //建立郵箱物件
        MimeMessage message=new MimeMessage(session);
            //指明郵件的發件人
        message.setFrom(new InternetAddress("發件人郵箱"));
            //指明郵件的收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("收件人郵箱"));
            //設定郵件標題
        message.setSubject("標題");
            //設定郵件內容
        message.setText("內容");
        
        //5、傳送郵件
        ts.sendMessage(message,message.getAllRecipients());
        ts.close();
    }
}