jsp內建對象(三):application

來源:互聯網
上載者:User

標籤:jsp

一:擷取檔案路徑

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘getPath.jsp‘ starting page</title>
  </head>
  <body>
  <%
  String path=application.getRealPath("/");
  %>
  <h3>真實路徑:<%=path %></h3>
  </body>
</html>


二:擷取輸入的內容

<%@page import="java.io.*"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘input_content.jsp‘ starting page</title>
  </head>
  <body>
  <form action="input_content.jsp" method="post">
  輸入檔案名稱:<input type="text" name="filename"><br>
  輸入檔案內容:<textarea name="filecontent" rows="2" cols="30"></textarea><br>
  <input type="submit" value="提交">
  <input type="reset" value="重設">
  </form>
  <%
  request.setCharacterEncoding("utf-8");
  String name=request.getParameter("filename");
  String content=request.getParameter("filecontent");
  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>


三:網站計數器

<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.PrintStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="javax.servlet.jsp.tagext.TryCatchFinally"%>
<%@page import="java.io.File"%>
<%@page import="java.math.BigInteger"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘count.jsp‘ starting page</title>
  </head>
  
  <body>
  <%!//定義一個全域變數
  BigInteger count=null;
  %>
  <%!
  //讀取計數檔案
  public BigInteger load(File file){
  //接收讀取的資料
  BigInteger count=null;
  try{
  if(file.exists()){
  Scanner scan=null;
  scan=new Scanner(new FileInputStream(file));
  if(scan.hasNext()){
  count=new BigInteger(scan.next());
  }
  scan.close();
  }else{
  //第一次訪問
  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);
  }
  }
  %>
  <h3>第<%=count==null?0:count %>位訪客!</h3>
  </body>
</html>


四:查看屬性範圍

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘application.jsp‘ starting page</title>
  </head>
  <body>
  <%
  Enumeration enu=this.getServletContext().getAttributeNames();
  while(enu.hasMoreElements()){
  String name=(String)enu.nextElement();
  %>
  <h4><%=name %>--><%=this.getServletContext().getAttribute(name) %></h4>
  <%
  }
  %>
  </body>
</html>

著作權聲明:博主原創文章,轉載請說明出處。http://blog.csdn.net/dzy21

jsp內建對象(三):application

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.