bodong_chen  
Java Space
日历
<2005年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
统计
  • 随笔 - 11
  • 文章 - 3
  • 评论 - 2
  • 引用 - 0

导航

常用链接

留言簿(2)

随笔分类

随笔档案

文章档案

Log4j

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

2005年11月23日

以前经常会在List的index处理上犯微小的错误,或大一,或小一,但结果都是一样:wrong!所以在这里简要总结一下最容易出错的两个方法。
1. List的indexOf(Object obj)方法:
这个方法返回的是List中一个节点的序号。例如:第一个节点返回0,最后一个节点返回list.size()-1。
2. List的subList(int begin, int end)方法:
这个方法是取出一个List的一个子List,在两个参数上很容易犯错误。
以一个size为5的List为例:
subList(0, 2);取出的是第一个到第二个节点;
subList(0, 1);取出第一个节点;
subList(1, 1);不取出任何节点;
subList(2, 1);则会出参数错误的异常。
可以看出,begin为起始节点的序号,end为所要取的最后一个节点的序号+1。

jdk中的这些类有很多细节都需要非常注意,不然实际的coding中会遇到很多麻烦。
posted @ 2006-04-10 16:37 bodong_chen 阅读(1351) | 评论 (0)编辑 收藏
 
在一个有关Jdom的讨论中提到:

Elliotte Rusty Harold: XML documents must be well-formed. There are, depending on how you count, anywhere from a hundred to several thousand different rules. These "well-formedness" rules are the minimum requirements for an XML document. The rules cover things like what characters are allowed in element names: The letter 'a' is OK. The letter omega is OK. The asterisk character is not OK. White space is not OK. The rules say that every start-tag has to have a matching end-tag. Elements can nest, but they cannot overlap. Processing instructions have the form <, ?, a target, white space, the data, ?, and a >. Comments cannot contain a double hyphen. There are many such rules governing well-formedness of XML documents.

Validity talks about which elements and attributes are allowed where. Well-formedness only talks about the structure of any XML document, irrespective of what the names are. Validity says, we're only going to allow these elements with these names in these positions. Validity is not required. Well-formedness is.

JDOM, and for that matter DOM, allows you to create malformed documents. They do not check everything they can possibly check. For instance, they do not currently check that the text content of a text node does not contain the null character, which is completely illegal in an XML document.

这是发生在2003年的讨论:http://www.artima.com/intv/jdom2.html。

现在,使用JDOM可以完成这两件事:
1. 验证XML文件的Wellformedness:
The build() method of SAXBuilder throws an IOException if an I/O error such as a broken socket prevents the document from being completely read. It throws a JDOMException if the document is malformed. This is the generic superclass for most things that can go wrong while working with JDOM other than I/O errors.
Example:
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.IOException;


public class JDOMChecker {

  
public static void main(String[] args) {
  
    
if (args.length == 0) {
      System.out.println(
"Usage: java JDOMChecker URL"); 
      
return;
    } 
      
    SAXBuilder builder 
= new SAXBuilder();
     
    
// command line should offer URIs or file names
    try {
      builder.build(args[
0]);
      
// If there are no well-formedness errors, 
      
// then no exception is thrown
      System.out.println(args[0+ " is well-formed.");
    }
    
// indicates a well-formedness error
    catch (JDOMException e) { 
      System.out.println(args[
0+ " is not well-formed.");
      System.out.println(e.getMessage());
    }  
    
catch (IOException e) { 
      System.out.println(
"Could not check " + args[0]);
      System.out.println(
" because " + e.getMessage());
    } 
  }
}
2. 验证XML文件的Validity:
SAXBuilder only checks documents for well-formedness, not validity. If you want to validate as well, then pass the boolean true to the SAXBuilder() constructor. Then any validity errors will also cause JDOMExceptions.
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.IOException;


public class JDOMValidator {

  
public static void main(String[] args) {
  
    
if (args.length == 0) {
      System.out.println(
"Usage: java JDOMValidator URL"); 
      
return;
    } 
      
    SAXBuilder builder 
= new SAXBuilder(true);
                                    
//  ^^^^
                                    
// Turn on validation
     
    
// command line should offer URIs or file names
    try {
      builder.build(args[
0]);
      
// If there are no well-formedness or validity errors, 
      
// then no exception is thrown.
      System.out.println(args[0+ " is valid.");
    }
    
// indicates a well-formedness or validity error
    catch (JDOMException e) { 
      System.out.println(args[
0+ " is not valid.");
      System.out.println(e.getMessage());
    }  
    
catch (IOException e) { 
      System.out.println(
"Could not check " + args[0]);
      System.out.println(
" because " + e.getMessage());
    }  
  }
}

参考文献:http://www.ibiblio.org/xml/books/xmljava/chapters/ch14s07.html
posted @ 2006-04-03 17:41 bodong_chen 阅读(328) | 评论 (0)编辑 收藏
 
MSSQLServer2000

<data-sources>
                <data-source key="dataSource" type="org.apache.commons.dbcp.BasicDataSource">
                        <set-property property="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
                        <set-property property="url" value="jdbc:microsoft:sqlserver://localhost:1433;databaseName=ClassManageSys" />
                        <set-property property="username" value="sa" />
                        <set-property property="password" value="" />
                        <set-property property="maxActive" value="10" />
                        <set-property property="maxWait" value="5000" />
                        <set-property property="defaultAutoCommit" value="false" />
                        <set-property property="defaultReadOnly" value="false" />
                </data-source>
</data-sources>

MySQL

<struts-config>
        <data-sources>
                <data-source key="dataSource" type="org.apache.commons.dbcp.BasicDataSource">
                        <set-property property="driverClassName" value="org.gjt.mm.mysql.Driver" />
                        <set-property property="url" value="jdbc:mysql://localhost/test" />
                        <set-property property="username" value="root" />
                        <set-property property="password" value="" />
                        <set-property property="maxActive" value="10" />
                        <set-property property="maxWait" value="5000" />
                        <set-property property="defaultAutoCommit" value="false" />
                        <set-property property="defaultReadOnly" value="false" />
        </data-source>
</data-sources>

posted @ 2006-03-17 15:42 bodong_chen 阅读(954) | 评论 (1)编辑 收藏
 
花了一天时间学习Log4j,现在差不多已经达到了熟练运用的程度了。
关于Log4j的文章很多,而且已经写得很好了,我在这里写也只能是copy & paste那些大牛们的作品。在此推荐我觉得讲得最清楚的一篇文章:
https://www.qos.ch/ac2001/F11-10.html
希望能对Log4j的学习者有所帮助。我看了几篇中文的文章,其实都是对这篇文章进行的翻译,其中还有一些不当的地方,建议还是学习英文原文为上。
此外,Log4j的官方主页http://logging.apache.org/log4j/docs/index.html上可以下载到相关的资源。

最后谈一下个人心得。对于一个web项目来说,可以构建一个Debugger类,专门服务于其他的类和JSP,这样就免去了在每一需要进行Debuging的地方都定义一个Logger实例,使用起来很方便。下面提供参考代码:
 1 /*
 2  * Created on 2006-3-14
 3  *
 4  * TODO To change the template for this generated file go to
 5  * Window - Preferences - Java - Code Style - Code Templates
 6  */
 7 package util;
 8 
 9 import org.apache.log4j.Logger;
10 
11 /**
12  * @author dirkchen
13  *
14  * TODO To change the template for this generated type comment go to
15  * Window - Preferences - Java - Code Style - Code Templates
16  */
17 public class Debugger 
18 {
19     protected static Logger logger = Logger.getLogger("ChildrenProject");
20     
21     static
22     {
23         logger.setAdditivity( false );
24     }
25     
26     public static void error( Throwable e ) 
27     {
28         logger.error("", e);
29     }
30     
31     public static void debug( Object obj )
32     {
33         logger.debug( obj );
34     }
35 }
在其他地方只需要编写如下代码就可以进行debuging或者其他操作:
util.Debugger.debug(...);
当然,这时针对debug级的log信息。
posted @ 2006-03-15 15:52 bodong_chen 阅读(298) | 评论 (0)编辑 收藏
 
Log4j基本使用方法

Log4j由三个重要的组件构成:日志信息的优先级,日志信息的输出目的地,日志信息的输出格式。日志信息的优先级从高到低有ERROR、WARN、 INFO、DEBUG,分别用来指定这条日志信息的重要程度;日志信息的输出目的地指定了日志将打印到控制台还是文件中;而输出格式则控制了日志信息的显 示内容。

1.定义配置文件

其实您也可以完全不使用配置文件,而是在代码中配置Log4j环境。但是,使用配置文件将使您的应用程序更加灵活。

Log4j支持两种配置文件格式,一种是XML格式的文件,一种是Java特性文件(键=值)。下面我们介绍使用Java特性文件做为配置文件的方法:

配置根Logger,其语法为:
log4j.rootLogger = [ level ] , appenderName, appenderName, …

其中,level 是日志记录的优先级,分为OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或者您定义的级别。Log4j建议只使用四个级别,优 先级从高到低分别是ERROR、WARN、INFO、DEBUG。通过在这里定义的级别,您可以控制到应用程序中相应级别的日志信息的开关。比如在这里定 义了INFO级别,则应用程序中所有DEBUG级别的日志信息将不被打印出来。
appenderName就是指定日志信息输出到哪个地方。您可以同时指定多个输出目的地。
配置日志信息输出目的地Appender,其语法为
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = value1

log4j.appender.appenderName.option = valueN其中,Log4j提供的appender有以下几种:
org.apache.log4j.ConsoleAppender(控制台),
org.apache.log4j.FileAppender(文件),
org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件),org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件),
org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
配置日志信息的格式(布局),其语法为:
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
log4j.appender.appenderName.layout.option1 = value1

log4j.appender.appenderName.layout.option = valueN其中,Log4j提供的layout有以下几种:
org.apache.log4j.HTMLLayout(以HTML表格形式布局),
org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
2.在代码中使用Log4j

下面将讲述在程序代码中怎样使用Log4j。

2.1.得到记录器

使用Log4j,第一步就是获取日志记录器,这个记录器将负责控制日志信息。其语法为:

public static Logger getLogger( String name),

通过指定的名字获得记录器,如果必要的话,则为这个名字创建一个新的记录器。Name一般取本类的名字,比如:

static Logger logger = Logger.getLogger ( ServerWithLog4j.class.getName () ) ;

2.2.读取配置文件

当获得了日志记录器之后,第二步将配置Log4j环境,其语法为:
BasicConfigurator.configure (): 自动快速地使用缺省Log4j环境。
PropertyConfigurator.configure ( String configFilename) :读取使用Java的特性文件编写的配置文件。
DOMConfigurator.configure ( String filename ) :读取XML形式的配置文件。

2.3.插入记录信息(格式化日志信息)

当上两个必要步骤执行完毕,您就可以轻松地使用不同优先级别的日志记录语句插入到您想记录日志的任何地方,其语法如下:

Logger.debug ( Object message ) ;
Logger.info ( Object message ) ;
Logger.warn ( Object message ) ;
Logger.error ( Object message ) ;
posted @ 2006-03-14 14:04 bodong_chen 阅读(240) | 评论 (0)编辑 收藏
 
在Java中这样定义一个整型常量:
    static final int aImmutableInt = 10;
上边的语句定义了一个常量,并赋初值为10。定义一个常量用到了final的关键词。如果在程序的任何地方试图改变这个常量的值,都会遭到编译器的拒绝。
那么,我们能否定义一个常量对象呢?做如下的尝试:
首先定义了一个Circle类:
class Circle
{
  private double radius;

  public Circle(double r)
  {
    radius = r;
  }

  public void setRadius(double r)
  {
    radius = r;
  }

  public double getRadius()
  {
    return radius;
  }
}
下面,定义一个用final修饰的对象wheel,并试图改变它:
public class FinalTest
{
  private static final Circle wheel = new Circle(5.0);

  public static void main(String args[])
  {
    System.out.println("Radius of wheel is " + wheel.radius());
    wheel = setRadius(7.4);
    System.out.println("Radius of wheel is now " + wheel.radius());
  }
}
这时编译器会很爽快地答应,并输出如下结果:
Radius of wheel is 5.0
Radius of wheel is now 7.4
这是为什么呢?

在实践一中谈到了,在java中定义一个对象,这个对象名就是这个对象的一个引用,这个引用的值是内存中的一个地址。在上边的例子中,用final修饰的 是对象的引用的值,而引用类似于一个地址,这里好像就是在对这个引用说:嘿,哥们,你只能指向这个对象了,不能再指向其他的对象了!
而在我们的程序中,这个引用的值确实没有改变,被改变的只是对象的属性值,所以不会报错!
如果把wheel = setRadius(7.4);换成wheel = new Circle(7.4);则肯定不能编译通过。不信?你就试试!
posted @ 2005-11-23 15:38 bodong_chen 阅读(303) | 评论 (0)编辑 收藏
 
Copyright © bodong_chen Powered by: 博客园 模板提供:沪江博客