天天看點

Servlet 第三方jar包使用1servlet建立方式三(模闆 設計模式)(重點)

建立一個Dynamic  Web  Project工程
然後在他的,工程下的src檔案夾下.建立.Class類

利用Servers 添加apache-tomcat-.M26.zip
就可以運作了.
           
apache-tomcat-.M26檔案夾下的檔案是什麼含義
backu: 是第一次配置檔案成功後生成的檔案夾
bin檔案夾:可執行檔案(打開關閉伺服器檔案)可能用到的shutdown.sh檔案  startup.sh 
cinf檔案夾: 伺服器的配置檔案 可能用到的web.xml
lib檔案夾: 伺服器使用(依賴)的Jar包
logs檔案夾: 日志檔案
temp 檔案夾 : tomcat産生的臨時檔案
webapps 檔案夾:系統自帶的 預設的存放項目的工程(應用程式)檔案夾 
work 檔案夾 : 伺服器自己的工作空間
wtpwebapps : 與 Eclipse 關聯成功後 産生的存放工程的檔案夾
           

使用終端

使用終端進入tomcat 伺服器下單bin檔案夾下.

在終端中 輸入:cd //Users/lanou/apache-tomcat-.M26/bin
擷取檔案夾權限
sudo chmod *.sh
輸入密碼;電腦密碼
password
執行啟動伺服器
sudo sh ./startup.sh
通路網址(url:全球網址)
http://localhost:(tomcat 的首頁)
協定://本地位址:端口号/項目名(應用程式名)/通路的資源
           

使用者如何從浏覽器通路到Servlet類???

Servlet 小服務程式
Servlet 是個Java類
Servlet 是個接口
使用者用浏覽器通路伺服器(tomcat)
伺服器通過網址可以找到 對應項目的 web.XML檔案
解析XML檔案擷取到對應的實作類,利用實作類建立對象.
    通過網址找到對象的servlet-name
    通過servlet-name找到對應的 servlet類
    建立servlet對象
執行生命周期的幾個方法
執行個體化-->初始化-->service服務-->銷毀
           

建立servlet的方式一 利用實作類

聲明周期方法以下四個

public class Demo01 implements Servlet{
 //1.執行個體化方法
 public Demo01(){
     super();
     System.out.println("我是執行個體化方法");
 }
 //2.初始化方法
 public void init(ServletConfig arg0)throws ServletException{
      System.out.println("我是初始化方法");
 }
 //3.服務方法Service
 public void service(ServletRequest arg0 , ServletResponse arg)throws  ServletException, IOException{
     System.out.println("我是服務方法Service");
 }
 //4.銷毀方法destory
 public void destroy(){
     System.out.println("我是銷毀方法destory");
 }
           

建立Servlet的方式二(擴充卡模式建立 遙控器)

使用那個方法就重寫那個方法

用不上的方法可以選擇性的不重寫

public class Demo02 extends GenericServlet {
        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("我是demo02");

        }
}
           

servlet建立方式三(模闆 設計模式)(重點)

常用方法(必須會)

public class Demo03 extends HttpServlet{
        //聲明一個ServletConfig對象 當做成員變量
        private ServletConfig config;

    //初始化方法(通過該方法的參數  擷取ServletConfig對象)
    //ServletConfig對象儲存的是servlet中的配置資訊
        @Override
        public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        super.init(config);
        //接收一下ServletConfig
        this.config =config;
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //擷取servle的配置資訊
//          getInitParameter(name);
            //參數是配置時的name(相當于key)
            //用key擷取對應的value
            String value = this.config.getInitParameter("encoding");
            // TODO Auto-generated method stub
            //調用父類的doGet方法 怎麼着都會報錯  
            //不是400  就是405
            //是以重寫 doGet方法 注意:不要調 super.doGet(req, resp);  要記得删掉
//          super.doGet(req, resp);
            System.out.println("我是Demo03");
            //寫要操作的邏輯(重點)

        }
        //接收post 請求
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
            //調用父類的doGet方法 怎麼着都會報錯  
            //不是400  就是405
            //是以重寫 doGet方法 注意:不要調 super.doPost(req, resp); 要記得删掉
//      super.doPost(req, resp);

        }
}
           

擷取ServletConfig方式一

public class Demo04  extends HttpServlet{
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                // TODO Auto-generated method stub
                //通過父類中的方法擷取配置資訊(擷取ServletConfig對象)
                String config = this.getServletConfig().getInitParameter("encoding");
                System.out.println(config);
            }
            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // TODO Auto-generated method stub
                doGet(req, resp);
            }
}
           

擷取ServletConfig方式一

/*
 * 域對象
 * 存儲(在一定範圍内,資訊的對象)
 * ServletContext範圍: 是整個程式中 都可以通路到 并且隻有一個 單例對象
 * 每個servlet都可以通路到這個域對象
 * 如何擷取ServletContext 對象?
 * 方式1:從SerletConfig對象中擷取
 * 方式2:從父類中直接或區域
 * 注意:所有的域對象  都有/設定/擷取/删除的方法
 * 
 */
public class Demo05 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //從ServletConfig對象中擷取
    ServletContext application = this.getServletConfig().getServletContext();
    //添加一個資料到context域中
    //相當于添加一個鍵值對  key  和 value 
    application.setAttribute("username", "wanglong");
    }

}
           
public class Demo06 extends  HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //擷取域對象
            Object object = this.getServletContext().getAttribute("username");
            System.out.println(object);
        }
}
           

Servlet 第三方jar包使用2

https://blog.csdn.net/qq_36390044/article/details/79720942

繼續閱讀