JSP实用篇

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("./");//网页所在的目录
    request.getRealPath("../");//网页所在目录的上一层目录
   
    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数组
    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如果不再有数据
    //用BufferedReader可以一次读取一行数据
    fr.skip(int i);//略过i个字符
   
   
    在引用parseInt等函数的时候,出错是NumberFormatException等
    Random获得随机数,
     Random rd=new Random((new Date()).getTime());
     int p=Math.abs(rd.nextInt())%s;//s为0到的范围
四,URL重组、表单隐藏域Cookie
 1,这些是用来弥补HTTP协议无状态特征的技术(Sessions技术)的一部分
 2,URL重组是用Get方法向服务器发送的信息“?param1=value1&param2=value2&...&paramn=valuen”
    如果服务器已经在超链接上面作了session标记,那么客户端通过这个走超链接发送请来时就会包含此标记
 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数据,还能放复杂的对象。
 2,session.putValue("key1",Object1);
    Object o=session.getValue("key1");
六,处理JSP中的中文问题
 1,ASCII码
  8bit存储,0~31和127是控制字符,32~126是可见字符。
 2,GB2312
  两个8bit表示。前一个127~255,以区分ASCII码。
 3,Unicode
  可以将世界上几十种文字编码统一在同一种编码机制下。以16bit为单位存储。0x0000~0xffff
 4,ISO-8859-1 或称为Latin-1,8859-1。在Unicode所占的值域为0~255,低位为ASCII扩展到0~255,然后在高位补上0x00,组成16bit(此处不太懂)。
 5,字节和unicode Java内核是unicode,class文件也是。但是流却是采用的byte方式。char为unicode方式,byte是字节方式。转换函数:sun.io里面:
  public static ByteToCharConverter getDefault();//获取系统使用的编码方式。
  public static ByteToCharConverter getConverter(String encoding);
  ByteToCharConverter c=New ByteToCharConverter(["encoding"]);
  Byte[] s=c.convertAll(Char[] d);
  也可以 Char[] d=c.converterAll(Byte[] s);
 6,一些函数:
  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文档时产生转译错误(500)。在执行Servlet Class时客户端请求错误会被catch。
 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、付费专栏及课程。

余额充值