JSP隱式對象之Application

JSP九大隱式對象分別爲:Request,Response,Session,Application,Config,Page,Exception,Out,pageContext;其用法基本與servlet相同

Application就是servlet中的servletContext對象,即域對象。一個servlet文件有且僅有一個context對象,同樣的,一個JSP文件也有且僅有一個Application對象。

       下載圖片:

<%@page import="java.net.URLEncoder"%><%@ pagelanguage="java" import="java.util.*"pageEncoding="UTF-8"%><%@page import="java.io.*"%><%

    //得到圖片放置路徑

    String path = application.getRealPath("./images/a.jpg");

    //將得到的路徑存入到file文件中

    File file = new File(path);

    //將文件寫入到輸入流中

    InputStream is = new FileInputStream(file);

   

    //設置響應頭信息

response.setHeader("content-disposition","attachment;fileName="+URLEncoder.encode(file.getName(),"UTF-8"));

    //設置緩衝流

    byte buf[] = new byte[1024];

//設置輸出流

    OutputStream os = response.getOutputStream();

    //判斷

    while(is.read(buf)!=-1){

    //將輸入流的信息寫入到輸出流中

       os.write(buf);

    }

    os.flush();

    os.close();

    is.close();

%>

   統計訪問首頁的次數:

   <%@ pagelanguage="java" import="java.util.*"pageEncoding="UTF-8"%>

<%Object count = application.getAttribute("count");%>

    <%if(count==null)

       {count=1;

 application.setAttribute("count",count);%>

<%}else{

 

 application.setAttribute

       ("count",(Integer)count+1);}

    %>

訪問的次數:<%=count%>

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