love fish大鵬一曰同風起,扶搖直上九萬里

          導航

          <2006年6月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          公告

          留言簿(15)

          隨筆分類(493)

          隨筆檔案(498)

          相冊

          閱讀排行榜

          常用鏈接

          統(tǒng)計

          積分與排名

          friends

          link

          最新評論

          JSP實用篇(轉)

          一,重定向頁面

          1,response.sendRedirect("url");
          2,response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
          response.setHeader("Location",newLocation);

          二,HTML Encoder和URL Encoder

          1,HTML Encoder自定義,原則:''不輸出,'&'-"&amp;",'<'-"&lt;",'>'-"gt;",'"'-"&quot;"
          2,URLEncoder 在java.net包中有定義
          原型:public static String encode(String s)
          例如:URLEncoder.encode("http://wwww.aaa.com/sss.jsp?name=小鬼")

          三,在JSP中讀寫文件

          1,用FileOutputStream初始化PrintWriter,然后用print或者println方法寫文件
          PrintWriter pw=new PrintWriter(new FileOutputStream("file1.txt"));
          pw.println("Hello world!");
          pw.close();//若有錯誤thow IOException

          用FileWriter初始化PrintWriter,然后用print或者println方法寫文件
          File f=new File("file1.txt");
          PrintWriter pw=new PrintWriter(new FileWriter(f));
          pw.print("Hello world!\n");
          pw.close();
          2,用InputStreamReader或者FileReader初始化BufferedReader,然后用readLine()方法讀取文件
          BufferedReader br=new BufferedReader(new FileReader("file1.txt"));
          String rt=br.readLine();//結尾為null
          br.close();
          3,用FileWriter初始化PrintWriter,然后用pint或者println方法添加文件
          PrintWriter pw=new PrintWriter(new FileWriter("file1.txt"),true);
          4,import java.io.*;
          File f=new File(request.getRealPath(""),"file1.txt");
          boolean f.exists();
          f.delete();f.createNewFile();

          File d=new File(request.getRealPath(""));
          boolean d.exists();
          d.delete();d.mkdir();

          request.getRealPath("url");//虛擬目錄映射為實際目錄
          request.getRealPath("./");//網(wǎng)頁所在的目錄
          request.getRealPath("../");//網(wǎng)頁所在目錄的上一層目錄

          File f=new File("path","file1.txt");
          f.getName();
          f.isFile();
          f.isDirectory();
          f.canRead();
          f.canWrite();
          f.isHidden();
          f.lastModified;
          f.createNewFile();
          f.length();

          File d=new File("path");
          File list[]=d.listFiles();//list是一個File數(shù)組
          for(int i=0;i<list.length;i++)out.println(list[i].getName());

          FileReader fr=new FileReader("path"+"\\file1.txt");
          if(fr.read()==-1)//空文件
          fr.close();
          fr.read(int i)//讀取i個字符,-1如果不再有數(shù)據(jù)
          //用BufferedReader可以一次讀取一行數(shù)據(jù)
          fr.skip(int i);//略過i個字符


          在引用parseInt等函數(shù)的時候,出錯是NumberFormatException等
          Random獲得隨機數(shù),
          Random rd=new Random((new Date()).getTime());
          int p=Math.abs(rd.nextInt())%s;//s為0到的范圍

          四,URL重組、表單隱藏域Cookie

          1,這些是用來彌補HTTP協(xié)議無狀態(tài)特征的技術(Sessions技術)的一部分
          2,URL重組是用Get方法向服務器發(fā)送的信息“?param1=value1&param2=value2&...&paramn=valuen”
          如果服務器已經(jīng)在超鏈接上面作了session標記,那么客戶端通過這個走超鏈接發(fā)送請來時就會包含此標記
          3,form中的<input type=hidden name="key1" value="value1" />也可以像URL重組那樣使用。
          4,Cookie對象
          Cookie c=new Cookie("key", "value");
          response.addCookie(c);

          Cookie[] c=request.getCookies();
          c.setMaxAge(int k);//k以秒為單位
          一般瀏覽器能放20個Cookie

          五,session對象

          1,session對象不僅僅能放String數(shù)據(jù),還能放復雜的對象。
          2,session.putValue("key1",Object1);
          Object o=session.getValue("key1");

          六,處理JSP中的中文問題

          1,ASCII碼
          8bit存儲,0~31和127是控制字符,32~126是可見字符。
          2,GB2312
          兩個8bit表示。前一個127~255,以區(qū)分ASCII碼。
          3,Unicode
          可以將世界上幾十種文字編碼統(tǒng)一在同一種編碼機制下。以16bit為單位存儲。0x0000~0xffff
          4,ISO-8859-1 或稱為Latin-1,8859-1。在Unicode所占的值域為0~255,低位為ASCII擴展到0~255,然后在高位補上0x00,組成16bit(此處不太懂)。
          5,字節(jié)和unicode Java內(nèi)核是unicode,class文件也是。但是流卻是采用的byte方式。char為unicode方式,byte是字節(jié)方式。轉換函數(shù):sun.io里面:
          public static ByteToCharConverter getDefault();//獲取系統(tǒng)使用的編碼方式。
          public static ByteToCharConverter getConverter(String encoding);
          ByteToCharConverter c=New ByteToCharConverter(["encoding"]);
          Byte[] s=c.convertAll(Char[] d);
          也可以 Char[] d=c.converterAll(Byte[] s);
          6,一些函數(shù):
          Integer.toHexString(int i);
          String s;s.getBytes();
          String(byte[]);String(byte[],encoding);//constructors
          //關于Unicode編碼打算單獨寫一篇

          七,獲取JVM屬性值

          Properties props=System.getProperties();
          Enumeration enum=props.propertyNames(); //key枚舉
          key=(String)enum.nextElement();
          String s=(String)props.getProperty(key);

          八,JSP錯誤處理

          1,所有可被throw和catch的Exception對象都繼承自Throwable。Exception應該被catch才對;Error對象也是繼承自Throwable,只是不應該catch,而的結束程序。
          2,catch序列針對的Exception應該從低級到高級才對。
          3,轉譯錯誤和客戶端端請求錯誤。jsp源程序錯誤、import路徑不正確等會在生成Servlet Class文檔時產(chǎn)生轉譯錯誤(500)。在執(zhí)行Servlet Class時客戶端請求錯誤會被catch。
          4,錯誤產(chǎn)生時,可以jsp:forward來控制,但更好是用errorPage來處理。也可以throw new Exception("errMsg")。

          Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=601253

          posted on 2006-06-29 14:52 liaojiyong 閱讀(295) 評論(0)  編輯  收藏 所屬分類: JSP

          主站蜘蛛池模板: 商洛市| 云龙县| 林西县| 土默特右旗| 金华市| 大悟县| 会泽县| 达州市| 汕尾市| 益阳市| 雅安市| 福建省| 嘉黎县| 大丰市| 五原县| 富顺县| 定南县| 微山县| 和田县| 同江市| 色达县| 正蓝旗| 大邑县| 盐源县| 河曲县| 修水县| 射洪县| 祁连县| 伊宁县| 灵台县| 南皮县| 松江区| 平顶山市| 荆门市| 利川市| 怀宁县| 太仓市| 长白| 荆州市| 通化县| 广南县|