<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>coreoak@J2EE.com</title>
    <description>每一个人都是这个世界的匆匆过客，生命的价值就在于他为社会做了多少贡献！</description>
    <link>http://coreoak.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
      <item>
        <title>javascript innerText示例</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/230795" style="color:red;">http://coreoak.javaeye.com/blog/230795</a>&nbsp;
          发表时间: 2008年08月20日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          javascript innderText实例，当用户点击文字时改变显示的文字<br /><pre name="code" class="javascript">
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&lt;HTML>
 &lt;HEAD>
  &lt;TITLE> New Document &lt;/TITLE>
  &lt;META NAME="Generator" CONTENT="EditPlus">
  &lt;META NAME="Author" CONTENT="">
  &lt;META NAME="Keywords" CONTENT="">
  &lt;META NAME="Description" CONTENT="">
  &lt;style>
  body {color: "blue";font-family:"宋体";font-size:"12pt" }
  &lt;/style>
  &lt;script  type = "text/javascript">
	function changeText() {
		var text = document.getElementById("CT").innerText;		
		if (text.indexOf("你") > -1){
			CT.innerText = "我很好呀！"
		} else {
			CT.innerText = "你好吗?";
		}
	}
  &lt;/script>
 &lt;/HEAD>

 &lt;BODY>
  &lt;div>
	&lt;ul>
	&lt;li id = "CT" onclick = "changeText();">你好吗？&lt;/li>
	&lt;/ul>
  &lt;/div>
 &lt;/BODY>
&lt;/HTML>

</pre>
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/230795#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Wed, 20 Aug 2008 18:01:22 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/230795</link>
        <guid>http://coreoak.javaeye.com/blog/230795</guid>
      </item>
      <item>
        <title>Spring MVC4</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/187606" style="color:red;">http://coreoak.javaeye.com/blog/187606</a>&nbsp;
          发表时间: 2008年04月28日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          9) Sample Application<br />9.1) Introduction<br /><br />The final Section of this article details a Simple Contact Application that has provisions for Creating, Deleting and Listing Contact Objects. The aim of this Application is to show the various use of Controller Components like Abstract Controller, Abstract Command Controller and Form Controller along with Configuration Information.<br />9.2) The Web Descriptor File<br /><br />As mentioned previously, since the Dispatcher Servlet acts as an Interceptor for the Client Request, an entry for the same has to be mentioned in the web.xml file. Follow is the code snippet for the same,<br /><br />web.xml<br /><br />				<br />&lt;?xml version="1.0" encoding="UTF-8"?><br />&lt;web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" <br />xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee<br />http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><br /><br />    &lt;servlet><br />        &lt;servlet-name>dispatcher&lt;/servlet-name><br />        &lt;servlet-class><br />            org.springframework.web.servlet.DispatcherServlet<br />        &lt;/servlet-class><br />        &lt;load-on-startup>2&lt;/load-on-startup><br />    &lt;/servlet><br /><br />    &lt;servlet-mapping><br />        &lt;servlet-name>dispatcher&lt;/servlet-name><br />        &lt;url-pattern>*.htm&lt;/url-pattern><br />    &lt;/servlet-mapping><br /><br />&lt;/web-app><br />				<br /><br />9.3) Configuration File<br /><br />The following represents the Configuration File for holding various piece of Configuration Information. The first thing to note is the type of Handler Mapping configured. In our case, it is the Bean Name Url Handler Mapping which means that the Url of the Client is tightly coupled with the class name of the Bean (Controller). Since all the Jsp files are maintained in the '/WEB/contacts' directory the 'prefix' property is pointing to '/WEB/contacts'.<br /><br />For the Create, Delete and List operation on Contacts, three different Controller Components have been defined. They are CreateContactController, DeleteContactController and ListContactsController respectively.<br /><br />dispatcher-servlet.xml<br /><br />				<br />&lt;?xml version="1.0" encoding="UTF-8" ?><br />&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" <br />"http://www.springframework.org/dtd/spring-beans.dtd"><br />&lt;beans><br /><br />    &lt;bean id="beanNameUrlMapping" class="org.springframework.web.servlet.handler.<br />    BeanNameUrlHandlerMapping"/><br /><br />    &lt;bean name = "/CreateContact.htm" class="net.javabeat.articles.spring.mvc.<br />    contacts.CreateContactController"><br /><br />        &lt;property name="formView"><br />            &lt;value>CreateContact&lt;/value><br />        &lt;/property><br />        &lt;property name="successView"><br />            &lt;value>ContactCreated&lt;/value><br />        &lt;/property>    <br /><br />    &lt;/bean>    <br /><br />    &lt;bean name = "/DeleteContact.htm" class= "net.javabeat.articles.spring.mvc.<br />    contacts.DeleteContactController">                    <br />    &lt;/bean>    <br /><br />    &lt;bean name = "/ListContacts.htm" class= "net.javabeat.articles.spring.mvc.<br />    contacts.ListContactsController">                    <br />    &lt;/bean>    <br /><br />    &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.<br />    InternalResourceViewResolver"><br />        &lt;property name="prefix" value="/WEB-INF/contacts/"/><br />        &lt;property name="suffix" value=".jsp" /><br />    &lt;/bean><br /><br />&lt;/beans><br />				<br /><br />9.4) CreateContact and ContactCreated Jsp Files<br /><br />The following is the code for CreateContact.jsp file.<br /><br />CreateContact.jsp<br /><br />				<br />&lt;html><br />&lt;head><br />&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br /><br />    &lt;title>Create a Contact&lt;/title><br />    &lt;/head><br />    &lt;body><br /><br />    &lt;h1>Create a Contact&lt;/h1><br /><br />    &lt;form name = "CreateContact" method = "get""><br />        &lt;input type = "text" name = "firstname" /><br />        &lt;input type = "text" name = "lastname" />        <br />        &lt;br><br />        &lt;input type="submit" name = "Create Contact" value = "Create Contact"/><br />   &lt;/form><br /><br />&lt;/body><br />&lt;/html><br />				<br /><br />Note that since this is the page that will be shown to the user initially, in the Configuration file, the property 'formView' is pointed to 'CreateContact'. Following is the code for ContactCreated.jsp. Since this is the View that will be shown after the Form Submission the property 'successView' is made to point to 'ContactCreated'.<br /><br />ContactCreated.jsp<br /><br />				<br />&lt;html><br />    &lt;head><br />    &lt;meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"><br />    &lt;title>Contact is Created&lt;/title><br />    &lt;/head><br /><br />    &lt;body><br /><br />    &lt;h1>Contact is successfully Created&lt;/h1><br /><br />   &lt;/body><br />&lt;/html><br />				<br /><br />9.5) DeleteContact.jsp<br /><br />Following is the complete listing for DeleteContact.jsp file. Note that this Jsp File is mapped to DeleteContactController in the Configuration File.<br /><br />DeleteContact.jsp<br /><br />				<br />&lt;html><br />&lt;head><br />&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br /><br />    &lt;title>Delete Contact&lt;/title><br />    &lt;/head><br />    &lt;body><br /><br />    &lt;h1>Delete Contact&lt;/h1><br /><br />    &lt;form name = "DeleteContact" method = "get"><br />        &lt;input type = "text" name = "firstname" /><br />        &lt;br><br />        &lt;input type="submit" name = "DeleteContact" value = "Delete Contact"/><br />    &lt;/form><br /><br />    &lt;/body><br />&lt;/html><br />				<br /><br />9.6) ListContacts.jsp<br /><br />This page is to list all the existing Contacts that were created before. It should be noted that the Model Object that holds all the Contact Information in the form of List is available in the ListContactsController. The Model Information from the Controller after getting bound to the Request Scope is being taken off from the View in the form of Expression Language.<br /><br />Following is the listing for ListContacts.jsp<br /><br />ListContacts.jsp<br /><br />				<br />&lt;html><br />&lt;head><br />&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br /><br />    &lt;title>Showing All Contacts&lt;/title><br />    &lt;/head><br />    &lt;body><br /><br />    &lt;h1>Showing All Contacts&lt;/h1><br /><br />    &lt;p> The following are the created Contacts &lt;/p><br /><br />        &lt;c:forEach items = "${allContacts}" var="contact"><br />            &lt;c:out value="${contact.firstname}"/>&lt;br><br />            &lt;c:out value="${contact.lastname}"/>&lt;br><br />        &lt;/c:forEach><br /><br />    &lt;/body><br />&lt;/html><br />				<br /><br />9.7) Contacts.java<br /><br />The following is the Class structure for Contacts.java for encapsulating the properties firstname and lastname.<br /><br />Contact.java<br /><br />				<br />package net.javabeat.articles.spring.mvc.contacts;<br /><br />public class Contact {<br /><br />    private String firstName;<br />    private String lastName;<br /><br />    public Contact() {<br />    }<br /><br />    public Contact(String firstName, String lastName){<br />        this.firstName = firstName;<br />        this.lastName = lastName;<br />    }<br /><br />    public String getFirstName() {<br />        return firstName;<br />    }<br /><br />    public void setFirstName(String firstName) {<br />        this.firstName = firstName;<br />    }<br /> <br />    public String getLastName() {<br />        return lastName;<br />    }<br /><br />    public void setLastName(String lastName) {<br />        this.lastName = lastName;<br />    }<br /><br />    public int hashCode(){<br />        return firstName.hashCode() + lastName.hashCode();<br />    }<br /><br />    public boolean equals(Object object){<br />        if (object instanceof Contact){<br />            Contact second = (Contact)object;<br />            return (firstName.equals(second.getFirstName()) && <br />                lastName.equals(second.getLastName()));<br />        }<br />        return false;<br />    }<br /><br />    public String toString(){<br />        return "[First Name = " + firstName + ", Last Name = " + lastName + "]";<br />    }<br />}<br />				<br /><br />9.8) ContactService.java<br /><br />This simple service class provides functionalities for creating, deleting and listing the Contact information. All the Controller Components makes use of this class to achieve their respective functionalities.<br /><br />ContactService.java<br /><br />				<br />package net.javabeat.articles.spring.mvc.contacts;<br /><br />import java.util.*;<br /><br />public class ContactService {<br /><br />    private static Map contacts = new HashMap();<br /><br />    public ContactService() {<br />    }<br /><br />    public static Contact createContact(Contact contact){<br />        contacts.put(new Integer(contact.hashCode()), contact);<br />        return contact;        <br />    }<br /><br />    public static Contact createContact(String firstName, String lastName){<br />        return createContact(new Contact(firstName, lastName));<br />    }<br /><br />    public static boolean deleteContact(String firstName){<br />        Iterator iterator = contacts.entrySet().iterator();        <br />        while (iterator.hasNext()){<br />            Map.Entry entry = (Map.Entry)iterator.next();<br />            Contact contact = (Contact)entry.getValue();            <br />            if (contact.getFirstName().equals(firstName)){<br />                contacts.remove(new Integer(contact.hashCode()));<br />                return true;<br />            }            <br />        }<br />        return false;<br />    }<br /><br />    public static List listContacts(){<br />        return toList(contacts);<br />    }    <br /><br />    private static List toList(Map contacts){<br />        List contactList = new ArrayList();<br />        Iterator iterator = contacts.entrySet().iterator();<br />        while (iterator.hasNext()){<br />            Map.Entry entry = (Map.Entry)iterator.next();<br />            Contact contact = (Contact)entry.getValue();            <br />            contactList.add(contact);<br />        }<br />        return contactList;<br />    }<br />}<br />				<br /><br />9.9) Controller Classes<br /><br />Following is the listing for CreateContact Controller. Note that since the Model Information for creating a contact (for which the Client supplies the firstname and the lastname parameters) is the Contact class, call has been made in the Constructor to setCommandClass() by passing the class name of the Contact class.<br /><br />CreateContactController.java<br /><br />				<br />package net.javabeat.articles.spring.mvc.contacts;<br /><br />import org.springframework.web.servlet.mvc.SimpleFormController;<br /><br />public class CreateContactController extends SimpleFormController{<br /><br />    public CreateContactController() {<br />        setCommandClass(Contact.class);<br />    }<br /><br />    public void doSubmitAction(Object command){<br />        Contact contact = (Contact)command;<br />        ContactService.createContact(contact);<br />    }        <br />}<br />				<br /><br />Note that the method doSubmitAction() doesn't return anything because the next Logical View to be displayed will be taken from the Configuration file which is represented by the property called 'successView'.<br /><br />Following two classes are the Controller Components for Deleting and Listing Contacts. Note that in the case of Delete Operation, a Jsp Page (DeletedContact.jsp) containing information telling that the Contact has been Deleted will displayed. But since for the Contact Listing operation, the model information containing a Collection of Contact Objects has to be passed from the Controller to the View and the same is achieved in the 3 argument constructor to ModelAndView.<br /><br />DeleteContactController.java<br /><br />				<br />package net.javabeat.articles.spring.mvc.contacts;<br /><br />import javax.servlet.http.*;<br />import org.springframework.validation.BindException;<br />import org.springframework.web.servlet.ModelAndView;<br />import org.springframework.web.servlet.mvc.AbstractCommandController;<br /><br />public class DeleteContactController extends AbstractCommandController{<br /><br />    public DeleteContactController(){<br />        setCommandClass(Contact.class);<br />    }<br /><br />    public ModelAndView handle(HttpServletRequest request, <br />        HttpServletResponse response, Object command, BindException errors) <br />            throws Exception {<br /><br />        Contact contact = (Contact)command;<br />        ContactService.deleteContact(contact.getFirstName());<br />        return new ModelAndView("DeletedContact");<br /><br />    }<br />}<br />				<br /><br />Here is the listing for ListContactsController.java.<br /><br />ListContactsController.java<br /><br />				<br />package net.javabeat.articles.spring.mvc.contacts;<br /><br />import java.util.List;<br />import javax.servlet.http.*;<br />import org.springframework.web.servlet.ModelAndView;<br />import org.springframework.web.servlet.mvc.AbstractController;<br /><br />public class ListContactsController extends AbstractController{<br /><br />    public ListContactsController() {<br />    }<br /><br />    public ModelAndView handleRequestInternal(HttpServletRequest request, <br />        HttpServletResponse response) throws Exception{<br /><br />        List allContacts = ContactService.listContacts();<br />        return new ModelAndView("ListContacts", "allContacts", allContacts);<br /><br />    }<br />}<br />				<br /><br />10) Conclusion<br /><br />The Article provided a basic Introduction over the core concepts in Spring MVC for the Web Tier. Since the Spring Web Tier is built on top of the Spring Core Layer all the functionalities like Bean Lifecycle Management, Dependency Injection etc. will be automatically available to the Bean Components. Starting off with the Interactions that will take place when a Client Request for a Resource, it pointed out the various micro-level activities that take place in that Work flow. Then the Core Components of Spring like Dispatcher Servlet, Controllers, Model/View, Handler Mappings, Handler Adapters and View Resolvers are also discussed briefly. Finally the article ended up with the Simple Contact Application that demonstrated the usage of the various types of Controllers.
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/187606#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 28 Apr 2008 16:40:10 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/187606</link>
        <guid>http://coreoak.javaeye.com/blog/187606</guid>
      </item>
      <item>
        <title>Spring MVC3</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/187600" style="color:red;">http://coreoak.javaeye.com/blog/187600</a>&nbsp;
          发表时间: 2008年04月28日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          6) Controller<br /><br />Controllers are components that are being called by the Dispatcher Servlet for doing any kind of Business Logic. Spring Distribution already comes with a variety of Controller Components each doing a specific purpose. All Controller Components in Spring implement the org.springframework.web.servlet.mvc.Controller interface. This section aimed to provide the commonly used Controllers in the Spring Framework. The following are the Controller Components available in the Spring Distribution.<br /><br />    * SimpleFormController<br />    * AbstractController<br />    * AbstractCommandController<br />    * CancellableFormController<br />    * AbstractCommandController<br />    * MultiActionController<br />    * ParameterizableViewController<br />    * ServletForwardingController<br />    * ServletWrappingController<br />    * UrlFilenameViewController<br /><br />The following section covers only on AbstractController, AbstractCommandController, SimpleFormController and CancellableFormController in detail.<br />6.1) Abstract Controller<br /><br />If one wants to implement Custom Controller Component right from the scratch, then instead of implementing the Controller interface, extending AbstractController can be preferred as it provides the basic support for the GET and the POST methods. It is advised that only for simple purpose, this type of extensions should be used. The purpose may be as simple as returning a resource to the Client upon request without having the need to examine the Request Parameters or other Stuffs. For example, consider the following piece of code,<br /><br />MySimpleController.java<br /><br />				<br />public class MySimpleController extends AbstractController{<br /><br />    public void handleRequestInternal(HttpServletRequest request, <br />	HttpServletResponse response){<br /><br />        return new ModelAndView("myView");<br /><br />    }<br />}<br />				<br /><br />Note that the Dispatcher Servlet will call the handleRequest() method by passing the Request and the Response parameters. The implementation just returns a ModelAndView (discussed later) object with myView being the logical view name. There are Components called View Resolvers whose job is to provide a mapping between the Logical View Name and the actual Physical Location of the View Resource. For the time being, assume that somehow, myView is mapped to myView.jsp. So, whenever the Dispatcher Servlet invokes this MySimpleController object, finally myView.jsp will be rendered back to the Client.<br />6.2) Abstract Command Controller<br /><br />The concept of Command Controller comes into picture when the Business Logic depends upon the values that are submitted by the User. Instead of depending on the Servlet Api to get the Request Parameter Values and other session Objects, we can depend on this Abstract Command Controller to take those pieces of Information. For example consider the following code snippet which has a simple business logic telling that, depending on the existence of username, display the form success.jsp or failure.jsp<br /><br />MySimpleController.java<br /><br />				<br />public class MySimpleController extends AbstractCommandController{<br /><br />    public MySimpleController(){<br />        setCommandClass(UserInfo.class);<br />    }<br /><br />    public void handle(HttpServletRequest request, HttpServletResponse response,<br />	Object command){<br /><br />        UserInfo userInfo = (UserInfo)command;<br />        if ( exists(userInfo.getUserName){<br />            return new ModelAndView("success");<br />        }else{<br />            return new ModelAndView("failure");<br />        }<br />    }<br /><br />    private boolean exits(String username){<br />        // Some logic here.<br />    }<br />}<br />				<br /><br />Note that the Client Parameters (username , in this case) is encapsulated in a simple Class called UserInfo which is given below. The value given by the Client for the username field will be directly mapped to the property called username in the UserInfo. In the Constructor of the MySimpleController class, we have mentioned the name of the Command Class which is going to hold the Client Request Parameters by calling the setCommandClass() method. Also note that in the case of Command Controller, the method that will be called by the Dispatcher Servlet will be handle() which is passed with the Command object apart from the Request and the Response objects.<br /><br />UserInfo.java<br /><br />				<br />public class UserInfo{<br /><br />    private String username;<br />    // Getters and Setters here.<br /><br />}<br />				<br /><br />6.3) Simple Form Controller<br /><br />Asking the User to fill in a Form containing various information and submitting the form normally happens in almost every Web Application. The Simple Form Controller is exactly used for that purpose. Let us give a simple example to illustrate this. Assume that upon Client Request a page called empInfo.jsp is rendered to the client containing empName, empAge and empSalary fields. Upon successful completion a Jsp Page called empSuccess.jsp is displayed back to the Client. Now let us see how we can make use of the Simple Form Controller to achieve this kind functionality.<br /><br />The very first thing is that, to collect the Client Input Values, a Command object which contains getter and setters must be defined. Following the skeleton of the class called EmpInfo.<br /><br />EmpInfo.java<br /><br />				<br />public class EmpInfo{<br /><br />    private String empName;<br />    private int empAge;<br />    private double empSalary;<br /><br />    // Getters and setters for the above properties.<br /><br />}<br />				<br /><br />The next thing is to write a class that extends SimpleFormController. But this time, the doSubmitAction() method should be overridden. This is the method that will be called when the Client submits the form. Following is the definition of the Controller class.<br /><br />EmpFormController.java<br /><br />				<br />public class EmpFormController extends SimpleFormController{<br /><br />    public EmpFormController(){<br />        setCommandClass(EmpInfo.class);<br />    }<br /><br />    public void doSubmitAction(Object command){<br />        EmpInfo info = (EmpInfo)command;<br />        process(info);<br />    }<br /><br />    private void process(EmpInfo info){<br />        //Do some processing with this object.<br />    }<br />}<br />				<br /><br />As we mentioned previously, the form that collects information from the Client is empInfo.jsp and upon successful submission the view empSuccess.jsp should be displayed. This information is externalized from the Controller class and it is maintained in the Configuration File like the following,<br /><br />				<br />&lt;bean id = "empForm" class="EmpFormController"><br /><br />    &lt;property name="formView"><br />        &lt;value>empInfo&lt;/value><br />    &lt;/property><br /><br />    &lt;property name="successView"><br />        &lt;value>empSuccess&lt;/value><br />    &lt;/property><br /><br />&lt;/bean><br />				<br /><br />Note the two property names 'formView' and 'successView' along with the values 'empInfo' and 'empSuccess'. These properties represent the initial View to be displayed and the final view (after successful Form submission) to be rendered to the Client.<br />6.4) Cancellable FormController<br /><br />If you carefully notice with the implementation of Simple Form Controller, there are ways to provide the Initial and the Successful View to the Clients. But what happens when the Form is cancelled by the User? Who will process the Cancel operation of the Form?<br /><br />The above issues can be given immediate solution with the usage of Cancellable FormController. The good thing is that Cancellable FormController extends SimpleForm Controller so that all the functionalities are visible to this Controller also. Suppose say that the User clicks the cancel button, the Framework will check in the Request parameter for a key with name 'cancelParamKey'. If it is so, then it will call the onCancel() method. Consider the following definition,<br /><br />MyCompleteFormController.java<br /><br />				<br />public class MyCompleteFormController extends CancellableFormController{<br /><br />    public ModelAndView onCancel(){<br />        return new ModelAndView("cancelView");<br />    }<br />}<br />				<br /><br />7) Model And View<br /><br />Model and View (represented by the class org.springframework.web.servlet.ModelAndView) is returned by the Controller object back to the Dispatcher Servlet. This class is just a Container class for holding the Model and the View information. The Mode object represents some piece of information that can be used by the View to display the information. Both these Objects are given high degree of abstraction in the Spring Framework.<br /><br />Any kind of View Technology (org.springframework.web.servlet.View) can be plugged into the Framework with ease. For example, Excel, Jasper Reports, Pdf, Xslt, Free Marker, Html, Tiles, Velocity etc. are the supported Frameworks as of now. The Model object (represented by org.springframework.ui.ModelMap) is internally maintained as a Map for storing the Information.<br /><br />Following are the ways to Construct the Model and the View object.<br /><br />				<br />View pdfView = �;<br />Map modelData = new HashMap();<br /><br />ModelAndView mv1 = new ModelAndView(pdfView, modelData);<br />				<br /><br />The above constructs a ModelAndView object by passing the actual View object along with the Model object. Now consider the following code,<br /><br />				<br />ModelAndView mv1 = new ModelAndView("myView", someData);<br />				<br /><br />Note, in the above example, a string with "myView" is passed for the View. This way of specifying a View is called a Logical View. It means that myView either can point to something called myView.jsp or myView.pdf or myView.xml. The Physical View Location corresponding to the Logical View can be made configurable in the Configuration File.<br />8) View Resolver<br /><br />In the previous section, we talked about Logical View and the Physical View Location for the Logical View. The mapping between the Logical name and the Physical View Location is taken care by the View Resolver object. Without any surprise, Spring comes with a set of Built-In Spring Resolvers. It is even possible to write Custom View Resolvers by implementing the org.springframework.web.servlet.ViewResolver interface. Following are the available View Resolvers in the Spring Distribution.<br /><br />    * BeanNameViewResolver<br />    * FreeMarkerViewResolver<br />    * InternalResourceViewResolver<br />    * JasperReportsViewResolver<br />    * ResourceBundleViewResolver<br />    * UrlBasedViewResolver<br />    * VelocityLayoutViewResolver<br />    * VelocityViewResolver<br />    * XmlViewResolver<br />    * XsltViewResolver<br /><br />The following section concentrates only on Internal Resource View Resolver and Bean Name View Resolver in detail.<br />8.1) Internal Resource View Resolver<br /><br />The Internal Resource View Resolver will try to map the Logical name of the Resource as returned by the Controller object in the form of ModelAndView object to the Physical View location. For example, consider the following class definition which returns different ModelAndView objects.<br /><br />MyController.java<br /><br />				<br />public class MyController {<br /><br />    public void handle(){<br />        if(condition1()){<br />            return new ModelAndView("myView1");<br />        }else if (condition2()){<br />            return new ModelAndView("myView2");<br />        }<br />        return new ModelAndView("myView3");<br />    }<br />}<br />				<br /><br />Assume that if the Client Request satisfies condition1(), then the view myView.jsp which is present in the /WEB-INF folder should be displayed and for the client Requests satisfying condition2() and the other one, myView2.jsp and myView3.jsp should be displayed.<br /><br />For this to happen, the following entry must be made in the Configuration File,<br /><br />				<br />&lt;bean id="viewResolver" class="org.springframework.web.servlet.view.<br />InternalResourceViewResolver"><br /><br />    &lt;property name="prefix">&lt;value>/WEB-INF/&lt;/value>&lt;/property><br />    &lt;property name="suffix">&lt;value>.jsp&lt;/value>&lt;/property><br /><br />&lt;/bean><br />				<br /><br />This is how the Internal Resource View Resolver will map the Logical View Name to the physical Location. When the logical View name is myView1, then it will construct a view name which is the summation of the prefix + the logical View Name + the suffix, which is going to be /WEB-INF/myView.jsp. The same is the case for myView2.jsp and myView3.jsp.<br />8.2) Bean Name View Resolver<br /><br />One of the dis-advantage of using Internal Resource View Resolver is that the name of the View file (whether it is a Jsp File or the Pdf File) must be present in the Web Application Context. Dynamically generated View files may not be possible. In such a case, we may use the Bean Name View Resolver which will dynamically generate View in Pdf or Excel Formats.<br /><br />For the example, if the ModelAndView object represents a View by name "pdf" as shown in the following snippet,<br /><br />				<br />return ModelAndView("pdf")<br />				<br /><br />And, if we want to generate the Pdf file, then we should have defined the Configuration information in the file as follows,<br /><br />				<br />&lt;bean id="beanNameResolver" <br />class="org.springframework.web.servlet.view.BeanNameViewResolver"/><br />				<br /><br />The above code configures the Framework to use BeanNameViewResolver. Since the logical name 'pdf' must resolve to a Bean Name, we should define a similar entry like the following in the Configuration File. Note that, in the following MyPdfGenerator may be the sub-class of org.springframework.web.servlet.view.document.AbstractPdfView for generating the Pdf File.<br /><br />				<br />&lt;bean id = " pdf " class = "MyPdfGenerator"/>
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/187600#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 28 Apr 2008 16:18:24 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/187600</link>
        <guid>http://coreoak.javaeye.com/blog/187600</guid>
      </item>
      <item>
        <title>Spring MVC2</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/187599" style="color:red;">http://coreoak.javaeye.com/blog/187599</a>&nbsp;
          发表时间: 2008年04月28日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          4) Handler Mappings<br /><br />When the Client Request reaches the Dispatcher Servlet, the Dispatcher Servlet tries to find the appropriate Handler Mapping Object to map between the Request and the Handling Object. A Handler Mapping provides an abstract way that tell how the Client's Url has to be mapped to the Handlers. Four concrete variation of Handler Mapping are available. They are defined as follows<br /><br />    * BeanNameUrl HandlerMapping<br />    * CommonsPathMap HandlerMapping<br />    * ControllerClassName HandlerMapping<br />    * SimpleUrl HandlerMapping<br /><br />All the above Handler Mapping objects are represented as BeanNameUrlHandlerMapping, CommonsPathMapHandlerMapping, ControllerClassNameHandlerMapping and SimpleUrlHandlerMapping in the org.springframework.web.servlet package respectively. Let us see the functionalities and the differences in usage one by one.<br />4.1) BeanNameUrl HandlerMapping<br /><br />This is the simplest of the Handler Mapping and it is used to map the Url that comes from the Clients directly to the Bean Object. In the later section, we will see that the Bean is nothing but a Controller object. For example, consider that the following are the valid Url in a Web Application that a Client Application can request for.<br /><br />				<br />http://myserver.com/eMail/showAllMails<br />http://myserver.com/eMail/composeMail<br />http://myserver.com/eMail/deleteMail<br />				<br /><br />Note that the Url (excluding the Application Context) in the above cases are 'showAllMails', 'composeMail' and 'deleteMail'. This means that the Framework will look for Bean Definitions with Identifiers 'showAllMails', 'composeMail' and 'deleteMail'. Consider the following Xml code snippet in the Configuration file,<br /><br />				<br />&lt;beans><br /><br />    &lt;bean name="/showAllMails.jsp"<br />    class="com.javabeat.net.ShowAllMailsController"><br />    &lt;/bean><br /><br />    &lt;bean name="/composeMail.jsp"<br />    class="com.javabeat.net.ComposeMailController"><br />    &lt;/bean><br /><br />    &lt;bean name="/ deleteMail.jsp"<br />    class="com.javabeat.net.DeleteMailController"><br />    &lt;/bean><br /><br />&lt;/beans><br />				<br /><br />So, in BeanNameUrl Handler Mapping, the Url of the Client is directly mapped to the Controller. To enable this kind of Handler Mapping in the Application, the Configuration file should have a similar kind of definition like the following,<br /><br />				<br />&lt;beans><br /><br />    �<br />   &lt;bean id="beanNameUrl" <br />   class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><br />    �<br /><br />&lt;/beans><br />				<br /><br />4.2) CommonsPathMap HandlerMapping<br /><br />This is a rarely used Handler Mapping in which case, the name of the Url to which the Controller has to be mapped is specified directly in the Source file of the Controller. Considering the previous example, if we want to map 'showAllMails', 'composeMail' and 'deleteMail' to Controllers namely ShowAllMailsController, ComposeMailController and DeleteMailController, then the mapping information must be specified in the form of meta-data in the source files inside the Javadoc comments. Consider the following Controller Definitions,<br /><br />				<br />/**<br />*@@ org.springframework.web.servlet.handler.commonsattributes.<br />*PathMap("/showAllMails.jsp")<br />*/<br />public class ShowAllMailsController{<br />}<br /><br />/**<br />*@@ org.springframework.web.servlet.handler.commonsattributes.<br />*PathMap("/composeMail.jsp")<br />*/<br />public class ComposeMailController{<br />}<br /><br />/**<br />*@@ org.springframework.web.servlet.handler.commonsattributes.<br />*PathMap("/deleteMail.jsp")<br />*/<br />public class DeleteMailController {<br />}<br />				<br /><br />The attribute must point to org.springframework.web.servlet.handler.commonsattributes.PathMap. By defining Controllers in this way, one more additional compilation step is needed. That is to make the availability of this attribute in the Class files, this Java Source has to be compiled with the Commons Attribute Compiler which comes along with the Spring Distribution. As before, to enable this kind of mapping , the Configuration File should have an entry similar to this,<br /><br />				<br />&lt;beans><br /><br />    &lt;bean id="metaHandlerMapping" class="org.springframework.web.servlet.handler.<br />    metadata.CommonsPathMapHandlerMapping"/><br /><br />&lt;/beans><br />				<br /><br />4.3) ControllerClassName HandlerMapping<br /><br />In this kind of Handler Mapping, the name of the Controller is taking directly from the Url itself with slight modifications. For example, let us assume that the Client request ends with Url as shown below,<br /><br />				<br />http://myserver.com/emailApp/showInbox.jsp<br />http://myserver.com/emailApp/showDeletedItems.jsp<br />				<br /><br />And as such, we have a Controller definition by name ShowController as follows,<br /><br />ShowController.java<br /><br />				<br />public class ShowController{<br />}<br />				<br /><br />Also the Configuration file is made to activate this kind of Handler Mapping by making the following definition,<br /><br />				<br />&lt;beans><br /><br />    &lt;bean id="controllerClassName" class="org.springframework.web.servlet.handler.<br />    metadata.ControllerClassNameHandlerMapping"/><br /><br />&lt;/beans><br />				<br /><br />The first thing the Framework does it, it will traverse through the List of Controllers defined in the Configuration File and perform these actions. For the Controller ShowController, then Framework will remove the Controller String and then lowercase the first letter. In our case the string now becomes show. Now whatever Client Request matches the pattern /show*, then the ShowController will be invoked.<br />4.4) SimpleUrl HandlerMapping<br /><br />This is the Simplest of all the Handler Mappings as it directly maps the Client Request to some Controller object. Consider the following Configuration File,<br /><br />				<br />&lt;bean id="simpleUrlMapping" <br />class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><br /><br />    &lt;property name="mappings"><br />        &lt;props><br />            &lt;prop key="/showAllMails.jsp">showController&lt;/prop><br />            &lt;prop key="/composeMail.jsp">composeController&lt;/prop><br />            &lt;prop key="/deleteMail.jsp">deleteController&lt;/prop><br />        &lt;/props><br />    &lt;/property><br /><br />&lt;/bean><br />				<br /><br />The set of mappings is encapsulated in the 'property' tag with each defined in a 'prop' element with the 'key' attribute being the Url, the value being the Identifier of the Controller Objects. Note that the Beans for the above Identifiers should be defined somewhere in the Configuration File.<br />5) Handler Adapters<br /><br />It is important to understand that the Spring Framework is so flexible enough to define what Components should be delegated the Request once the Dispatcher Servlet finds the appropriate Handler Mapping. This is achieved in the form of Handler Adapters. If you remember in the Spring Work flow section, that it is mentioned once the Dispatcher Servlet chooses the appropriate Handler Mapping, the Request is then forwarded to the Controller object that is defined in the Configuration File. This is the default case. And this so happens because the Default Handler Adapter is Simple Controller Handler Adapter (represented by org.springframework.web.servlet.SimpleControllerHandlerAdapter), which will do the job of the Forwarding the Request from the Dispatcher to the Controller object.<br /><br />Other types of Handler Adapters are Throwaway Controller HandlerAdapter (org.springframework.web.servlet.ThrowawayControllerHandlerAdapter) and SimpleServlet HandlerAdapter (org.springframework.web.servlet.SimpleServletHandlerAdapter). The Throwaway Controller HandlerAdapter, for example, carries the Request from the Dispatcher Servlet to the Throwaway Controller (discussed later in the section on Controllers) and Simple Servlet Handler Adapter will carry forward the Request from the Dispatcher Servlet to a Servlet thereby making the Servlet.service() method to be invoked.<br /><br />If, for example, you don't want the default Simple Controller Handler Adapter, then you have to redefine the Configuration file with the similar kind of information as shown below,<br /><br />				<br />&lt;bean id="throwawayHandler" class = "org.springframework.web.servlet.mvc.throwaway.<br />    ThrowawayControllerHandlerAdapter"/><br />				<br /><br />or<br /><br />				<br />&lt;bean id="throwawayHandler" class="org.springframework.web.servlet.mvc.throwaway.<br />    SimpleServletHandlerAdapter"/><br />				<br /><br />Even, it is possible to write a Custom Handler Adapter by implementing the HandlerAdapter interface available in the org.springframework.web.servlet package.
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/187599#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 28 Apr 2008 16:17:19 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/187599</link>
        <guid>http://coreoak.javaeye.com/blog/187599</guid>
      </item>
      <item>
        <title>spring MVC</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/187598" style="color:red;">http://coreoak.javaeye.com/blog/187598</a>&nbsp;
          发表时间: 2008年04月28日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          Introduction to Spring MVC Web Framework - Web Tier<br />1) Introduction<br /><br />The Spring MVC provides rich functionality for building robust Web Applications and it is available as a separate module in the Distribution. As a pre-requisite, readers are advised to go through the introductory article on Spring Framework http://spring.javabeat.net/articles/2007/06/introduction-spring-web-framework. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly configurable. Also Spring can integrate effortlessly with other popular Web Frameworks like Struts, WebWork, Java Server Faces and Tapestry. It means that you can even instruct Spring to use any one of the Web Frameworks. More than that Spring is not tightly coupled with Servlets or Jsp to render the View to the Clients. Integration with other View technologies like Velocity, Freemarker, Excel or Pdf is also possible now. This article provides an introduction over the various components that are available in the Spring MVC for the Web Tier. Specifically the major Core Components like Dispatcher Servlet, Handler Mappings, Controller, Model, View and View Resolver along with the appropriate Api are discussed briefly. Finally the article will conclude by presenting a Sample Application.<br /><br />    * Page 1 - Introduction to Spring MVC Web Framework - Web Tier<br />    * Page 2 - Handler Mappings<br />    * Page 3 - Model,View,Controller,View Resolver<br />    * Page - 4 Sample Application<br /><br />2) The Spring Workflow<br /><br />Before taking a look over the various Components that are involved in the Spring MVC Framework, let us have a look on the style of Spring Web Flow.<br /><br />   1. The Client requests for a Resource in the Web Application.<br />   2. The Spring Front Controller, which is implemented as a Servlet, will intercept the Request and then will try to find out the appropriate Handler Mappings.<br />   3. The Handle Mappings is used to map a request from the Client to its Controller object by browsing over the various Controllers defined in the Configuration file.<br />   4. With the help of Handler Adapters, the Dispatcher Servlet will dispatch the Request to the Controller.<br />   5. The Controller processes the Client Request and returns the Model and the View in the form of ModelAndView object back to the Front Controller.<br />   6. The Front Controller then tries to resolve the actual View (which may be Jsp, Velocity or Free marker) by consulting the View Resolver object.<br />   7. Then the selected View is rendered back to the Client.<br /><br />Let us look into the various Core Components that make up the Spring Web Tier. Following are the components covered in the next subsequent sections.<br />3) Dispatcher Servlet<br /><br />The Dispatcher Servlet as represented by org.springframework.web.servlet.DispatcherServlet, follows the Front Controller Design Pattern for handling Client Requests. It means that whatever Url comes from the Client, this Servlet will intercept the Client Request before passing the Request Object to the Controller. The Web Configuration file should be given definition in such a way that this Dispatcher Servlet should be invoked for Client Requests.<br /><br />Following is the definition given in the web.xml to invoke Spring's Dispatcher Servlet.<br /><br />web.xml<br /><br /><br />&lt;?xml version="1.0" encoding="UTF-8"?><br />    &lt;web-app version="2.4"><br /><br />    &lt;servlet><br />        &lt;servlet-name>dispatcher&lt;/servlet-name><br />        &lt;servlet-class><br />            org.springframework.web.servlet.DispatcherServlet<br />        &lt;/servlet-class><br />        &lt;load-on-startup>2&lt;/load-on-startup><br />    &lt;/servlet><br /><br />    &lt;servlet-mapping><br />        &lt;servlet-name>dispatcher&lt;/servlet-name><br />        &lt;url-pattern>*.*&lt;/url-pattern><br />    &lt;/servlet-mapping><br /><br />&lt;/web-app><br />				<br /><br />Look into the definition of servlet-mapping tag. It tells that whatever be the Client Request (represented by *.* meaning any Url with any extension), invoke the Servlet by name 'dispatcher'. In our case, the 'dispatcher' servlet is nothing but an instance of type 'org.springframework.web.servlet.DispatcherServlet'.<br /><br />Closing associated term with the Dispatcher Servlet is the Application Context. An Application Context usually represents a set of Configuration Files that are used to provide Configuration Information to the Application. The Application Context is a Xml file that contain various Bean Definitions. By default the Dispatcher Servlet will try to look for a file by name &lt;servlet-name>-servlet.xml in the WEB-INF directory. So, in our case the Servlet will look for a file name called dispatcher-servlet.xml file in the WEB-INF directory.<br /><br />It is wise sometimes to split all the Configuration information across multiple Configuration Files. In such a case we have to depend on a Listener Servlet called Context Loader represented by org.springframework.web.context.ContextLoaderListener.<br /><br />				<br />&lt;web-app><br /><br />    &lt;listener><br />        &lt;listener-class><br />            org.springframework.web.context.ContextLoaderListener<br />        &lt;/listener-class><br />    &lt;/listener><br /><br />&lt;/web-app><br />				<br /><br />By default, this Context Listener will try to look for the Configuration File by name 'applicationContext.xml' in the '/WEB-INF' directory. But with the help of the parameter 'contextConfigLocation' the default location can be overridden. Even multiple Configuration Files each containing separate piece of Information is also possible.<br /><br />web.xml<br /><br />				<br />&lt;?xml version="1.0" encoding="UTF-8"?><br />&lt;web-app version="2.4"><br /><br />    &lt;listener><br />        &lt;listener-class><br />            org.springframework.web.context.ContextLoaderListener<br />        &lt;/listener-class><br />    &lt;/listener><br /><br />    &lt;context-param><br />        &lt;param-name>contextConfigLocation&lt;/param-name><br />        &lt;param-value>/WEB-INF/contacts.xml, /WEB-INF/resources.xml&lt;/param-value><br />    &lt;/context-param><br /><br />&lt;/web-app><br />				<br /><br />The above definition instructs the Framework to look and load for the Configuration Files by name 'contacts.xml' and 'resources.xml' in the WEB-INF directory.
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/187598#comments" style="color:red;">已有 <strong>1</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 28 Apr 2008 16:15:38 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/187598</link>
        <guid>http://coreoak.javaeye.com/blog/187598</guid>
      </item>
      <item>
        <title>PHP程序设计翻译1--简单的变量和操作符</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/176575" style="color:red;">http://coreoak.javaeye.com/blog/176575</a>&nbsp;
          发表时间: 2008年03月27日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <pre name="code" class="java"></pre> 一旦你安装和运行你自己的PHP编程，你会发现你编写的大多数的脚本是变数，操作员并且你经常用的就是10个函数。 <br />在这一章中我们探讨不同类型的变量在PHP中如何使用，以及他们之间的如何转换，我们也将讨论操作符，这些操作符有对变量的加、减、乘等的功能。 <br />这一章将告诉你PHP的变量是如何工作的，你不需要了解相当复杂的部分，比如references 以及可变变量，除非你想真正理解PHP。 <br />本章的主要内容包括： <br /><br />(1)PHP中你可以用到什么样的数据 <br />(2)References，类型转换，可变变量 <br />(3)脚本变量，预定义变量，脚本常量，预定义的常量 <br />(4)操作符，比如说加、减、乘、除 <br />2.1：数据类型 <br />2.2检查一个变量是否被设置 <br />2.3自动类型转换 <br />2.4强制类型转化 <br />2.5非十进制系统 <br />2.6变量作用域 <br />2.7可变变量 <br />2.8The superglobal arrays <br />2.9 预定义变量 <br />2.10 References <br />2.11 常量 <br />2.11.1 预定义常量 <br />2.12 操作符 <br />2.12.1. Shorthand unary operators <br />2.12.2. 比较操作符 <br />2.12.3. 完整操作符 <br />2.12.4. 三元操作符 <br />2.12.5. Scope resolution operator <br />2.12.6. Execution operator <br />2.12.7. 运算符优先级 <br /><br />2.1：数据类型 <br />PHP有7种类型的变量，他们是strings, integers, floats, booleans, arrays, objects, and resources.你将要在本书中一直用到他们，因此在这里我们有必要记住他们。 <br />String类型是字符串类型，如"a","abc","Jack and Jill went up the hill to fetch a pail of water",etc, String 可以很短也可以随便长，String类型没有长度的限制。 <br /><br />Integer 整形变量，正数或者负数，比如1,-20,55028932,etc，最小值 <br />-2147483647 最大值是2147483647，如果数字大于2147483647 则将会将数字自动转化成foloat类型。 <br /><br />浮点数（也叫浮点数，双精度数或实数）可以用以下任何语法定义： <br /><pre name="code" class="java">&lt;?php 
$a = 1.234; 
$a = 1.2e3; 
$a = 7E-10; 
?> 
</pre><br /><br />boolean类型 只有 TRUE 或 FALSE。 boolean类型将0看作是false其它的任何数据都是true
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/176575#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 27 Mar 2008 00:11:58 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/176575</link>
        <guid>http://coreoak.javaeye.com/blog/176575</guid>
      </item>
      <item>
        <title>PHP在线书籍整理</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/174733" style="color:red;">http://coreoak.javaeye.com/blog/174733</a>&nbsp;
          发表时间: 2008年03月22日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <a href="http://hudzilla.org/phpwiki/index.php?title=Table_of_Contents" target="_blank">http://hudzilla.org/phpwiki/index.php?title=Table_of_Contents</a>
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/174733#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sat, 22 Mar 2008 03:01:29 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/174733</link>
        <guid>http://coreoak.javaeye.com/blog/174733</guid>
      </item>
      <item>
        <title>HyperSnap基本用法 </title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/174732" style="color:red;">http://coreoak.javaeye.com/blog/174732</a>&nbsp;
          发表时间: 2008年03月22日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          作为抓图软件领域的老牌明星，HyperSnap-DX提供了专业级的影像效果，可让你轻松抓取屏幕，它支持抓取使用DirectX技术的游戏及DVD画面，并采用了新的去背景功能，你可以轻松去除抓取后的图形背景，Hpersnap-Dx还拥有许多抓图特色，是很多朋友喜爱的一款抓图工具。 <br /><br />HyperSnap-DX启动后，除了常用工具按钮外，左边的那些按钮主要是用来作图的，在"捕捉"菜单中提供了它的主要抓取对象和热键，默认的热键分别为：全屏幕——"Ctrl+Shift+F"；窗口或控件(比如工具条/滚动条)——"Ctrl+ Shift+W"；按钮(包括命令按钮和工具栏上的各个按钮)——"Ctrl+Shift+B"；活动窗口——"Ctrl+Shift+A"；不带边框的活动窗口(使抓取的对象不包含边框)——"Ctrl+Shift+C"；选区(由用户指定要抓取的任意方形区域)——"Ctrl+Shift+R"；多区域捕捉——"Ctrl+Shift+M"。 <br /><br />下面介绍几个常用且重要的操作： <br /><br />1.抓取全屏幕：按下热键"Ctrl+Shift+F"，或者用鼠标点击"捕捉"下的"全屏幕"菜单项，之后你会听到类似照相的"咔嚓"声，操作成功。 <br /><br />2.抓取活动窗口：首先使要抓取的窗口成为活动窗口，然后按下热键"Ctrl+ Shift+A"。 <br /><br />3.抓取下拉菜单：当你需要抓取某个窗口中的下拉菜单时，有多种方法可以实现：先让HyperSnap-DX最小化到任务栏上，再点击要抓取的菜单使之展开，然后按下热键"Ctrl+Shist+R"，屏幕上将出现一个十字形光标，移动此光标到起始位置用鼠标左键点击，再移动到菜单右下方再次点击；或者当菜单出现后直接按下窗口抓取热键"Ctrl+Shift+W"，你会看到一个闪动的矩形框，点击左键即可抓取该菜单；如果希望抓取多级子菜单中的某一级，应依次打开该级联菜单，按下热键"Ctrl+Shift+W"，当矩形框闪动时移动到希望抓取的子菜单上点击左键；如希望抓取级联菜单的全部，则要用到其"多区域捕捉"功能，按下热键"Ctrl+Shift+M"，当屏幕上出现闪动矩形框时点击左键增加要抓取的区域，以便让各级菜单都被选中(选中的区域会用黑色覆盖)，然后按下回车键完成抓取。 <br /><br />4.多区域抓图：上面介绍的抓取级联菜单仅仅是多区域抓图的一个简单应用，实际上这个功能相当强大，你可以将它和"Region Capture"(区域抓图)结合使用以完成更复杂的抓取任务。例如：要在资源管理器中同时抓取某个文件(夹)的右键快捷菜单和该文件(夹)的图标，可以这样操作：首先用鼠标右键点击该文件(夹)弹出其快捷菜单，然后按下抓取热键"Ctrl+Shift+M"，点取菜单区域使它被选中，再按下鼠标右键不放手，会马上出现一个子菜单，从中点击"重启区域方式"后放开，此时出现十字形光标，用该光标点击文件图标的左上角和右下角各一次，使文件图标被选中(原来选中的菜单仍处于选中状态)，最后按下回车键完成抓取。 <br /><br />在上述抓取过程中，只要还没有完成抓取，随时可按下ESC键放弃当前操作。 <br /><br />5.抓取对话框中的按钮：如果希望抓取某个对话框中的命令按钮，当对话框出现后，把光标移到要抓取的按钮上，然后按下热键"Ctrl+Shift+B"，便会看到这个按钮被自动"按"了一下，抓取完成。 <br /><br />6.抓取游戏画面：如需要抓取游戏的一连串画面，并且不希望中断游戏并对所抓取的图像进行命名等操作，则要使用HyperSnap-DX的特殊捕捉功能，还要设置让HyperSnap-DX自动保存。 <br /><br />1)首先设置其"特殊捕捉"：在"Capture"(捕捉)菜单下点击"Directx/Glide"(使用特殊捕捉)，在设置窗口中选中其提供的三种捕捉类型之一，如果不能确定到底使用的是哪一种，则最好三个一并选上，其他选项取默认值，最后点击确定。 <br /><br />2)设置自动保存：在"Capture Settings"(捕捉设置)对话框中点击"Quick save"(快速保存)选项，选上"Automatically save each capture to a file"(自动保存捕捉后的文件)；如果不想使用其默认的保存文件名，点击"Change"按钮定位一个保存位置，然后在该按钮左边的文本框中输入文件名的前缀字母，并输入名称的起始/终止数字(如输入前缀为pic，范围值定为1到100，则抓取后会自动生成pic1、pic2......pic100等文件)。 <br /><br />关闭捕捉设置对话框后进入游戏，当出现需要的画面时按下抓取热键"Scroll Lock"或"Print Screen"键(注意不要误认为是全屏幕游戏画面就用"Ctrl+Shift+F")，图像会自动被捕捉并依次保存下来。 <br /><br />退出游戏后，在HyperSnap-DX窗口中会看到抓取到的最后一幅图像，你可以按PageDown和PageUp键来回翻动，逐个查看所有抓取到的画面。 <br /><br />7.抓取VCD/DVD电影画面：能否顺利捕捉VCD/DVD电影画面取决于你所使用的播放器是否支持DirectX(最好使用PowerDVD，不过经笔者验证也可以用"豪杰超级解霸"和"Windows Media Player 8.0"，HyperSnap-DX都可以正常抓取)，其次同样需要设置并启动特殊捕捉功能，然后用VCD/DVD播放软件播放电影，当出现需要捕捉的画面时(注意让电影画面出现在前台)，按下"Scroll Lock"键或"Print Screen "抓取。 <br /><br />如果在抓取时出现"Unsupported Pixel Format"(不支持的像素格式)的提示，这表明HyperSnap-DX无法从DirectX覆盖缓冲区中对图像解码，你可以找找看播放软件中是否有"Hardware Acceleration"(启用硬件加速)或"Optimized Video"(视频优化)等选项，如有则取消它，然后重新抓取图像。如果还不能抓取，建议你换用其他的播放软件。 <br /><br />8.抓取超长网页窗口：如果要抓取超过屏幕的超长网页(即要拖动滚动条才能查看所有内容的网页)，可以使用Hypersnap-Dx的抓取"扩展活动窗口"功能来完成：启动Hypersnap-Dx，切换到网页画面，按下热键"Ctrl+Shift+X"，会提示你输入要扩展的高度和宽度(单位为像素)，其高度和宽度可以大于整个屏幕尺寸，然后按下确定，稍等片刻(等待时间长短取决于输入的高度和宽度)就会将超长网页抓取下来。 <br /><br />注：也可使用Snagit来抓取：在Snagit中选择"Image Capture"，单击"Input→Advanced→Custom Scroll"以启动其卷动抓取功能，切换到网页窗口中，按下抓取热键"Ctrl+Shift+P"，出现类似区域抓图时的手形光标，移动此光标到左上角，然后拖动到垂直滚动条底端的下拉按钮处放开左键，此时光标变成手指向上且旁边出现向下三角形，单击左键即可滚动当前网页直至窗口底部(注意：一定要到滚动条的下拉按钮处单击)。这样就将超长网页抓取成一个图片。 <br /><br />9.隐藏HyperSnap-DX窗口：你可以让HyperSnap-DX在抓图时彻底隐藏，甚至让它看起来好像是Windows的一部分而非单独的程序，也可以设置让它把每一次抓取的内容自动放入剪贴板而不只是放在它自己的窗口中，设置方法如下：单击"捕捉"下的"Capture settings" (捕捉设置)菜单项，在对话框中点击"Capture settings"选项卡，去掉"Restore HyperSnap-DX window to front after capture"(完成捕捉后恢复HyperSnap-DX窗口至最前面)前面的对钩，这样抓图完毕就不会看到HyperSnap-DX的窗口了；再点击"复制和打印"选项卡，勾选上"复制每次抓取的图像到剪贴板"(此项不选则不会自动放入剪贴板，这样便不能直接在其他程序中粘贴)，以上两项组合使用后，用HyperSnap-DX所抓取的图像便自动存入剪贴板；如果选中下面的"将每次捕捉的图像粘贴"并指定一个程序，则以后每次抓取的图像便被自动放入该窗口中——这对经常写软件介绍方面稿子的朋友很有用，比如，你可以指定自动粘贴到Word中，这样就不用来回切换抓图窗口和Word窗口了。 <br /><br />记不住抓取热键又不想打开HyperSnap-DX窗口怎么办？别担心，用右键点击它在任务栏上的图标，弹出的菜单中会给你提示。
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/174732#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sat, 22 Mar 2008 02:59:19 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/174732</link>
        <guid>http://coreoak.javaeye.com/blog/174732</guid>
      </item>
      <item>
        <title>感悟城市的发展</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/170954" style="color:red;">http://coreoak.javaeye.com/blog/170954</a>&nbsp;
          发表时间: 2008年03月13日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          在一个城市生活久了， 就或多或少的感受一点这个城市的一种文化，接触到这个城市的一些人民，同时也了解他们的一些思想，不知不觉中就有这样的感触，每一个经济高速发展的城市，都忽略了一个问题，那就是物质文明和精神文明建设没有一起抓，往往我们只是抓住了物质文明建设但是缺少了精神文明建设，没有做到两手抓两手都要硬。因此在经济高速发展的过程中就会有一段时间是人们心理出现病态的时期，这些病态的心理并没有被人们所认识，往往认识到的人就会被打入冷宫，显得好像跟这个城市格格不入！
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/170954#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 13 Mar 2008 20:48:24 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/170954</link>
        <guid>http://coreoak.javaeye.com/blog/170954</guid>
      </item>
      <item>
        <title>做人的实话</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/141434" style="color:red;">http://coreoak.javaeye.com/blog/141434</a>&nbsp;
          发表时间: 2007年11月17日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <br />
做人的几大实话<br />
1.遇到乞讨者：遇到要钱的就给他(她)点饭，遇到要饭的就给他(她)点钱。<br />
2.上车遇到老弱病残、孕妇：让座的时候别动声色，也别大张旗鼓。站起来用身体挡住其他人留出空位子给需要的人，然后装作下车走远点。人太多实在走不远，人家向你表示谢意的时候微笑一下。<br />
3.雨雪的时候、天冷的傍晚或者是雪天的傍晚，遇到卖菜的、卖水果的、卖报纸的剩的不多了又不能回家，能全买就全买，不能全买就买一份，反正吃什么也是吃，看什么也是看，买下来让人早点回家。<br />
4.遇到迷路的小孩和老头老太太，能送回家送回家，不能送回家的送上车、送到派出所也行，如果有电话的替老人或小孩打个电话就走，反正你也不缺那两个电话费。<br />
5.遇到迷路的人打听某个地址，碰巧你又知道，就主动告诉一声。别不好意思，没有人笑话你。　6.捡到钱包就找找失主，如果你实在缺钱就把现金留下。打电话告诉失主就说你在厕所里捡到的。把信用卡、身份证、驾驶执照还给人家，一般人家也不会在乎钱了。把人家的地址记在你的笔记本上，以后发达了去找人家道个谦，把钱还给人家。<br />
7.遇到学生出来打工的、勤工俭学的，特别是中学生、小姑娘。她卖什么你就买点，他发传单，你就收下，如果她不是家庭困难，出来打工也需要勇气的，鼓励鼓励她吧。<br />
8.遇到夜里摆地摊的，能买就多买一些，别还价，东西都不贵。家境哪怕好一点，谁会大冷天夜里摆地摊。<br />
9.如果钱还宽裕，别养二奶，偷偷养几个贫困山区的学生。别让人家知道你是谁，要不然见面了多尴尬，多不好意思。但是你心里一定会觉得舒坦，比包二奶提心吊胆的要好得多。如果真想包也可以包一个，好事坏事一起做。人吗，本来就复杂。
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/141434#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sat, 17 Nov 2007 23:11:34 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/141434</link>
        <guid>http://coreoak.javaeye.com/blog/141434</guid>
      </item>
      <item>
        <title>HTML学习教程</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/141377" style="color:red;">http://coreoak.javaeye.com/blog/141377</a>&nbsp;
          发表时间: 2007年11月17日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          帮朋友整理的W3C上中文html学习教程，个人认为是比较好的html入门资料， 如果有需要的朋友一起分享一下
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/141377#comments" style="color:red;">已有 <strong>1</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sat, 17 Nov 2007 15:19:32 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/141377</link>
        <guid>http://coreoak.javaeye.com/blog/141377</guid>
      </item>
      <item>
        <title>光贵节快乐！</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/140002" style="color:red;">http://coreoak.javaeye.com/blog/140002</a>&nbsp;
          发表时间: 2007年11月12日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          一年一度的光贵节来临了， 祝愿全世界的光贵都在这一天快乐！
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/140002#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 12 Nov 2007 19:20:20 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/140002</link>
        <guid>http://coreoak.javaeye.com/blog/140002</guid>
      </item>
      <item>
        <title>java垃圾回收机制学习资料</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/91594" style="color:red;">http://coreoak.javaeye.com/blog/91594</a>&nbsp;
          发表时间: 2007年06月18日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script>
1<strong>、谁在做Garbage Collection?</strong>
<p>　　一种流行的说法:在C++里，是系统在做垃圾回收;而在Java里，是Java自身在做。</p>
<p>　 　在C++里，释放内存是手动处理的，要用delete运算符来释放分配的内存。这是流行的说法。确切地说，是应用认为不需要某实体时，就需用 delete告诉系统，可以回收这块空间了。这个要求，对编码者来说，是件很麻烦、很难做到的事。随便上哪个BBS，在C/C++版块里总是有一大堆关于 内存泄漏的话题。</p>
<p>　　Java采用一种不同的，很方便的方法:Garbage Collection.垃圾回收机制放在JVM里。JVM完全负责垃圾回收事宜，应用只在需要时申请空间，而在抛弃对象时不必关心空间回收问题。</p>
<p>　　<strong>二、对象在啥时被丢弃?</strong></p>
<p>　　在C++里，当对象离开其作用域时，该对象即被应用抛弃。</p>
<p>　　是对象的生命期不再与其作用域有关，而仅仅与引用有关。</p>
<p>　　Java的垃圾回收机制一般包含近十种算法。对这些算法中的多数，我们不必予以关心。只有其中最简单的一个:引用计数法，与编码有关。</p>
<p>　　一个对象，可以有一个或多个引用变量指向它。当一个对象不再有任何一个引用变量指向它时，这个对象就被应用抛弃了。或者说，这个对象可以被垃圾回收机制回收了。</p>
<p>　　这就是说，当不存在对某对象的任何引用时，就意味着，应用告诉JVM:我不要这个对象，你可以回收了。</p>
<p>　　JVM的垃圾回收机制对堆空间做实时检测。当发现某对象的引用计数为0时，就将该对象列入待回收列表中。但是，并不是马上予以销毁。</p>
<p>　　<strong>三、丢弃就被回收?</strong></p>
<p>　　该对象被认定为没有存在的必要了，那么它所占用的内存就可以被释放。被回收的内存可以用于后续的再分配。</p>
<p>　　但是，并不是对象被抛弃后当即被回收的。JVM进程做空间回收有较大的系统开销。如果每当某应用进程丢弃一个对象，就立即回收它的空间，势必会使整个系统的运转效率非常低下。</p>
<p>　　前面说过，JVM的垃圾回收机制有多个算法。除了引用计数法是用来判断对象是否已被抛弃外，其它算法是用来确定何时及如何做回收。JVM的垃圾回收机制要在时间和空间之间做个平衡。</p>
<p>　　因此，为了提高系统效率，垃圾回收器通常只在满足两个条件时才运行:即有对象要回收且系统需要回收。切记垃圾回收要占用时间，因此，Java运行时系统只在需要的时候才使用它。因此你无法知道垃圾回收发生的精确时间。</p>
<p>　　<strong>四、没有引用变量指向的对象有用吗?</strong></p>
<p>　　前面说了，没挂上引用变量的对象是被应用丢弃的，这意味着，它在堆空间里是个垃圾，随时可能被JVM回收。</p>
<p>　　不过，这里有个不是例外的例外。对于一次性使用的对象(有些书称之为临时对象)，可以不用引用变量指向它。举个最简单也最常见的例子:</p>
<p>　　System.out.println(&ldquo;I am Java!&rdquo;);</p>
<p>　　就是创建了一个字符串对象后，直接传递给println()方法。</p>
<p>　　<strong>五、应用能干预垃圾回收吗?</strong></p>
<p>　 　许多人对Java的垃圾回收不放心，希望在应用代码里控制JVM的垃圾回收运作。这是不可能的事。对垃圾回收机制来说，应用只有两个途径发消息给 JVM.第一个前面已经说了，就是将指向某对象的所有引用变量全部移走。这就相当于向JVM发了一个消息:这个对象不要了。第二个是调用库方法 System.gc()，多数书里说调用它让Java做垃圾回收。</p>
<p>　　第一个是一个告知，而调用System.gc()也仅仅是一个请求。JVM接受这个消息后，并不是立即做垃圾回收，而只是对几个垃圾回收算法做了加权，使垃圾回收操作容易发生，或提早发生，或回收较多而已。</p>
<p>　　希望JVM及时回收垃圾，是一种需求。其实，还有相反的一种需要:在某段时间内最好不要回收垃圾。要求运行速度最快的实时系统，特别是嵌入式系统，往往希望如此。</p>
<p>　　Java的垃圾回收机制是为所有Java应用进程服务的，而不是为某个特定的进程服务的。因此，任何一个进程都不能命令垃圾回收机制做什么、怎么做或做多少。</p>
<p>　　<strong>六、对象被回收时要做的事</strong></p>
<p>　　一个对象在运行时，可能会有一些东西与其关连。因此，当对象即将被销毁时，有时需要做一些善后工作。可以把这些操作写在finalize()方法(常称之为终止器)里。</p>
<p>　　protected void finalize()</p>
<p>　　{</p>
<p>　　// finalization code here</p>
<p>　　}</p>
<p>　 　这个终止器的用途类似于C++里的析构函数，而且都是自动调用的。但是，两者的调用时机不一样，使两者的表现行为有重大区别。C++的析构函数总是当对 象离开作用域时被调用。这就是说，C++析构函数的调用时机是确定的，且是可被应用判知的。但是，Java终止器却是在对象被销毁时。由上所知，被丢弃的 对象何时被销毁，应用是无法获知的。而且，对于大多数场合，被丢弃对象在应用终止后仍未销毁。</p>
<p>　　在编码时，考虑到这一点。譬如，某对象在 运作时打开了某个文件，在对象被丢弃时不关闭它，而是把文件关闭语句写在终止器里。这样做对文件操作会造成问题。如果文件是独占打开的，则其它对象将无法 访问这个文件。如果文件是共享打开的，则另一访问该文件的对象直至应用终结仍不能读到被丢弃对象写入该文件的新内容。</p>
<p>　　至少对于文件操作，编码者应认清Java终止器与C++析构函数之间的差异。</p>
<p>　　那么，当应用终止，会不会执行应用中的所有finalize()呢?据Bruce Eckel在Thinking in Java里的观点:&ldquo;到程序结束的时候，并非所有收尾模块都会得到调用&rdquo;。这还仅仅是指应用正常终止的场合，非正常终止呢?</p>
<p>　　因此，哪些收尾操作可以放在finalize()里，是需要酌酎的。</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://coreoak.javaeye.com/blog/91594#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 18 Jun 2007 14:21:32 +0800</pubDate>
        <link>http://coreoak.javaeye.com/blog/91594</link>
        <guid>http://coreoak.javaeye.com/blog/91594</guid>
      </item>
      <item>
        <title>java加密技术收集</title>
        <author>coreoak</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://coreoak.javaeye.com">coreoak</a>&nbsp;
          链接：<a href="http://coreoak.javaeye.com/blog/91576" style="color:red;">http://coreoak.javaeye.com/blog/91576</a>&nbsp;
          发表时间: 2007年06月18日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>在chinaitlab上面找到关于java加密技术的文章， 摘抄下来， 日后好好学习，链接地址：<font face="Arial"><a href="http://java.chinaitlab.com/base/719984_2.html">http://java.chinaitlab.com/base/719984_2.html</a></font></p>
<p>&nbsp;最近在研究JAVA CLASS LOADING技术，已实现了一个自定义的加载器。对目前自定义加载器的应用，还在探讨中。下面是自定义的CLASSLOADER在JAVA加密解密方面的一些研究。<br />
<br />
<strong>JAVA安全<br />
</strong><br />
&nbsp;&nbsp;&nbsp; JAVA是解释执行的语言，对于不同的操作平台都有相应的JVM对字节码文件进行解释执行。而这个字节码文件，也就是我们平时所看到的每一个.class文件。<br />
<br />
&nbsp;&nbsp;&nbsp; 这是我们大家都知道的常识，也就是由.java文件，经过编译器编译，变成JVM所能解释的.class文件。<br />
<br />
&nbsp;&nbsp;&nbsp; 而这个过程，在现在公开的网络技术中，利用一个反编译器，任何人都可以很容易的获取它的源文件。这对于很多人来说是不希望看到的。<br />
<br />
&nbsp;&nbsp;&nbsp; 对于加密解密技术，我懂的不多，有些可以利用某种技术&ldquo;模糊&rdquo;JAVA类文件。这样能够使反编译的难度增加。但估计反编译器的技术水平也在不断提升，导致这种方法层层受阻。另外还有很多其他的技术也可以实现对JAVA文件的加密解密。我现在所想要研究的，就是其中的一种。<br />
<br />
&nbsp;&nbsp;&nbsp; JAVA的灵活性使反编译变得容易，同时，也让我们的加密解密的方法变得灵活。<br />
<br />
<strong>利用自定义的CLASSLOADER<br />
</strong><br />
参照：http://www.blogjava.net/realsmy/archive/2007/04/18/111582.html<br />
<br />
JAVA中的每一个类都是通过类加载器加载到内存中的。对于类加载器的工作流程如下表示：<br />
1.searchfile() <br />
找到我所要加载的类文件。（抛除JAR包的概念，现在只是要加载一个.class文件）<br />
2.loadDataClass()<br />
读取这个类文件的字节码。<br />
3.difineClass()<br />
加载类文件。（加载的过程其实很复杂，我们现在先不研究它。）<br />
<br />
&nbsp;&nbsp;&nbsp; 从这个过程中我们能很清楚的发现，自定义的类加载能够很轻松的控制每个类文件的加载过程。这样在第二步（loadDataClass）和第三步（difineClass）之间，我们将会有自己的空间灵活的控制这个过程。<br />
<br />
我们加密解密的技术就应用到这里。<br />
<br />
<strong>加密解密</strong><br />
<br />
&nbsp;&nbsp;&nbsp; JAVA加密解密的技术有很多。JAVA自己提供了良好的类库对各种算法进行支持。对于采用哪种算法，网络上说法不一，自己去GOOGLE一下吧。<br />
<br />
下面用DES对称加密算法（设定一个密钥，然后对所有的数据进行加密）来简单举个例子。<br />
<br />
首先，生成一个密钥KEY。<br />
我把它保存到key.txt中。这个文件就象是一把钥匙。谁拥有它，谁就能解开我们的类文件。代码参考如下：<br />
</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><span style="COLOR: #0000ff">package</span><span style="COLOR: #000000">&nbsp;com.neusoft.jiami;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.File;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.FileOutputStream;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.security.SecureRandom;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.KeyGenerator;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.SecretKey;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911592.gif" id="Codehighlighter1_193_881_Open_Image" onclick="this.style.display='none'; Codehighlighter1_193_881_Open_Text.style.display='none'; Codehighlighter1_193_881_Closed_Image.style.display='inline'; Codehighlighter1_193_881_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911626.gif" id="Codehighlighter1_193_881_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_193_881_Closed_Text.style.display='none'; Codehighlighter1_193_881_Open_Image.style.display='inline'; Codehighlighter1_193_881_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" /></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Key&nbsp;</span><span id="Codehighlighter1_193_881_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911799.gif" alt="" /></span><span id="Codehighlighter1_193_881_Open_Text"><span style="COLOR: #000000">{<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">&nbsp;String&nbsp;keyName;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911220.gif" id="Codehighlighter1_250_279_Open_Image" onclick="this.style.display='none'; Codehighlighter1_250_279_Open_Text.style.display='none'; Codehighlighter1_250_279_Closed_Image.style.display='inline'; Codehighlighter1_250_279_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911814.gif" id="Codehighlighter1_250_279_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_250_279_Closed_Text.style.display='none'; Codehighlighter1_250_279_Open_Image.style.display='inline'; Codehighlighter1_250_279_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;Key(String&nbsp;keyName)&nbsp;</span><span id="Codehighlighter1_250_279_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911799.gif" alt="" /></span><span id="Codehighlighter1_250_279_Open_Text"><span style="COLOR: #000000">{<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.keyName&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;keyName;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911240.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911220.gif" id="Codehighlighter1_338_747_Open_Image" onclick="this.style.display='none'; Codehighlighter1_338_747_Open_Text.style.display='none'; Codehighlighter1_338_747_Closed_Image.style.display='inline'; Codehighlighter1_338_747_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911814.gif" id="Codehighlighter1_338_747_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_338_747_Closed_Text.style.display='none'; Codehighlighter1_338_747_Open_Image.style.display='inline'; Codehighlighter1_338_747_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;createKey(String&nbsp;keyName)&nbsp;</span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000">&nbsp;Exception&nbsp;</span><span id="Codehighlighter1_338_747_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911799.gif" alt="" /></span><span id="Codehighlighter1_338_747_Open_Text"><span style="COLOR: #000000">{<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;创建一个可信任的随机数源，DES算法需要</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SecureRandom&nbsp;sr&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;SecureRandom();<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;用DES算法创建一个KeyGenerator对象</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;KeyGenerator&nbsp;kg&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;KeyGenerator.getInstance(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">DES</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;初始化此密钥生成器,使其具有确定的密钥长度</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kg.init(sr);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;生成密匙</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SecretKey&nbsp;key&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;kg.generateKey();<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;获取密钥数据</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">byte</span><span style="COLOR: #000000">&nbsp;rawKeyData[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;key.getEncoded();<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;将获取到密钥数据保存到文件中，待解密时使用</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileOutputStream&nbsp;fo&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;FileOutputStream(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;File(keyName));<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fo.write(rawKeyData);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911240.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911220.gif" id="Codehighlighter1_790_879_Open_Image" onclick="this.style.display='none'; Codehighlighter1_790_879_Open_Text.style.display='none'; Codehighlighter1_790_879_Closed_Image.style.display='inline'; Codehighlighter1_790_879_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911814.gif" id="Codehighlighter1_790_879_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_790_879_Closed_Text.style.display='none'; Codehighlighter1_790_879_Open_Image.style.display='inline'; Codehighlighter1_790_879_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String&nbsp;args[])&nbsp;</span><span id="Codehighlighter1_790_879_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911799.gif" alt="" /></span><span id="Codehighlighter1_790_879_Open_Text"><span style="COLOR: #000000">{<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911220.gif" id="Codehighlighter1_798_825_Open_Image" onclick="this.style.display='none'; Codehighlighter1_798_825_Open_Text.style.display='none'; Codehighlighter1_798_825_Closed_Image.style.display='inline'; Codehighlighter1_798_825_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911814.gif" id="Codehighlighter1_798_825_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_798_825_Closed_Text.style.display='none'; Codehighlighter1_798_825_Open_Image.style.display='inline'; Codehighlighter1_798_825_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">&nbsp;</span><span id="Codehighlighter1_798_825_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911799.gif" alt="" /></span><span id="Codehighlighter1_798_825_Open_Text"><span style="COLOR: #000000">{<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Key(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">key.txt</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911220.gif" id="Codehighlighter1_847_875_Open_Image" onclick="this.style.display='none'; Codehighlighter1_847_875_Open_Text.style.display='none'; Codehighlighter1_847_875_Closed_Image.style.display='inline'; Codehighlighter1_847_875_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911814.gif" id="Codehighlighter1_847_875_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_847_875_Closed_Text.style.display='none'; Codehighlighter1_847_875_Open_Image.style.display='inline'; Codehighlighter1_847_875_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000">&nbsp;(Exception&nbsp;e)&nbsp;</span><span id="Codehighlighter1_847_875_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911799.gif" alt="" /></span><span id="Codehighlighter1_847_875_Open_Text"><span style="COLOR: #000000">{<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911240.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911240.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911397.gif" align="top" alt="" />}</span></span></div>
<p>&nbsp;</p>
<p>第二步，对我们所要进行加密的类文件进行加密。</p>
<p>&nbsp;&nbsp;&nbsp; 比如我有一个DigestPass类，已经被正常编译好生成DigestPass.class文件。此时，这个类文件是任何人都可以用的。因为系统的类加载器可以自动的加载它。那么下一步，我们要做的就是把这个类文件加密。使系统的类加载器无法读取到正确的字节码文件。参考代码如下：<br clear="all" />
</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><span style="COLOR: #0000ff">package</span><span style="COLOR: #000000">&nbsp;com.neusoft.jiami;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.File;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.FileInputStream;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.FileOutputStream;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.security.SecureRandom;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.Cipher;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.SecretKey;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.SecretKeyFactory;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.spec.DESKeySpec;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911592.gif" id="Codehighlighter1_304_1289_Open_Image" onclick="this.style.display='none'; Codehighlighter1_304_1289_Open_Text.style.display='none'; Codehighlighter1_304_1289_Closed_Image.style.display='inline'; Codehighlighter1_304_1289_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911626.gif" id="Codehighlighter1_304_1289_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_304_1289_Closed_Text.style.display='none'; Codehighlighter1_304_1289_Open_Image.style.display='inline'; Codehighlighter1_304_1289_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;JiaMi&nbsp;</span><span id="Codehighlighter1_304_1289_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911799.gif" alt="" /></span><span id="Codehighlighter1_304_1289_Open_Text"><span style="COLOR: #000000">{<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911220.gif" id="Codehighlighter1_364_1287_Open_Image" onclick="this.style.display='none'; Codehighlighter1_364_1287_Open_Text.style.display='none'; Codehighlighter1_364_1287_Closed_Image.style.display='inline'; Codehighlighter1_364_1287_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911814.gif" id="Codehighlighter1_364_1287_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_364_1287_Closed_Text.style.display='none'; Codehighlighter1_364_1287_Open_Image.style.display='inline'; Codehighlighter1_364_1287_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000">&nbsp;Exception&nbsp;</span><span id="Codehighlighter1_364_1287_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911799.gif" alt="" /></span><span id="Codehighlighter1_364_1287_Open_Text"><span style="COLOR: #000000">{<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;DES算法要求有一个可信任的随机数源</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SecureRandom&nbsp;sr&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;SecureRandom();<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;获得密匙数据</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInputStream&nbsp;fi&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;FileInputStream(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;File(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">key.txt</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">));<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">byte</span><span style="COLOR: #000000">&nbsp;rawKeyData[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">byte</span><span style="COLOR: #000000">[fi.available()];<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi.read(rawKeyData);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi.close();<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;从原始密匙数据创建DESKeySpec对象</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DESKeySpec&nbsp;dks&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;DESKeySpec(rawKeyData);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;创建一个密匙工厂，然后用它把DESKeySpec转换成一个SecretKey对象</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SecretKey&nbsp;key&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;SecretKeyFactory.getInstance(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">DES</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">).generateSecret(dks);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;Cipher对象实际完成加密操作</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cipher&nbsp;cipher&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;Cipher.getInstance(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">DES</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;用密匙初始化Cipher对象</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cipher.init(Cipher.ENCRYPT_MODE,&nbsp;key,&nbsp;sr);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;现在，获取要加密的文件数据</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInputStream&nbsp;fi2&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;FileInputStream(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;File(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">DigestPass.class</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">));<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">byte</span><span style="COLOR: #000000">&nbsp;data[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">byte</span><span style="COLOR: #000000">[fi2.available()];<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi2.read(data);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi2.close();<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;正式执行加密操作</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">byte</span><span style="COLOR: #000000">&nbsp;encryptedData[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;cipher.doFinal(data);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;用加密后的数据覆盖原文件</span><span style="COLOR: #008000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileOutputStream&nbsp;fo&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;FileOutputStream(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;File(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">DigestPass.class</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">));<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fo.write(encryptedData);<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fo.close();<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911240.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911397.gif" align="top" alt="" />}</span></span><span style="COLOR: #000000"><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span></div>
<p><br />
第三步，用自定义的CLASSLOADER进行加载。参考代码如下：<br />
</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><span style="COLOR: #0000ff">package</span><span style="COLOR: #000000">&nbsp;com.neusoft.jiami;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.File;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.FileInputStream;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.io.FileOutputStream;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;java.security.SecureRandom;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.Cipher;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.SecretKey;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.SecretKeyFactory;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;javax.crypto.spec.DESKeySpec;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000">&nbsp;com.neusoft.classloader.MyClassLoader;<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911361.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911592.gif" id="Codehighlighter1_350_1557_Open_Image" onclick="this.style.display='none'; Codehighlighter1_350_1557_Open_Text.style.display='none'; Codehighlighter1_350_1557_Closed_Image.style.display='inline'; Codehighlighter1_350_1557_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911626.gif" id="Codehighlighter1_350_1557_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_350_1557_Closed_Text.style.display='none'; Codehighlighter1_350_1557_Open_Image.style.display='inline'; Codehighlighter1_350_1557_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;JieMi&nbsp;</span><span id="Codehighlighter1_350_1557_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911799.gif" alt="" /></span><span id="Codehighlighter1_350_1557_Open_Text"><span style="COLOR: #000000">{<br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911518.gif" align="top" alt="" /><br />
<img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911220.gif" id="Codehighlighter1_410_1555_Open_Image" onclick="this.style.display='none'; Codehighlighter1_410_1555_Open_Text.style.display='none'; Codehighlighter1_410_1555_Closed_Image.style.display='inline'; Codehighlighter1_410_1555_Closed_Text.style.display='inline';" align="top" alt="" /><img src="http://java.chinaitlab.com/UploadFiles_8734/200704/20070425095911814.gif" id="Codehighlighter1_410_1555_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_410_1555_Closed_Text.style.display='none'; Codehighlighter1_410_1555_Open_Image.style.display='inline'; Codehighlighter1_410_1555_Open_Text.style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000">&nbsp;Exception&nbsp;</span><span id="Codehighlighter1_410_1555_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px so