02---jsp内置对象05(application对象)

来源:互联网 发布:中国乡村 萧公权知乎 编辑:程序博客网 时间:2024/06/07 09:25

application是javax.servlet.ServletContext接口的实例,表示整个Servlet的上下文,ServletContext
 代表了整个容器的操作;
 public String getRealPath(String path)  得到虚拟目录对应的决定路径
 public Enumeration getAttributeNames()  得到所有属性名称
 public String getContextPath()   取得当前虚拟路径名称
除了以上的外,对属性的增加、获取、移除也是有其作用的,setAttribute()、getAttribute()、
 removeAttribute();

1、取得绝对路径:
 如果要想取得一个虚拟目录对应的绝对路径要使用getRealPath()方法;
  <%@ page contentType="text/html" pageEncoding="gbk"%>
  <html>
  <head><title>这是测试</title></head>
  <body>
   <%String path=application.getRealPath("/");%>
   <h1>真实路径:<%=path%></h1> 
  </body>
  </html>
 即得到在server.xml中配置的<Context path="/lid" docBase="D:\javaeedemo">
 中的docBase的值;
 getRealPath()中的参数是你想获取的文件的真实路径,假设想获取index.jsp,此处填写
  index.jsp即可;想获得虚拟目录的真是路径,此处填写"/";

 如果想要进行文件操作,首先需要通过File类找到一个指定的路径,这个路径最好是绝对路径,
 所以这个时候getRealPath()就起到了作用;

 input_content.html:
  <html>
  <head><title>这是测试</title></head>
  <body>
   <form action="input_content.jsp" method="post">
    输入文件名称:<input type="text" name="filename"><br>
    输入文件内容:<textarea name="filecontent" cols="30" rows="3"></textarea><br>
    <input type="submit" value="保存"/>
    <input type="reset" value="重置"/>
   </form>
  </body>
  </html>
 input_content.jsp:
  <%@ page contentType="text/html" pageEncoding="gbk"%>
  <%@ page import="java.io.*"%>
  <html>
  <head><title>这是测试</title></head>
  <body>
   <%
    request.setCharacterEncoding("GBK");//解决乱码问题
    String name=request.getParameter("filename");
    String content=request.getParameter("filecontent");
    //通过getRealPath()得到真实路径
    String fileName=this.getServletContext().getRealPath("/")+"note"+File.separator+name;
    File file=new File(fileName);
    if(!file.getParentFile().exists()){//查看文件的上层目录是否存在
     file.getParentFile().mkdir();//创建文件夹
     }
    PrintStream ps=null;
    ps=new PrintStream(new FileOutputStream(file));
    ps.println(content);
    ps.close();
   %>
    <h2>保存成功</h2>
  </body>
  </html>
  下面把程序简单的修改一下,当文件保存完毕之后让其在显示出来,这个时候可以通过
  Scanner类完成;
   <%@ page contentType="text/html" pageEncoding="gbk"%>
   <%@ page import="java.io.*"%>
   <%@ page import="java.util.*"%>
   <html>
   <head><title>这是测试</title></head>
   <body>
    <%
     request.setCharacterEncoding("GBK");//解决乱码问题
     String name=request.getParameter("filename");
     String content=request.getParameter("filecontent");
     //通过getRealPath()得到真实路径
     String fileName=this.getServletContext().getRealPath("/")+"note"+File.separator+name;
     File file=new File(fileName);
     if(!file.getParentFile().exists()){//查看文件的上层目录是否存在
      file.getParentFile().mkdir();//创建文件夹
      }
     PrintStream ps=null;
     ps=new PrintStream(new FileOutputStream(file));
     ps.println(content);
     ps.close();
    %>
    <%
     Scanner scan=new Scanner(new FileInputStream(file));
     scan.useDelimiter("\n");
     StringBuffer buf=new StringBuffer();
     while(scan.hasNext()){
       buf.append(scan.next()).append("<br>");
      }  
     scan.close();
    %>
    <%=buf%>
   </body>
   </html>

2、网站计数器:
 需要注意一下问题:
  ·访问的人数可能很多我们用BigInteger;
  ·用户每次在第一次访问时才需要计数,在执行计数之前必须使用isNew()判断;
  ·在进行更改、保存的时候需要进行同步操作;
   <%@ page contentType="text/html" pageEncoding="gbk"%>
   <%@ page import="java.io.*"%>
   <%@ page import="java.util.*"%>
   <%@ page import="java.math.*"%>
   <html>
   <head><title>这是测试</title></head>
   <body>
    <%!
     BigInteger count=null;//接受数据
    %>
    <%!
     public BigInteger load(File file){//取得数据
      
      try{
        if(file.exists()){
         Scanner scan=new Scanner(new FileInputStream(file));
         if(scan.hasNext()){
          count=new BigInteger(scan.next());
          }
          scan.close();
         }else{//应该保存一个新的,从0开始
          count=new BigInteger("0");
          save(file,count);//保存一个新的文件
          }
       }
      catch(Exception e){
       e.printStackTrace();
       }
      return count;
     }
     
     public void save(File file,BigInteger count){//向指定文件输入数据
      try{
       PrintStream ps=null;
       ps=new PrintStream(new FileOutputStream(file));
       ps.println(count);
       ps.close();
       }
      catch(Exception e){
       e.printStackTrace();
       }
      }
    %> 
    <%
     String fileName=this.getServletContext().getRealPath("/")+"count.txt";
     File file=new File(fileName);
     if(session.isNew()){
        synchronized(this){
        count=load(file);//读取
        count=count.add(new BigInteger("1"));
        save(file,count);
        }
      }
    %>
    <h2>你是第<%=count==null?0:count%>位访客</h2>
   </body>
   </html>

3、查看属性:
 在application中也是存在属性操作的,
  <%@ page contentType="text/html" pageEncoding="gbk"%>
  <%@ page import="java.util.*"%>
  <html>
  <head><title>这是测试</title></head>
  <body>
   <%
    Enumeration enu=this.getServletContext().getAttributeNames();
    while(enu.hasMoreElements()){
     String name=(String)enu.nextElement();
   %>
    <h3><%=name%>-----><%=this.getServletContext().getAttribute(name)%></h3>
   <%
     }
   %>
  </body>
  </html>
 通过以上程序运行的结果可以看出,tomcat的所有第三方的jar文件
 都是通过application属性设置到服务器上去的,所以在每次配置了一个新的开发包
 的时候,服务器必须重新启动;