JSP實(shí)用篇(轉(zhuǎn))
一,重定向頁面
1,response.sendRedirect("url");
2,response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location",newLocation);
二,HTML Encoder和URL Encoder
1,HTML Encoder自定義,原則:''不輸出,'&'-"&",'<'-"<",'>'-"gt;",'"'-"""
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();//若有錯(cuò)誤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();//結(jié)尾為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");//虛擬目錄映射為實(shí)際目錄
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是一個(gè)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個(gè)字符,-1如果不再有數(shù)據(jù)
//用BufferedReader可以一次讀取一行數(shù)據(jù)
fr.skip(int i);//略過i個(gè)字符
在引用parseInt等函數(shù)的時(shí)候,出錯(cuò)是NumberFormatException等
Random獲得隨機(jī)數(shù),
Random rd=new Random((new Date()).getTime());
int p=Math.abs(rd.nextInt())%s;//s為0到的范圍
四,URL重組、表單隱藏域Cookie
1,這些是用來彌補(bǔ)HTTP協(xié)議無狀態(tài)特征的技術(shù)(Sessions技術(shù))的一部分
2,URL重組是用Get方法向服務(wù)器發(fā)送的信息“?param1=value1¶m2=value2&...¶mn=valuen”
如果服務(wù)器已經(jīng)在超鏈接上面作了session標(biāo)記,那么客戶端通過這個(gè)走超鏈接發(fā)送請(qǐng)來時(shí)就會(huì)包含此標(biāo)記
3,form中的<input type=hidden name="key1" value="value1" />也可以像URL重組那樣使用。
4,Cookie對(duì)象
Cookie =new Cookie("key", "value");
response.addCookie( );
Cookie[] =request.getCookies();
. (int k);//k以秒為單位
一般瀏覽器能放20個(gè)Cookie
五,session對(duì)象
1,session對(duì)象不僅僅能放String數(shù)據(jù),還能放復(fù)雜的對(duì)象。
2,session.putValue("key1",Object1);
Object o=session.getValue("key1");
六,處理JSP中的中文問題
1,ASCII碼
8bit存儲(chǔ),0~31和127是控制字符,32~126是可見字符。
2,GB2312
兩個(gè)8bit表示。前一個(gè)127~255,以區(qū)分ASCII碼。
3,Unicode
可以將世界上幾十種文字編碼統(tǒng)一在同一種編碼機(jī)制下。以16bit為單位存儲(chǔ)。0x0000~0xffff
4,ISO-8859-1 或稱為L(zhǎng)atin-1,8859-1。在Unicode所占的值域?yàn)?~255,低位為ASCII擴(kuò)展到0~255,然后在高位補(bǔ)上0x00,組成16bit(此處不太懂)。
5,字節(jié)和unicode Java內(nèi)核是unicode,class文件也是。但是流卻是采用的byte方式。char為unicode方式,byte是字節(jié)方式。轉(zhuǎn)換函數(shù):sun.io里面:
public static ByteToCharConverter getDefault();//獲取系統(tǒng)使用的編碼方式。
public static ByteToCharConverter getConverter(String encoding);
ByteToCharConverter =New ByteToCharConverter(["encoding"]);
Byte[] s= .convertAll(Char[] d);
也可以 Char[] d= .converterAll(Byte[] s);
6,一些函數(shù):
Integer.toHexString(int i);
String s;s.getBytes();
String(byte[]);String(byte[],encoding);//constructors
//關(guān)于Unicode編碼打算單獨(dú)寫一篇
七,獲取JVM屬性值
Properties props=System.getProperties();
Enumeration enum=props.propertyNames(); //key枚舉
key=(String)enum.nextElement();
String s=(String)props.getProperty(key);
八,JSP錯(cuò)誤處理
1,所有可被throw和catch的Exception對(duì)象都繼承自Throwable。Exception應(yīng)該被catch才對(duì);Error對(duì)象也是繼承自Throwable,只是不應(yīng)該catch,而的結(jié)束程序。
2,catch序列針對(duì)的Exception應(yīng)該從低級(jí)到高級(jí)才對(duì)。
3,轉(zhuǎn)譯錯(cuò)誤和客戶端端請(qǐng)求錯(cuò)誤。jsp源程序錯(cuò)誤、import路徑不正確等會(huì)在生成Servlet Class文檔時(shí)產(chǎn)生轉(zhuǎn)譯錯(cuò)誤(500)。在執(zhí)行Servlet Class時(shí)客戶端請(qǐng)求錯(cuò)誤會(huì)被catch。
4,錯(cuò)誤產(chǎn)生時(shí),可以jsp:forward來控制,但更好是用errorPage來處理。也可以throw new Exception("errMsg")。
1,response.sendRedirect("url");
2,response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location",newLocation);
二,HTML Encoder和URL Encoder
1,HTML Encoder自定義,原則:''不輸出,'&'-"&",'<'-"<",'>'-"gt;",'"'-"""
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();//若有錯(cuò)誤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();//結(jié)尾為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");//虛擬目錄映射為實(shí)際目錄
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是一個(gè)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個(gè)字符,-1如果不再有數(shù)據(jù)
//用BufferedReader可以一次讀取一行數(shù)據(jù)
fr.skip(int i);//略過i個(gè)字符
在引用parseInt等函數(shù)的時(shí)候,出錯(cuò)是NumberFormatException等
Random獲得隨機(jī)數(shù),
Random rd=new Random((new Date()).getTime());
int p=Math.abs(rd.nextInt())%s;//s為0到的范圍
四,URL重組、表單隱藏域Cookie
1,這些是用來彌補(bǔ)HTTP協(xié)議無狀態(tài)特征的技術(shù)(Sessions技術(shù))的一部分
2,URL重組是用Get方法向服務(wù)器發(fā)送的信息“?param1=value1¶m2=value2&...¶mn=valuen”
如果服務(wù)器已經(jīng)在超鏈接上面作了session標(biāo)記,那么客戶端通過這個(gè)走超鏈接發(fā)送請(qǐng)來時(shí)就會(huì)包含此標(biāo)記
3,form中的<input type=hidden name="key1" value="value1" />也可以像URL重組那樣使用。
4,Cookie對(duì)象
Cookie =new Cookie("key", "value");
response.addCookie( );
Cookie[] =request.getCookies();
. (int k);//k以秒為單位
一般瀏覽器能放20個(gè)Cookie
五,session對(duì)象
1,session對(duì)象不僅僅能放String數(shù)據(jù),還能放復(fù)雜的對(duì)象。
2,session.putValue("key1",Object1);
Object o=session.getValue("key1");
六,處理JSP中的中文問題
1,ASCII碼
8bit存儲(chǔ),0~31和127是控制字符,32~126是可見字符。
2,GB2312
兩個(gè)8bit表示。前一個(gè)127~255,以區(qū)分ASCII碼。
3,Unicode
可以將世界上幾十種文字編碼統(tǒng)一在同一種編碼機(jī)制下。以16bit為單位存儲(chǔ)。0x0000~0xffff
4,ISO-8859-1 或稱為L(zhǎng)atin-1,8859-1。在Unicode所占的值域?yàn)?~255,低位為ASCII擴(kuò)展到0~255,然后在高位補(bǔ)上0x00,組成16bit(此處不太懂)。
5,字節(jié)和unicode Java內(nèi)核是unicode,class文件也是。但是流卻是采用的byte方式。char為unicode方式,byte是字節(jié)方式。轉(zhuǎn)換函數(shù):sun.io里面:
public static ByteToCharConverter getDefault();//獲取系統(tǒng)使用的編碼方式。
public static ByteToCharConverter getConverter(String encoding);
ByteToCharConverter =New ByteToCharConverter(["encoding"]);
Byte[] s= .convertAll(Char[] d);
也可以 Char[] d= .converterAll(Byte[] s);
6,一些函數(shù):
Integer.toHexString(int i);
String s;s.getBytes();
String(byte[]);String(byte[],encoding);//constructors
//關(guān)于Unicode編碼打算單獨(dú)寫一篇
七,獲取JVM屬性值
Properties props=System.getProperties();
Enumeration enum=props.propertyNames(); //key枚舉
key=(String)enum.nextElement();
String s=(String)props.getProperty(key);
八,JSP錯(cuò)誤處理
1,所有可被throw和catch的Exception對(duì)象都繼承自Throwable。Exception應(yīng)該被catch才對(duì);Error對(duì)象也是繼承自Throwable,只是不應(yīng)該catch,而的結(jié)束程序。
2,catch序列針對(duì)的Exception應(yīng)該從低級(jí)到高級(jí)才對(duì)。
3,轉(zhuǎn)譯錯(cuò)誤和客戶端端請(qǐng)求錯(cuò)誤。jsp源程序錯(cuò)誤、import路徑不正確等會(huì)在生成Servlet Class文檔時(shí)產(chǎn)生轉(zhuǎn)譯錯(cuò)誤(500)。在執(zhí)行Servlet Class時(shí)客戶端請(qǐng)求錯(cuò)誤會(huì)被catch。
4,錯(cuò)誤產(chǎn)生時(shí),可以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) 評(píng)論(0) 編輯 收藏 所屬分類: JSP