zoukankan      html  css  js  c++  java
  • 内置对象实例

      1 1.网页访问量计数
      2 <%@page import="java.math.BigInteger"%>
      3 <%@page import="java.io.*"%>
      4 <%@page import="java.util.*"%>
      5 <%@ page language="java" contentType="text/html"
      6     pageEncoding="utf-8"%>
      7 <!DOCTYPE html>
      8 <html>
      9 <head>
     10 <title>网页访问量计数</title>
     11 </head>
     12 <body>
     13     <%!
     14         //定义全局变量
     15         BigInteger count = null;
     16     %>
     17     <%!  //读入文件读取方法
     18         public BigInteger load(File f){
     19             BigInteger count = null;
     20             try{
     21                 if(f.exists()){//判断文件存不存在
     22                     Scanner sc = new Scanner(new FileInputStream(f));
     23                     if(sc.hasNext()){
     24                         count = new BigInteger(sc.next());
     25                     }
     26                     sc.close();
     27                 }else{
     28                     //文件不存在,即还没有访客进行访问
     29                     count = new BigInteger("0");
     30                     //保存到文件中
     31                     save(f,count);            
     32                 }
     33             }catch(Exception e){
     34                 System.out.println(e);
     35             }    
     36             return count;
     37         }
     38     //写出文件保存方法
     39      public void save(File f,BigInteger count){
     40          try{
     41              PrintStream ps= null;
     42              ps = new PrintStream(new FileOutputStream(f));
     43              ps.println(count);
     44              ps.close();
     45          }catch(Exception e){
     46              System.out.println(e);
     47          }
     48      }    
     49     %>
     50     <%   
     51         String name = this.getServletContext().getRealPath("/")+"count.txt";
     52         System.out.print(name);
     53         File f = new File(name);
     54         if(session.isNew()){
     55             synchronized(this){
     56                 //读取数据以及保存
     57                 count = load(f);
     58                 //做自增操作
     59                 count = count.add(new BigInteger("1"));
     60                 //保存数据
     61                 save(f,count);
     62             }
     63         }
     64     %>
     65     <h2>您是第<%=count==null?0:count %>位访客</h2>
     66 </body>
     67 </html>
     68 2.获取真实路径
     69 <%@ page language="java" contentType="text/html"
     70     pageEncoding="GBK"%>
     71 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     72 <html>
     73 <head>
     74 <title>Insert title here</title>
     75 </head>
     76 <body>
     77     <%
     78         String path = this.getServletContext().getRealPath("/");
     79     %>
     80     真实路径:<%=path%>
     81 </body>
     82 </html>
     83 3.获取属性名称及属性
     84 <%@page import="java.util.Enumeration"%>
     85 <%@ page language="java" contentType="text/html"
     86     pageEncoding="GBK"%>
     87 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     88 <html>
     89 <head>
     90 <title>Insert title here</title>
     91 </head>
     92 <body>
     93     <%
     94         Enumeration en = this.getServletContext().getAttributeNames();
     95         while(en.hasMoreElements()){
     96             String name = (String)en.nextElement();
     97     %>
     98         <h1><%=name %>----<%=this.getServletContext().getAttribute(name) %></h1>
     99     <%    
    100         }
    101     %>
    102 </body>
    103 </html>
    104 4.缓冲区大小
    105 <%@page import="java.util.Enumeration"%>
    106 <%@ page language="java" contentType="text/html"
    107     pageEncoding="GBK"%>
    108 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    109 <html>
    110 <head>
    111 <title>Insert title here</title>
    112 </head>
    113 <body>
    114     <%
    115         int buffer = out.getBufferSize();//全部缓冲区大小
    116         int unuse = out.getRemaining();//未使用缓冲区大小
    117         int use = buffer-unuse;
    118     %>
    119     <h3>缓冲区大小<%=buffer %></h3>
    120     <h3>未使用缓冲区大小<%=unuse %></h3>
    121     <h3>已使用缓冲区大小<%=use %></h3>
    122 </body>
    123 </html>
    124 5.表单内容写入、写出文件
    125 <!DOCTYPE html>
    126 <html>
    127 <head>
    128 <meta charset="GBK">
    129 <title>Insert title here</title>
    130 </head>
    131 <body>
    132     <form action="input_context.jsp" method="post">
    133         文件名:<input type="text" name="filename"><br>
    134         文件内容<textarea rows="10" cols="30" name="context"></textarea><br>
    135         <input type="submit" value="保存">
    136     </form>
    137 </body>
    138 </html>
    139 
    140 <%@ page language="java" contentType="text/html"
    141     pageEncoding="GBK"%>
    142 <%@ page import="java.io.*" %>
    143 <%@ page import="java.util.*" %>
    144 <%@ page import="java.lang.*" %>
    145 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    146 <html>
    147     <head>
    148         <title>Insert title here</title>
    149     </head>
    150     <body>
    151         <%
    152             //设置统一编码
    153             request.setCharacterEncoding("GBK");
    154             //获取到文件名称 文件内容
    155             String filename = request.getParameter("filename");
    156             String content = request.getParameter("context");
    157             //拼凑path  内容保存于 当前路径下新建的note文件夹中,
    158             String path = this.getServletContext().getRealPath("/")+"note"+File.separator+filename;
    159             //判断父文件夹是否存在
    160             File f = new File(path);
    161             if(!f.getParentFile().exists()){
    162                 f.getParentFile().mkdir();//创建文件夹
    163             }
    164             PrintStream ps = null;
    165             ps = new PrintStream(new FileOutputStream(f));
    166             ps.println(content);
    167             ps.close();
    168             System.out.println("输出成功");
    169         %>
    170         <%
    171             Scanner sc = new Scanner(new FileInputStream(f));
    172             sc.useDelimiter("
    ");//按换行符扫描
    173             StringBuffer sbu = new StringBuffer();
    174             while(sc.hasNext()){
    175                 sbu.append(sc.next()).append("<br>");//显示文件内容
    176             }
    177         
    178         %>
    179             <%=sbu %>
    180     </body>
    181 </html>
  • 相关阅读:
    cocos2d-x quick 学习 二 Hello world
    cocos2d-x quick 学习 一 环境
    给自己
    最近遇到问题
    lua 基础 1
    lua 学习 (一 )Mac搭建lua环境和ide
    VueDay1
    Git的简单使用
    web常用第三方接口
    2.node基础知识笔记
  • 原文地址:https://www.cnblogs.com/liuyangv/p/8076190.html
Copyright © 2011-2022 走看看