ServletContext的功能

 1、獲得整個web應用初始化參數

類中:
public class ContextServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //通過config獲得context
        ServletContext context=getServletConfig().getServletContext();

       //從而獲得全局的參數

       System.out.println(context.getInitParameter("hobby"));//一定不能忘記用context
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);

}
}

web.xml中:

<context-param>
        <param-name>hobby</param-name>
        <param-value>唱兒歌</param-value>

</context-param>

2、實現全局數據共享

以記錄網站的訪問次數爲例

countServlet    類中
public class countServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {

        //獲得servletcontext對象

       ServletContext context=getServletContext();

        //保存訪問次數0
        context.setAttribute("visitimes",0);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //每次訪問都回執行doget方法,因此把遞增次數的訪問次數放在doget中。
        
        //從servletcontext中獲得訪問次數
        ServletContext context=getServletContext();
        int times=(Integer)context.getAttribute("visitimes");
        
        //訪問次數遞增
        times++;
        
        //更新訪問次數到servletcontext中
        context.setAttribute("visitimes", times);
        response.getWriter().print("網站被訪問了"+times+"次");//輸出到瀏覽器!
        //System.out.println("網站被訪問了"+times+"次");
    }

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

}

CountShowServlet類中(代碼和上個類中的代碼一致!)

public class CountShowServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*ServletContext context=getServletContext();
        int times =(Integer)context.getAttribute("visitimes");*/
        ServletContext context=getServletContext();
        int time=(Integer)context.getAttribute("visitimes");
        time++;
        context.setAttribute("visitimes", time);
        response.getWriter().print(time);
    }

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

}

此例說明了在countServlet中用context記錄的訪問次數,在CountShowServlet 也能訪問,即全局共享!


3、讀取we工程資源文件

package aweiyo.io;

public class ReadFileServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //因爲不在wenroot的路徑下,而在工程路徑下,所以要加上前面的路徑。又因爲工程的存放文件的路徑是在webroot的目錄下,所以可以read,但是
        //web-inf目錄下的文件就不可以讀取了,因爲其不在webroot的目錄下
        String filename2="/WEB-INF/classes/a.txt";
        filename2=getServletContext().getRealPath(filename2);
        readfile(filename2);
        
        // 路徑開頭必須寫/,因爲webroot就是servlet重的根目錄,所以在webroot下的文件不用加其他的
        String filename="/a2.txt";
        //獲得該路徑的絕對路徑,即全部路徑
        filename=getServletContext().getRealPath(filename);
        readfile(filename);
        
        //用Class的路徑,也可用於Java中
        Class c=ReadFileServlet.class;
        String filename3=c.getResource("/a.txt").getFile();
        readfile(filename3);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
    
    public static void readfile(String filename) throws FileNotFoundException,
    IOException {
            BufferedReader in =new BufferedReader(new FileReader(filename));
            String line;
            while((line=in.readLine())!=null){
            System.out.println(line);
}
in.close();
}
    
}

注:java讀取文件


/**
 * 通過Java程序讀取文件
 * @author aweiyoo
 *
 */
public class FileReaderTest {
    public static void main(String[] args) throws IOException {
        String filename="src/a.txt";
        readfile(filename);
        String filename2="WebRoot/a2.txt";
        readfile(filename2);
        String filename3=("a3.txt");//位於根目錄下的文件可以直接寫
        readfile(filename3);
        
    }

    public static void readfile(String filename) throws FileNotFoundException,
            IOException {
        BufferedReader in =new BufferedReader(new FileReader(filename));
        String line;
        while((line=in.readLine())!=null){
            System.out.println(line);
        }
        in.close();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章