Thursday, June 29, 2017

Proxy Pattern

@reference_1_tutorialspoint
Design Patterns - Proxy Pattern

There are many different flavours of Proxy, depending on it's purpose. You may have a protection proxy, to control access rights to an object. A virtual proxy handles the case where an object might be expensive to create, and a remote proxy controls access to a remote object.

This pattern is recommended when either of the following scenarios occur in your application:
  • The object being represented is external to the system.
  • Objects need to be created on demand. 
  • Access control for the original object is required
  • Added functionality is required when an object is accessed.
You'll have noticed that this is very similar to the Adapter pattern. However, the main difference between bot is that the adapter will expose a different interface to allow interoperability. The Proxy exposes the same interface, but gets in the way to save processing time or memory.

@reference_2_dzone.com
Proxy Pattern Tutorial with Java Examples

If you've read my "Decorate Your Java Code" (JavaWorld, December 2001), you may see similarities between the Decorator and Proxy design patterns. Both patterns use a proxy that forwards method calls to another object, known as the real subject. The difference is that, with the Proxy pattern, the relationship between a proxy and the real subject is typically set at compile time, whereas decorators can be recursively constructed at runtime.

@reference_3_javaworld
Java Design Patterns

Wednesday, June 28, 2017

JAVA_HOME、PATH、CLASSPATH

JAVA_HOME
指的是你JDK安装的位置,一般默认安装在C盘,如 C:\Program Files\Java\jdk1.8.0_91

PATH
将程序路径包含在PATH当中后,在命令行窗口就可以直接键入它的名字了,而不再需要键入它的全路径,比如上面代码中我用的到javac和java两个命令。
一般的 PATH=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%PATH%;
也就是在原来的PATH路径上添加JDK目录下的bin目录和jre目录的bin.

CLASSPATH CLASSPATH=.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
一看就是指向jar包路径。
需要注意的是前面的 . 代表当前目录。

@reference_1_csdn

JAVA_HOME= C:\Program Files\Java\jdk1.6.0_10
Path= %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
CLASSPATH= .;%JAVA_HOME%\lib;

@reference_2_zhidao.baidu

The bootstrap class loader loads the core Java libraries located in the <JAVA_HOME>/jre/lib directory. This class loader, which is part of the core JVM, is written in native code.
The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext, or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.
The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.

@reference_3_wikipedia

Tuesday, June 27, 2017

Java ClassLoader

@reference_1_stackoverflow
What is a Java ClassLoader?
@reference_2_oracle
Understanding Network Class Loaders
@reference_3_docs.oracle
Class ClassLoader

The bootstrap class loader loads the core Java libraries located in the <JAVA_HOME>/jre/lib directory. This class loader, which is part of the core JVM, is written in native code.
The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext, or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.
The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.

@reference_4_wikipedia

Every class loaded in a Java application is identified by its fully qualified name (package name + class name), and the ClassLoader instance that loaded it. That means, that a class MyObject loaded by class loader A, is not the same class as the MyObject class loaded with class loader B.
MyObject object = (MyObject)
    myClassReloadingFactory.newInstance("com.jenkov.MyObject");
 
Notice how the MyObject class is referenced in the code, as the type of the object variable. This causes the MyObject class to be loaded by the same class loader that loaded the class this code is residing in.
If the myClassReloadingFactory object factory reloads the MyObject class using a different class loader than the class the above code resides in, you cannot cast the instance of the reloaded MyObject class to the MyObject type of the object variable. Since the two MyObject classes were loaded with different class loaders, the are regarded as different classes, even if they have the same fully qualified class name. Trying to cast an object of the one class to a reference of the other will result in a ClassCastException.
It is possible to work around this limitation but you will have to change your code in either of two ways:
  1. Use an interface as the variable type, and just reload the implementing class.
  2. Use a superclass as the variable type, and just reload a subclass.
Here are two coresponding code examples:
MyObjectInterface object = (MyObjectInterface)
    myClassReloadingFactory.newInstance("com.jenkov.MyObject");
MyObjectSuperclass object = (MyObjectSuperclass)
    myClassReloadingFactory.newInstance("com.jenkov.MyObject");
 
@reference_5_tutorials.jenkov.com
Java Reflection - Dynamic Class Loading and Reloading

Monday, June 19, 2017

Youdao Dictionary API

http://dict.youdao.com/jsonapi?q=nice&keyfrom=deskdict.mini&dogVersion=1.0&dogui%20=%20json&client=deskdict&id=b1915e13d589286bf&vendor=unknown&in=YoudaoDictSetup&appVer=7.2.0.0511&appZengqiang=0&abTest=&le=en&dicts=%7B%22count%22%3A4%2C%22dicts%22%3A%5B%5B%22ec%22%2C%22ce%22%2C%22cj%22%2C%22jc%22%2C%22ck%22%2C%22kc%22%2C%22cf%22%2C%22fc%22%20%2C%20%22multle%22%20%5D%2C%5B%22web_trans%22%5D%2C%5B%22fanyi%22%5D%2C%5B%22typos%22%5D%5D%7D&LTH=47

Decode:
http://dict.youdao.com/jsonapi?q=nice&keyfrom=deskdict.mini&dogVersion=1.0&dogui = json&client=deskdict&id=b1915e13d589286bf&vendor=unknown&in=YoudaoDictSetup&appVer=7.2.0.0511&appZengqiang=0&abTest=&le=en&dicts={"count":4,"dicts":[["ec","ce","cj","jc","ck","kc","cf","fc" , "multle" ],["web_trans"],["fanyi"],["typos"]]}&LTH=47

http://dict.youdao.com/jsonapi?q=good&keyfrom=deskdict.main&dogVersion=1.0&dogui=json&client=deskdict&id=b1915e13d589286bf&vendor=unknown&in=YoudaoDictSetup&appVer=7.2.0.0511&appZengqiang=0&abTest=&le=en&dicts=%7B%22count%22%3A12%2C%22dicts%22%3A%5B%5B%22newjc%22%5D%2C%5B%22newcj%22%5D%2C%5B%22auth_sents_part%22%5D%2C%20%5B%22longman%22%5D%20%2C%20%5B%22collins%22%5D%20%2C%20%5B%22ec21%22%5D%2C%5B%22hh%22%5D%2C%5B%22ee%22%5D%2C%5B%22media_sents_part%22%5D%2C%5B%22rel_word%22%5D%2C%5B%22special%22%5D%2C%5B%22syno%22%5D%5D%7D&LTH=1828

Decode:
http://dict.youdao.com/jsonapi?q=good&keyfrom=deskdict.main&dogVersion=1.0&dogui=json&client=deskdict&id=b1915e13d589286bf&vendor=unknown&in=YoudaoDictSetup&appVer=7.2.0.0511&appZengqiang=0&abTest=&le=en&dicts={"count":12,"dicts":[["newjc"],["newcj"],["auth_sents_part"], ["longman"] , ["collins"] , ["ec21"],["hh"],["ee"],["media_sents_part"],["rel_word"],["special"],["syno"]]}&LTH=1828

http://dict.youdao.com/jsonapi?q=good&le=en&dicts={%22count%22:12,%22dicts%22:[[%22newjc%22],[%22newcj%22],[%22auth_sents_part%22],%20[%22longman%22]%20,%20[%22collins%22]%20,%20[%22ec21%22],[%22hh%22],[%22ee%22],[%22media_sents_part%22],[%22rel_word%22],[%22special%22],[%22syno%22]]}

Decode:
http://dict.youdao.com/jsonapi?q=good&le=en&dicts={"count":12,"dicts":[["newjc"],["newcj"],["auth_sents_part"], ["longman"] , ["collins"] , ["ec21"],["hh"],["ee"],["media_sents_part"],["rel_word"],["special"],["syno"]]}

http://dict.youdao.com/jsonapi?q=good&le=en&dicts={"count":1,"dicts":[["collins"]]}

@url encode/decode

Friday, June 16, 2017

Arrays are objects in Java

In the Java programming language, arrays are objects, are dynamically created, and may be assigned to variables of type Object. All methods of class Object may be invoked on an array.

Every array has an associated Class object, shared with all other arrays with the same component type. [This] acts as if: the direct superclass of an array type is Object [and] every array type implements the interfaces Cloneable and java.io.Serializable.

@reference_1_zhihu.com
JAVA中的数组是对象吗?

Thursday, June 15, 2017

Servlet Tutorials

@reference_1_jenkov.com
Java Servlets
@reference_2_tutorialspoint.com
Servlets Tutorial
@reference_3_ntu.edu.sg
Java Server-Side Programming
Java Servlets

Initializers in Java

An initializer (sometimes called an initializer block) is a lonely block of code in Java that’s placed outside any method, constructor, or other block of code. Initializers are executed whenever an instance of a class is created, regardless of which constructor is used to create the instance.
Initializer blocks are similar to variable initializers used to initialize variables. The difference is that with an initializer block, you can code more than one statement. Here’s a class that gets the value for a class field from the user when the class is initialized:
class PrimeClass
{
 private Scanner sc = new Scanner(System.in);
 public int x;
 {
  System.out.print(
   "Enter the starting value for x: ");
  x = sc.nextInt();
 }
}
You can almost always achieve the same effect by using other coding techniques, which usually are more direct. You could prompt the user for the value in the constructor, for example, or you could call a method in the field initializer, like this:
class PrimeClass
{
 private Scanner sc = new Scanner(System.in);
 public int x = getX();
 private int getX()
 {
  System.out.print("Enter the starting value "
  + "for x: ");
  return sc.nextInt();
 }
}
Either way, the effect is the same.

Here are a few other tidbits of information concerning initializers:
  • If a class contains more than one initializer, the initializers are executed in the order in which they appear in the program.
  • Initializers are executed before any class constructors.
  • A special kind of initializer block called a static initializer lets you initialize static fields.
  • Initializers are sometimes used with anonymous classes.
@reference_1_dummies.com
How to Use Initializers in Java
@reference_2_stackoverflow 
Use of Initializers vs Constructors in Java
@reference_3_javaworld.com
Java 101: Class and object initialization in Java

Class Initializers

The fifth line (or block of lines) is a class initializer block. It begins with a { and ends with a }. Inside this block you can put initialization code that is to be executed a instance of the class is created. In this example the block is empty. The text inside the block is just a comment. The Java compiler ignores it.
{
    //class initializer
}
Class initializers can also be static. Then they are executed already when the class is loaded, and only once because the class is only loaded in the Java Virtual Machine once. Here is an example of a static initializer block:
static {
    //static class initializer
}
Notice the keyword static before the block. This makes the class initializer block static.

@reference_4_jenkov.com
Java Syntax

Thursday, June 8, 2017

GET vs. POST

@reference_1_diffen.com
GET vs. POST
@reference_2_javatpoint.com
Get vs. Post
@reference_3_stackoverflow
What is the difference between POST and GET?
@reference_4_w3schools
HTTP Methods: GET vs. POST

@refernce_5_stackoverflow
What does enctype='multipart/form-data' mean?

Content-Type & MIME types


Content Type is also known as MIME (Multipurpose internet Mail Extension) Type. It is a HTTP header that provides the description about what are you sending to the browser.
MIME is an internet standard that is used for extending the limited capabilities of email by allowing the insertion of sounds, images and text in a message.
The features provided by MIME to the email services are as given below:
  • It supports the non-ASCII characters
  • It supports the multiple attachments in a single message
  • It supports the attachment which contains executable audio, images and video files etc.
  • It supports the unlimited message length.
@reference_1_javatpoint
Content Type
@reference_2_developer.mozilla.org
Content-Type
@reference_3_developer.mozilla.org
MIME types

What is a MIME type?

MIME stands for "Multipurpose Internet Mail Extensions. It's a way of identifying files on the Internet according to their nature and format. For example, using the "Content-type" header value defined in a HTTP response, the browser can open the file with the proper extension/plugin.

What is an Internet Media Type?

"Internet Media Type" is the same as a MIME type. MIME types were originally created for emails sent using the SMTP protocol. Nowadays, this standard is used in a lot of other protocols, hence the new naming convention "Internet Media Type".

What is a Content-Type?

A "Content-type" is simply a header defined in many protocols, such as HTTP, that makes use of MIME types to specify the nature of the file currently being handled.

What does a MIME type look like?

A MIME type is a string identifier composed of two parts: a "type" and a "subtype". The "type" refers to a logical grouping of many MIME types that are closely related to each other; it's no more than a high level category. "subtypes" are specific to one file type within the "type".
For example, the MIME value "application/xml" is used for XML documents and specifies that the "xml" subtype belongs in the "application" type.

Why are some MIME subtypes prefixed with "x-"?

The "x-" prefix of a MIME subtype simply means that it's non-standard, i.e. not registered with the "Internet Assigned Numbers Authority" (IANA).

Why are some MIME subtypes prefixed with "vnd"?

The "vnd" prefix means that the MIME value is vendor specific.

How can I set the Content-type header of my files?

It depends on the programming language you are using. Note that most language have a default "Content-type" of "text/html".
  • Setting the Content-type in Java:

    // Response is of type javax.servlet.ServletReponse
    response.setContentType("text/plain");
  • Setting the Content-type in PHP:

    <?php header('Content-type: text/html');?>
 @reference_4_freeformatter
MIME Types List
@reference_5_stackoverflow
What are all the possible values for HTTP “Content-Type” header?

Difference between servlet and JSP


public void service(ServletRequest request, ServletResponse response){
    PrintWriter writer = ((HttpServletResponse) response)).getWriter();

    writer.write("<html>");
    writer.write("<body>");
    writer.write("<table>");

    for(int i=0; i<10; i++){
       writer.write("<tr><td>");
       writer.write("" + i);
       writer.write("</td></tr>");
    }

    writer.write("</table>");
    writer.write("</body>");
    writer.write("</html>");
}
 
<html>
<body>
<table>
    <%
        for(int i=0; i<10; i++){
            %><tr><td><%=i%></td></tr><%
        }
    %>
</table>
</body>
</html>
 
As you can see, the JSP example is much shorter than the Servlet example, and it is much easier to get an overview of the HTML. However, this is not always the case. If you are working with an application with much domain logic, and little HTML, a servlet will perhaps be easier to use. If you have a lot of HTML and only a little logic here and there, JSP would probably be better suited for the task.

@reference_1_tutorials.jenkov.com
Java Web Application Technologies

Java Annotations & Reflection & Lambda Expressions

@reference_1_tutorials.jenkov.com
Java Annotations

@reference_2_tutorials.jenkov.com
Java Reflection Tutorial

@reference_3_tutorials.jenkov.com
Java Lambda Expressions

Getting Started with Apache TomEE


@reference_0_youtube
Getting Started with Apache TomEE 
@reference_1_stackoverflow
Dynamic Web Project option missing in Eclipse
@reference_2_youtube
Eclipse Dynamic Web Project missing / Java EE Tools not available + Installing External Tools
@reference_3_help.eclipse.org
Dynamic Web projects and applications
@reference_4_stackoverflow
The import javax.servlet can't be resolved
@reference_5_stackoverflow
java EJB: The import javax.ejb cannot be resolved
@reference_6_stackoverflow
Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use
@reference_7_openejb.apache.org
JavaEE Examples

Monday, June 5, 2017

How to connect one model variable to mutiple html elements using angularjs data binding?

@reference_1_stackoverflow
How to connect one model variable to mutiple html elements using angularjs data binding?

@reference_1_toptal
Top 18 Most Common Mistakes that AngularJS Developers Make

Common Mistake #2: Not Having a Dot In There

You probably have read that if you were not having a dot in your ng-model, you were doing it wrong. When it regards inheritance, that statement is often true. Scopes have a prototypal model of inheritance, typical to JavaScript, and nested scopes are common to AngularJS. Many directives create child scopes such as ngRepeat, ngIf, and ngController. When resolving a model, the lookup starts on the current scope and goes through every parent scope, all the way to $rootScope.
But, when setting a new value, what happens depends on what kind of model (variable) we want to change. If the model is a primitive, the child scope will just create a new model. But if the change is to a property of a model object, the lookup on parent scopes will find the referenced object and change its actual property. A new model would not be set on the current scope, so no masking would occur:
function MainController($scope) {
  $scope.foo = 1;
  $scope.bar = {innerProperty: 2};
}

angular.module(‘myApp’, [])
.controller('MainController', MainController);
<div ng-controller=“MainController">
  <p>OUTER SCOPE:</p>
  <p>{{ foo }}</p>
  <p>{{ bar.innerProperty }}</p>
  <div ng-if=“foo”> <!— ng-if creates a new scope —>
    <p>INNER SCOPE</p>
    <p>{{ foo }}</p>
    <p>{{ bar.innerProperty }}</p>
    <button ng-click="foo = 2”>Set primitive</button>
    <button ng-click=“bar.innerProperty = 3”>Mutate object</button>
  </div>
</div>
Clicking the button labelled “Set primitive” will set foo in the inner scope to 2, but will not change foo in the outer scope.
Clicking the button labelled “Change object” will change the bar property from the parent scope. Since there is no variable on the inner scope, no shadowing will happen, and the visible value for bar will be 3 in both scopes.
Another way to do this is to leverage the fact that the parent scopes and the root Scope are referenced from every scope. The $parent and $root objects can be used to access the parent scope and $rootScope, respectively, directly from the view. It may be a powerful way, but I am not a fan of it due to the problem with targeting a particular scope up the stream. There is another way to set and access properties specific to a scope - using the controllerAs syntax.


@reference_3_itfashion4u
 AnularJS 'limitTo' filter inside/outside div element with 'ng-if' attribute

AnularJS 'limitTo' filter inside/outside div element with 'ng-if' attribute

The 'ng-repeat' limitTo filter won't work unless you remove the ng-if='true' attribute or put the <ul> list inside div element.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="myApp" ng-controller="namesCtrl">

<div ng-if='true'>

<p>Type a letter in the input field:</p>

<p><input type="number" ng-model="test"></p>
</div>
<ul>
  <li ng-repeat="x in names | limitTo:test">
    {{ x }}
  </li>
</ul>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        'Jani',
        'Carl',
        'Margareth',
        'Hege',
        'Joe',
        'Gustav',
        'Birgit',
        'Mary',
        'Kai'
    ];
});
</script>

<p>The list will only consists of names matching the filter.</p>
</body>
</html>

@reference_1_w3schools
@reference_2_youtube

@reference_3_itfashion4u
How to connect one model variable to mutiple html elements using angularjs data binding?

Sunday, June 4, 2017

< input type=“number” > not working in IE10

IE doesn't have support for input type="number" but you can use a polyfill to make it work.
Here is the solution : http://html5please.com/#number

@reference_1_stackoverflow
<input type=“number”> not working in IE10

Showing Spinner GIF during $http request in angular

@reference_1_stackoverflow
Showing Spinner GIF during $http request in angular

@reference_2_stackoverflow
Delay loading data in Angular JS

Use this 'ng-cloak' to avoid the undesirable flicker effect caused by the html template display

What you are looking for is ng-cloak.
You have to add it like this:
<body class="{{ bodyClass }}" ng-cloak> 
Edit:
It is also advisable to put the snippet below into your CSS file, according to docs.
"For the best result, the angular.js script must be loaded in the head section of the html document; alternatively, the css rule above must be included in the external stylesheet of the application."
[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak { display: none !important; }


@reference_1_stackoverflow
AngularJS strategy to prevent flash-of-unstyled-content for a class

@reference_2_angularjs.org
ngCloak - directive in module ng
 
* @reference_3_stackoverflow  
 AngularJS: How to prevent “code flash” in page while loading