标准标签库应用及从文件读取消息的备忘

//properties文件中的格式

title=标题
1=内容1
2=内容2
3=内容3
4=内容....

//处理中文乱码的类

public class CodeSwitching {
 
 static public String Transition(String str) throws UnsupportedEncodingException
 {
  String code ;
  byte bytes[] = str.getBytes("iso-8859-1");
  code = new String(bytes,"gb2312");
  return code;
 }
}

//读properties文件

//在servlet里写

FileInputStream file = null;

Properties pro = new Properties();

Vector v = new Vector();

try

{

     file = new FileInputStream(路径//文件名.properties);          //注:文件需要提前建好

     pro.load(file);

     String title = CodeSwitching.Transition();              //取文件标题

     int count = pro.size();

     for(int i =1 ; i<count ; i++)                    //循环遍历文件里的内容

     {

          String aa = CodeSwitching.Transition(pro.getProperty(i+""));

          v.add(aa);

     }

     request.setAttribute("title",title);

     request.setAttribute("v",v);

     request.getRequestDispatcher("index.jsp").forward(request,response);

}

catch(Exception e)

{

     e.printStackTrace();

}

 

//页面index.jsp

<tr>
       <td height="34" background="../images/homedhbg.gif" style="border-left: 1 solid #C0C0C0; border-right: 1 solid #C0C0C0">&nbsp;<img border="0" src="../images/index_news.gif" width="138" height="21"></td>
      </tr>
      <tr>
       <%-- ********************显示文件标题******************** --%>
       <th><font size="+2">${title }</font></th>
      </tr>
      <tr>
       <td>
       <%-- ********************循环遍历vector向量里的值******************** --%>
       <c:forEach items="${content}" var="a">
        &nbsp;&nbsp;&nbsp;&nbsp;${a }<br><br>
       </c:forEach>
       </td>
      </tr>

posted on 2008-10-16 17:27  VictorShan  阅读(146)  评论(0编辑  收藏  举报

导航