`
zl198751
  • 浏览: 274082 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

apache-commons 工具包学习

阅读更多

Commons是apache下属的开源子项目,专门提供一些通用的工具类。

这些方法用起来后,程序看起来更加简单,专业。

Commons的主要类结构:

 

Attributes Runtime API to metadata attributes such as doclet tags.
BeanUtils Easy-to-use wrappers around the Java reflection and introspection APIs.
Betwixt Services for mapping JavaBeans to XML documents, and vice versa.
Chain Chain of Responsibility pattern implemention.
CLI Command Line arguments parser.
Codec General encoding/decoding algorithms (for example phonetic, base64, URL).
Collections Extends or augments the Java Collections Framework.
Compress Defines an API for working with tar, zip and bzip2 files.
Configuration Reading of configuration/preferences files in various formats.
Daemon Alternative invocation mechanism for unix-daemon-like java code.
DBCP Database connection pooling services.
DbUtils JDBC helper library.
Digester XML-to-Java-object mapping utility.
Discovery Tools for locating resources by mapping service/reference names to resource names.
EL Interpreter for the Expression Language defined by the JSP 2.0 specification.
Email Library for sending e-mail from Java.
Exec API for dealing with external process execution and environment management in Java.
FileUpload File upload capability for your servlets and web applications.
IO Collection of I/O utilities.
JCI Java Compiler Interface
Jelly XML based scripting and processing engine.
Jexl Expression language which extends the Expression Language of the JSTL.
JXPath Utilities for manipulating Java Beans using the XPath syntax.
Lang Provides extra functionality for classes in java.lang.
Launcher Cross platform Java application launcher.
Logging Wrapper around a variety of logging API implementations.
Math Lightweight, self-contained mathematics and statistics components.
Modeler Mechanisms to create Model MBeans compatible with JMX specification.
Net Collection of network utilities and protocol implementations.
Pool Generic object pooling component.
Primitives Smaller, faster and easier to work with types supporting Java primitive types.
Proxy Library for creating dynamic proxies.
Sanselan A pure-Java image library.
SCXML An implementation of the State Chart XML specification aimed at creating and maintaining a Java SCXML engine. It is capable of executing a state machine defined using a SCXML document, and abstracts out the environment interfaces.
Transaction Implementations for multi level locks, transactional collections and transactional file access.
Validator Framework to define validators and validation rules in an xml file.
VFS Virtual File System component for treating files, FTP, SMB, ZIP and such like as a single logical file system.


Lang

Lang Provides extra functionality for classes in java.lang.

 

org.apache.commons.lang.builder. ToStringBuilder:

提供object重写toString()的工具类.

ApplicationToStringStyle.APP_TO_STRING_STYLE提供toString的style。

Fruit中重写了toString方法。

 

public abstract class ApplicationToStringStyle extends ToStringStyle {

    private static final long serialVersionUID = 1L;
    
    public static final ApplicationToStringStyle APP_TO_STRING_STYLE = new CustomToStringStyle();

    /**
     * Constructor
     *
     * Use the static constant rather than instantiating
     */
	private static final class CustomToStringStyle extends ApplicationToStringStyle {

		private static final long serialVersionUID = -4289672936337739486L;

		public CustomToStringStyle() {
	        super();
	        this.setUseClassName(true);
	        this.setUseIdentityHashCode(false);
	        this.setContentStart(SystemUtils.LINE_SEPARATOR + "[");
	        this.setFieldSeparator(SystemUtils.LINE_SEPARATOR + "  ");
	        this.setFieldSeparatorAtStart(true);
	        this.setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
	        
	        this.setUseShortClassName(true);
	    }

	    /**
	     * Override the append() method to skip fields that start with "_".  In this
	     * application, Axis generates some fields using this prefix that we don't want to include
	     * in the log files.  
	     */
		@Override
	    public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
	    	if (fieldName != null && !fieldName.startsWith("_")) {
	    		super.append(buffer, fieldName, value, fullDetail);
	    	}
	    }
	
	    /**
	     * Ensure Singleton after serialization
	     *
	     * @return the singleton
	     */
	    private Object readResolve() {
	        return ApplicationToStringStyle.APP_TO_STRING_STYLE;
	    }
	}   
}
 
public class Fruit {
    private String name = "apple";
    private String size = "3";
    private String price = "$40";

    public String toString() {
        return ReflectionToStringBuilder.toString(this, ApplicationToStringStyle.APP_TO_STRING_STYLE);
    }
}

 

HttpClient

The Commons HttpClient project used to be a part of Commons, but is now part of Apache HttpComponents - see Jakarta Commons HttpClient

 

用于http有关的连接,包括ssl等。

 

Notice that we go through the entire process regardless of whether the server returned an error or not. This is important because HTTP 1.1 allows multiple requests to use the same connection by simply sending the requests one after the other. Obviously, if we don't read the entire response to the first request, the left over data will get in the way of the second response. HttpClient tries to handle this but to avoid problems it is important to always release the connection.

 

基本http get访问

package commons.httpclient;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class Test {
    
    public static void main(String[] args) throws HttpException, IOException {
        //1. Create an instance of HttpClient. 
        HttpClient client = new HttpClient();
        
        //2.
        //Create an instance of one of the methods (GetMethod in this case). 
        //The URL to connect to is passed in to the the method constructor.
        GetMethod method = new GetMethod("http://www.baidu.com/");
        
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
                new DefaultHttpMethodRetryHandler(3, false));
        
        //3.Tell HttpClient to execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
          }
        
        //4.Read the response. 
        String reponse = method.getResponseBodyAsString();
        
        //5.Release the connection. 
        method.releaseConnection();
        
        //6.Deal with the response. 
        System.out.println(reponse);
    }
    
}
0
0
分享到:
评论
2 楼 zl198751 2010-12-31  
谢谢纠错,笔误了。

这篇帖子会跟着我的学习进度,逐步介绍commons里面的实用工具,现只是个开始。
1 楼 sarin 2010-12-30  
是apache,不是apach。没有什么实质性介绍

相关推荐

    apache-commons的常用工具包

    刚刚从apache官网下载的apache-commons的常用工具jar包,都是最新版本,有源码包,有api,包括了15种常用工具jar,比如common-lang,commons-collections,commons-io等等。也简单说明了每种工具包的用途。

    apache-commons源码及jar文件

    Apache Commons是一个非常有用的工具包,解决各种实际的通用问题。(附件中提供了该工具包的jar包,及源文件以供研究) BeanUtils Commons-BeanUtils 提供对 Java 反射和自省API的包装 Betwixt Betwixt提供将 ...

    org.apache.commons.lang jar包下载(commons-lang3-3.1.jar)

    commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE....

    可用org.apache.commons.httpclient-3.1.0.jar.zip

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....

    org.apache.commons.lang jar包下载

    commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE....

    org.apache.commonsjar包官方免费版

    本站为大家提供了org.apache.commons的jar包下载地址,Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动,需要此类JAR包的朋友们欢迎前来下载使用。 基本简介 commons包,根据...

    org.apache.commons 的 jar 包 源码

    org.apache.commons 的 jar 包 org.apache.commons的jar包,Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动,有需要的赶快来CSDN下载吧!

    orgapache_commons

    org.apache.commons的jar包,Apache Commons包含了很多开源的工具。 包括commons-beanutils-1.8.0-bin、commons-betwixt-0.8、commons-cli-1.1、commons-codec-1.3、commons-collections-3.2.1-bin、commons-...

    commons-math3-3.6.1-API文档中文版

    apache-commons-math3是java的一种科学计算类库,实现科学计算功能的类库其他语言如python、scala都有很多而且很容易找到资料,java可能是由于这方面的需求不多,所以相关的资料较少,详细的使用还是需要自己去研究...

    org.apache.commons工具包

    强大的apache工具包,大型项目中使用及其频繁

    apache-common-compress.rar

    用于压缩/解压缩的java开发工具包,基本上主流格式全包含,其中apache-common-compress内有5个jar包,两个是test的不用管,剩余三个,一个是源码包,一个是开发包另一个是javadoc。解压除RAR外的所有格式。 apache-...

    常用org.apache.commons整合

    常见的org.apache.commons,整合了一下,省得到处找了,开发必备 commons-beanutils-1.8.0 commons-betwixt-0.8 commons-cli-1.1 commons-codec-1.3 commons-collections-3.2.1 commons-digester-1.8 commons-...

    Apache Commons工具集

    天天都有人导入Apache的包,但是里面那么多工具类又有多少人使用过,这里面有一些使用介绍

    apache commons IO工具包

    apache commons 工具包中包括很多JAVA的实用工具。IO包主要是扩展JDK中的IO包,可用性很高。

    apache commons configuration 学习

    apache commons 工具包中提供的一个针对配置文件动态修改的工具类

    commons-lang3-3.4.jar官方工具包

    java 开发工具commons-lang3-3.4 jar包,有org.apache.commons.lang3.StringUtils; org.apache.commons.lang3.reflect.FieldUtils;等类

    比较全面的:Jakarta-commons jar包(附: chm参考手册 & 资源简介)

    commons-pool 提供了通用对象池接口,一个用于创建模块化对象池的工具包,以及通常的对象池实 commons-primitives java 简单类型使用的扩展 commons-proxy 创建动态代理的库 commons-scxml commons-transaction ...

    commons-lang.jar

    commons-lang.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache....

    Commons-dbutils1.7 jar包.rar

    commons-dbutils包是Apache开源组织提供的用于操作数据库的工具包。简单来讲,这个工具包就是用来更加方便我们操作数据库的,最近工作中使用了一下,感觉确实方便很多,基本告别自己封装JDBC代码对数据库进行增删改...

Global site tag (gtag.js) - Google Analytics