欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

day15-Servlet04

Servlet04

12.ServletConfig

12.1ServletConfig基本介紹

  1. ServletConfig類是為Servlet程序配置信息的類
  2. Servlet對(duì)象和ServletConfig對(duì)象都是由Tomcat負(fù)責(zé)創(chuàng)建
  3. Servlet對(duì)象默認(rèn)是第一次訪問(wèn)的時(shí)候創(chuàng)建,ServletConfig在Servlet對(duì)象創(chuàng)建的時(shí)候,就創(chuàng)建一個(gè)對(duì)應(yīng)的ServletConfig對(duì)象

12.2ServletConfig作用

  1. 獲取Servlet程序的servlet-name的值
  2. 獲取初始化參數(shù)init-param
  3. 獲取ServletContext對(duì)象(上下文對(duì)象)

12.3ServletConfig應(yīng)用實(shí)例

例子

目前創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、秦都網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

需求:編寫(xiě)DBServlet.java,完成如下功能

  1. 在web.xml配置連接mysql的用戶名和密碼

  2. 在DBServlet執(zhí)行doGet()或者doPost()時(shí),可以獲取到web.xml配置的用戶名和密碼

  3. 思路分析:

    瀏覽器發(fā)送請(qǐng)求,Tomcat去創(chuàng)建DBServlet,DBServlet去web.xml文件中去獲取配置的參數(shù),獲取的方法有兩種:一是使用dom4j,二是使用ServletConfig類

web.xml配置Servlet:

<!--配置DBServlet-->
<servlet>
    <servlet-name>DBServlet</servlet-name>
    <servlet-class>servlet.DBServlet</servlet-class>
    <!--為該Servlet配置初始參數(shù)-->
    <init-param>
        <!--參數(shù)名-->
        <param-name>username</param-name>
        <!--參數(shù)值-->
        <param-value>jack</param-value>
    </init-param>
    <init-param>
        <param-name>pwd</param-name>
        <param-value>123456</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>DBServlet</servlet-name>
    <url-pattern>/db</url-pattern>
</servlet-mapping>

DBServlet:

package servlet;

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

public class DBServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //在DBServlet執(zhí)行doGet()或者doPost()時(shí),可以獲取到web.xml配置的用戶名和密碼
        //DBServlet的父類GenericServlet有方法getServletConfig()
        /**
         * 1.getServletConfig()是父類GenericServlet的
         * 2.返回的ServletConfig對(duì)象是GenericServlet的private transient ServletConfig config
         * 3.當(dāng)一個(gè)屬性被transient修飾,表示該屬性不會(huì)被串行化(有些重要信息不希望保存到文件中)
         */
        ServletConfig servletConfig = getServletConfig();
        String username = servletConfig.getInitParameter("username");
        String pwd = servletConfig.getInitParameter("pwd");
        System.out.println("初始化參數(shù)username=" + username);
        System.out.println("初始化參數(shù)pwd=" + pwd);
    }
}

瀏覽器訪問(wèn)DBServlet時(shí),后臺(tái)輸出:


問(wèn)題一:在doPost方法中可以得到servletConfig,在doGet方法也可以得到servletConfig,那么這兩個(gè)servletConfig是同一個(gè)servletConfig嗎?

答:是同一個(gè)servletConfig。

先來(lái)看一個(gè)例子

在上述的DBServlet中重寫(xiě)init方法,并且分別在init和doPost方法中輸出ServletConfig對(duì)象

package servlet;

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

public class DBServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("init()=" + config);
    }

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

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        ServletConfig servletConfig = getServletConfig();
        System.out.println("doPost()=" + servletConfig);
        String username = servletConfig.getInitParameter("username");
        String pwd = servletConfig.getInitParameter("pwd");
        System.out.println("初始化參數(shù)username=" + username);
        System.out.println("初始化參數(shù)pwd=" + pwd);
    }
}

redeployTomcat,在瀏覽器重新訪問(wèn)DBServlet,會(huì)發(fā)現(xiàn)出現(xiàn)了500錯(cuò)誤,這表明服務(wù)器內(nèi)部運(yùn)行出現(xiàn)錯(cuò)誤

查看控制臺(tái)輸出,發(fā)現(xiàn)doPost方法竟然輸出了null

在DBServlet中的init方法加上語(yǔ)句super.init(config);

redeployTomcat,重新訪問(wèn)瀏覽器,會(huì)發(fā)現(xiàn)訪問(wèn)DBServlet成功,后臺(tái)輸出變正常了

問(wèn)題二:這是為什么呢?


我們先來(lái)梳理ServletConfig config的使用流程

  1. 當(dāng)DBServlet對(duì)象初始化時(shí),Tomcat會(huì)同時(shí)創(chuàng)建一個(gè)ServletConfig對(duì)象

  2. 如果DBServlet init()方法中調(diào)用了super.init(config);

  3. 就會(huì)調(diào)用父類GenericServlet的init方法:

    public void init(ServletConfig config) throws ServletException {
    	this.config = config;
    	this.init();
    }
    

    這時(shí)就會(huì)把Tomcat創(chuàng)建的ServletConfig對(duì)象賦給GenericServlet的屬性config

  4. 因此如果要重寫(xiě)init()方法,記住如果你想在其他方法通過(guò)getServletConfig()獲取ServletConfig,則一定要記住調(diào)用super.init(config);


回到問(wèn)題二:

如果沒(méi)有把tomcat創(chuàng)建的ServletConfig,賦值給GenericServlet的屬性config。那么GenericServlet的屬性config的值就為null,而doPost或者doGet方法通過(guò)getServletConfig()拿到的就是GenericServlet的屬性config,因此就會(huì)輸出null。

側(cè)面證實(shí)了方法中獲取的servletConfig是同一個(gè)對(duì)象(問(wèn)題一)

因此上面的例子中,瀏覽器訪問(wèn)DBServlet,發(fā)現(xiàn)出現(xiàn)了500錯(cuò)誤的原因是,doPost方法中獲取了為null的ServletConfig對(duì)象中的屬性

13.ServletContext

13.1為什么需要ServletContext

先來(lái)看一個(gè)需求:如果我們希望統(tǒng)計(jì)某個(gè)web應(yīng)用的所有Servlet被訪問(wèn)的次數(shù),怎么辦?

方案一:使用DB

方案二:使用ServletContext

13.2ServletContext基本介紹

  1. ServletContext是一個(gè)接口,它表示Servlet上下文對(duì)象

  2. 一個(gè)web工程中,只有一個(gè)ServletContext對(duì)象實(shí)例

  3. ServletContext對(duì)象是在web工程啟動(dòng)的時(shí)候創(chuàng)建的,在web工程停止的時(shí)候銷毀

  4. 可以通過(guò)ServletConfig.getServletContext方法獲得對(duì)ServletContext對(duì)象的應(yīng)用,也可以通過(guò)this.getServletContext()來(lái)獲得其對(duì)象的引用

  5. 由于一個(gè)web應(yīng)用中的所有Servlet共享一個(gè)ServletContext對(duì)象,因此Servlet對(duì)象之間可以通過(guò)ServletContext對(duì)象來(lái)實(shí)現(xiàn)多個(gè)Servlet間的通信。ServletContext對(duì)象通常也被稱為域?qū)ο蟆?/p>

13.3ServletContext可以做什么

  1. 獲取web.xml文件中配置的上下文參數(shù)context-param [信息和整個(gè)web應(yīng)用相關(guān),而不是屬于某個(gè)Servlet]

  2. 獲取當(dāng)前的工程路徑,格式:/工程路徑

  3. 獲取工程部署后在服務(wù)器硬盤上的絕對(duì)路徑

    比如 D:\IDEA-workspace\servlet\out\artifacts\servlet_war_exploded

  4. 向Map一樣存取數(shù)據(jù),多個(gè)Servlet共享數(shù)據(jù)

13.4應(yīng)用實(shí)例

13.4.1應(yīng)用實(shí)例1-獲取工程相關(guān)信息

需求如下:

  1. 獲取web.xml中配置的上下文參數(shù)context-param
  2. 獲取當(dāng)前的工程路徑,格式:/工程路徑
  3. 獲取工程部署后在服務(wù)器硬盤上的絕對(duì)路徑

配置ServletContext_: 在web.xml文件增加相關(guān)配置

<!--配置ServletContext_-->
<servlet>
    <servlet-name>ServletContext_</servlet-name>
    <servlet-class>com.li.servlet.servletcontext.ServletContext_</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ServletContext_</servlet-name>
    <url-pattern>/servletContext_</url-pattern>
</servlet-mapping>

<!--配置整個(gè)網(wǎng)站的信息-->
<context-param>
    <param-name>website</param-name>
    <param-value>http://www.lili.net</param-value>
</context-param>
<context-param>
    <param-name>company</param-name>
    <param-value>lili有限公司</param-value>
</context-param>

ServletContext_:

package com.li.servlet.servletcontext;

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

public class ServletContext_ extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取web.xml的context-parameter

        //1.獲取到ServletContext對(duì)象
        ServletContext servletContext = getServletContext();
        //2.獲取website
        String website = servletContext.getInitParameter("website");
        String company = servletContext.getInitParameter("company");
        System.out.println("website= " + website);
        System.out.println("company= " + company);
        //3.獲取項(xiàng)目的工程路徑
        String contextPath = servletContext.getContextPath();
        System.out.println("項(xiàng)目路徑= " + contextPath);// /servlet_demo
        //4.得到項(xiàng)目發(fā)布后真正的工作路徑
        //這里的斜杠/表示我們的項(xiàng)目發(fā)布后的根路徑 D:\IDEA-workspace\servlet_demo\out\artifacts\servlet_demo_war_exploded
        String realPath = servletContext.getRealPath("/");
        System.out.println("項(xiàng)目發(fā)布后的絕對(duì)路徑= " + realPath);
    }
}

瀏覽器訪問(wèn)ServletContext_:

后臺(tái)輸出:

13.4.2應(yīng)用實(shí)例2-簡(jiǎn)單的網(wǎng)站訪問(wèn)次數(shù)統(tǒng)計(jì)器

需求:完成一個(gè)簡(jiǎn)單的網(wǎng)站訪問(wèn)次數(shù)統(tǒng)計(jì)器

不管使用什么瀏覽器,每訪問(wèn)一次Servlet,就增加1訪問(wèn)次數(shù),在后臺(tái)輸出,并將結(jié)果返回給瀏覽器顯示

WebUtils.java:

package com.li.servlet.servletcontext;

import javax.servlet.ServletContext;

public class WebUtils {
    //該方法對(duì)訪問(wèn)網(wǎng)站的次數(shù)累加,同時(shí)返回次數(shù)
    public static Integer visitCount(ServletContext servletContext) {
        //從servletContext獲取 visit_count 屬性 k-v
        Object visit_count = servletContext.getAttribute("visit_count");
        //判斷visit_count是否為空
        if (visit_count == null) {//說(shuō)明是第1次訪問(wèn)網(wǎng)站
            servletContext.setAttribute("visit_count", 1);
            visit_count = 1;
        } else {//說(shuō)明是第二次或之后訪問(wèn)
            //visit_count+1
            visit_count = Integer.parseInt(visit_count + "") + 1;
            //再將其放回servletContext
            servletContext.setAttribute("visit_count", visit_count);
        }
        return Integer.parseInt(visit_count + "");
    }
}

Servlet01.java:

package com.li.servlet.servletcontext;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/Servlet01"})
public class Servlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取到ServletContext對(duì)象
        ServletContext servletContext = getServletContext();

        Integer visit_count = WebUtils.visitCount(servletContext);

        //輸出顯示
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("<h1>該網(wǎng)站被訪問(wèn)的次數(shù)是" + visit_count + "</h1>");
        writer.flush();
        writer.close();
    }
}

Servlet02.java:

package com.li.servlet.servletcontext;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = {"/Servlet02"})
public class Servlet02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //獲取到ServletContext對(duì)象
        ServletContext servletContext = getServletContext();

        Integer visit_count = WebUtils.visitCount(servletContext);

        //輸出顯示
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("<h1>該網(wǎng)站被訪問(wèn)的次數(shù)是" + visit_count + "</h1>");
        writer.flush();
        writer.close();
    }
}

redeployTomcat,在不同的瀏覽器分別訪問(wèn)Servlet01和Servlet02:

分享名稱:day15-Servlet04
本文路徑:http://chinadenli.net/article38/dsoiosp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、面包屑導(dǎo)航網(wǎng)站營(yíng)銷、、App設(shè)計(jì)、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化