JSP隐式对象之Application

来源:互联网 发布:js数字滚动抽奖 编辑:程序博客网 时间:2024/04/30 14:48

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%>

原创粉丝点击