JSP实用技巧大全

 
  1. 一,重定向页面
  2. 1,response.sendRedirect("url");
  3. 2,response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
  4. response.setHeader("Location",newLocation);
  5. 二,HTML Encoder和URL Encoder
  6. 1,HTML Encoder自定义,原则:''不输出,'&'-"&",'<'-"<",'>'-"gt;",'"'-"""
  7. 2,URLEncoder 在java.net包中有定义
  8. 原型:public static String encode(String s)
  9. 例如:URLEncoder.encode("http://wwww.aaa.com/sss.jsp?name=小鬼")
  10. 三,在JSP中读写文件
  11. 1,用FileOutputStream初始化PrintWriter,然后用print或者println方法写文件
  12. PrintWriter pw=new PrintWriter(new FileOutputStream("file1.txt"));
  13. pw.println("Hello world!");
  14. pw.close();//若有错误thow IOException
  15. 用FileWriter初始化PrintWriter,然后用print或者println方法写文件
  16. File f=new File("file1.txt");
  17. PrintWriter pw=new PrintWriter(new FileWriter(f));
  18. pw.print("Hello world!/n");
  19. pw.close();
  20.   2,用InputStreamReader或者FileReader初始化BufferedReader,然后用readLine()方法读取文件
  21. BufferedReader br=new BufferedReader(new FileReader("file1.txt"));
  22. String rt=br.readLine();//结尾为null
  23. br.close();
  24.   3,用FileWriter初始化PrintWriter,然后用pint或者println方法添加文件
  25. PrintWriter pw=new PrintWriter(new FileWriter("file1.txt"),true);
  26.   4,import java.io.*;
  27. File f=new File(request.getRealPath(""),"file1.txt");
  28. boolean f.exists();
  29. f.delete();f.createNewFile();
  30. File d=new File(request.getRealPath(""));
  31. boolean d.exists();
  32. d.delete();d.mkdir();
  33. request.getRealPath("url");//虚拟目录映射为实际目录
  34. request.getRealPath("./");//网页所在的目录
  35. request.getRealPath("../");//网页所在目录的上一层目录
  36. File f=new File("path","file1.txt");
  37. f.getName();
  38. f.isFile();
  39. f.isDirectory();
  40. f.canRead();
  41. f.canWrite();
  42. f.isHidden();
  43. f.lastModified;
  44. f.createNewFile();
  45. f.length();
  46. File d=new File("path");
  47. File list[]=d.listFiles();//list是一个File数组
  48. for(int i=0;i<list.length;i++)out.println(list[i].getName());
  49. FileReader fr=new FileReader("path"+"//file1.txt");
  50. if(fr.read()==-1)//空文件
  51. fr.close();
  52. fr.read(int i)//读取i个字符,-1如果不再有数据
  53. file://用BufferedReader可以一次读取一行数据
  54. fr.skip(int i);//略过i个字符
  55.   在引用parseInt等函数的时候,出错是NumberFormatException等
  56.   Random获得随机数,
  57. Random rd=new Random((new Date()).getTime());
  58. int p=Math.abs(rd.nextInt())%s;//s为0到的范围
  59. 四,URL重组、表单隐藏域Cookie
  60.   1,这些是用来弥补HTTP协议无状态特征的技术(Sessions技术)的一部分
  61.   2,URL重组是用Get方法向服务器发送的信息“?param1=value1¶m2=value2&...¶mn=valuen”
  62.   如果服务器已经在超链接上面作了session标记,那么客户端通过这个走超链接发送请来时就会包含此标记
  63.   3,form中的<input type=hidden name="key1" value="value1" />也可以像URL重组那样使用。
  64.   4,Cookie对象
  65. Cookie c=new Cookie("key""value");
  66. response.addCookie(c);
  67. Cookie[] c=request.getCookies();
  68. c.setMaxAge(int k);//k以秒为单位
  69. 一般浏览器能放20个Cookie
  70. 五,session对象
  71. 1,session对象不仅仅能放String数据,还能放复杂的对象。
  72. 2,session.putValue("key1",Object1);
  73. Object o=session.getValue("key1");
  74.   六,处理JSP中的中文问题
  75.   1,ASCII码
  76.   8bit存储,0~31和127是控制字符,32~126是可见字符。
  77.   2,GB2312
  78.   两个8bit表示。前一个127~255,以区分ASCII码。
  79.   3,Unicode
  80.   可以将世界上几十种文字编码统一在同一种编码机制下。以16bit为单位存储。0x0000~0xffff
  81.   4,ISO-8859-1 或称为Latin-1,8859-1。在Unicode所占的值域为0~255,低位为ASCII扩展到0~255,然后在高位补上0x00,组成16bit(此处不太懂)。
  82.   5,字节和unicode Java内核是unicode,class文件也是。但是流却是采用的byte方式。char为unicode方式,byte是字节方式。转换函数:sun.io里面:
  83. public static ByteToCharConverter getDefault();//获取系统使用的编码方式。
  84. public static ByteToCharConverter getConverter(String encoding);
  85. ByteToCharConverter c=New ByteToCharConverter(["encoding"]);
  86. Byte[] s=c.convertAll(Char[] d);
  87.   也可以 Char[] d=c.converterAll(Byte[] s);
  88.   6,一些函数:
  89. Integer.toHexString(int i);
  90. String s;s.getBytes();
  91. String(byte[]);String(byte[],encoding);//constructors
  92. file://关于Unicode编码打算单独写一篇
  93. 七,获取JVM属性值
  94. Properties props=System.getProperties();
  95. Enumeration enum=props.propertyNames(); file://key枚举
  96. key=(String)enum.nextElement();
  97. String s=(String)props.getProperty(key);
  98. 八,JSP错误处理
  99.   1,所有可被throwcatch的Exception对象都继承自Throwable。Exception应该被catch才对;Error对象也是继承自Throwable,只是不应该catch,而的结束程序。
  100.   2,catch序列针对的Exception应该从低级到高级才对。
  101.   3,转译错误和客户端端请求错误。jsp源程序错误、import路径不正确等会在生成Servlet Class文档时产生转译错误(500)。在执行Servlet Class时客户端请求错误会被catch
  102.  4,错误产生时,可以jsp:forward来控制,但更好是用errorPage来处理。也可以throw new Exception("errMsg")。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值