JSP----application

来源:互联网 发布:webpack mac 安装 编辑:程序博客网 时间:2024/04/28 17:44

取得绝对路径

通过getRealPath()来获取绝对路径

<body><% //  String path= application.getRealPath("hello.html");   //------这里用常用this.getServletContext()代替application-------%>真实路径:<%=path %></body>

这个真实的路径就是在server.xml 文件中配置而成的。

 getServletContext().getRealPath也可以实现。。。。而且最好使用该方法,而不是application.getRealPath()

取得绝对路径就可以在JSP中进行文件操作了。如果想要对文件操作,就必须要通过File 类找到一个指定的路径,最好是绝对路径。所以这个时候getRealPath() 方法就起作用了。

查看属性

在application中有一个方法可以取得全部属性。getAttributeNames()

<body><% Enumeration enu = this.getServletContext().getAttributeNames(); // ------这里用this.getServletContext()代替application-------   //------------取得全部属性----------- while (enu.hasMoreElements()){ String name = (String) enu.nextElement();%><h4><%=name %>---->   <%=this.getServletContext().getAttribute(name) %>    // ------这里用this.getServletContext()代替application-------</h4><%} %></body>

--------------------------------------------------------------------------------------------

在实际应用中,经常用this.getServletContext() 方法来代替application。

0 0
原创粉丝点击