Java AWT Example – Screen Capture

 

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class TestingScreenCapture {
 public TestingScreenCapture(){
 
 }
 
 public static void main(String[] args) throws AWTException, IOException{
 Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
 Robot robot = new Robot();
 BufferedImage capture = robot.createScreenCapture(screenRect);
 ImageIO.write(capture, "png", new File("testing.png"));

 
 }
}

 

 

Tomcat 8: Websocket Integration

The Websocket in Tomcat 8 could be implemented through the annotation @ServerEndpoint. It specifies the class as a Websocket Endpoint. Tomcat 8 would recognize the class and provide the Websocket service to the client.

 

package com.ittechoffice;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value="/hello")
public class Hello {
 
 public String messages = "";
 
 @OnOpen
 public void onOpen(Session session) { 
 System.out.println("@OnOpen");
 }

 @OnClose
 public void onClose(Session session) {
 System.out.println("@OnClose");
 }

 @OnMessage
 public void onMessage(Session session, String message) {
 System.out.println("@OnMessage");
 }

 @OnError
 public void onError(Session session, Throwable throwable) { 
 System.out.println("@OnError");
 }

}


hello.html

<html>

<head>
  
    url = "http://localhost:8080/helloworld-tomcat-websocket/hello";
    websocket = new WebSocket(url);
    websocket.send("hello");
  
</head>

</html>

JQuery

Introduction

JQuery provide various libraries for Client-Side Javascript Web Application.

  • JQuery, a Document Object Model (DOM) Manipulation Library
  • JQuery UI, a User Interface Interaction Library
  • JQuery Mobile, a Mobile Touch Library
  • QUnit, a Test Framework

Example List

 

$() Function

It would return  a collection of matched element found in DOM based on passed argument or created by passing HTML String.

The below is a html. The h1 tag is identified with id, example. $() Function would be used to retrieve the DOM element.

<html>
  <body>
    <h1 id='example'>Example</h1>
  </body>
</html>
$("#example")

It would return the element whose id is example.

 

Plugin

It enable new method can be added to Jquery object. The below example show a JQuery Plugin named “plugin".

$.fn.plugin= function() {
  console.log("plugin called")
};

$( "a" ).plugin();

 

Reference:

Javascript OOP

Introduction

JavaScript is Prototype-baed Programming Language. Prototype-based Programming is a Object-oriented Programming Model which the inheritance is performed through reuse of existing object (prototype).

 

Namespace

Namespace is a container which bundle up functionality. In JavaScirpt, it is just an object containing methods, property and objects.

 

Class

JavaScript uses functions as constructors for classes. Defining a function is to define a class in JavaScript.

var Student = function(){

};

var student1 = new Student();

 

Property

The properties of function is defined through the keyword, this.

var Student = function(name){
  this.name = name;
}

var student1 = new Student("Tester");
console.log(student1.name);

 

Method

Declared method to a class in JavaScript is just to add method to object prototype but not object. There methods would be assigned to prototype property of a function.

var Student = function(name){
  this.name = name;
};

Student.prototype.getName = function(){
  return this.name;
};

var student = new Student("tester");
console.log(student1.getName());

 

Reference

Java Database Connectivity (JDBC)

JDBC

It is an interface for access Database for Java.

The Class.forname(XXXXDriver) Frequently Ask Questions (FAQ)

http://stackoverflow.com/questions/8053095/what-is-the-actual-use-of-class-fornameoracle-jdbc-driver-oracledriver-while

JDBC Tutorial Example

http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm

Database Connection Pool

Reference: https://en.wikipedia.org/wiki/Connection_pool

It is used to reduce the connection establish time.

Java Virtual Machine (JVM)

Introduction

Java Virtual Machine (JVM) is a software that enables a computer to run a Java program. JVM can be separated into two components.

  • Specification
  • Implementation

Specification describes what is required in JVM while Implementation is Software that meets the requirements specified in Specification. There are many Implementation of JVM.

  • HotSpot (Oracle, Primary Reference Java VM Implementation)
  • J9 (IBM)

Slide2.PNG

Reference

MyBatis Generator

MyBatis Generator

Home Page: http://www.mybatis.org/generator/index.html

MyBatis Generator 1.3.2 release download:

https://github.com/mybatis/generator/releases/download/mybatis-generator-1.3.2/mybatis-generator-core-1.3.2-bundle.zip

Introduction: 

MyBatis Generator is to generate codes for MyBatis to accessing database. It includes:

  • SQLMap File (.xml)
  • Model File (.java)
  • Interface of Data Access Object File (.java)
  • Implementation of Data Access Object File (.java)

Prerequisite

  • JDBC Connector (.jar)
  • Config File (.xml)

 

The config can be referenced:

http://www.mybatis.org/generator/configreference/xmlconfig.html

Execution 

Run in Command line:

unzip mybatis-generator-core-1.3.2-bundle.zip cd mybatis-generator-core-1.3.2 cd lib java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

Related Article

 



			

					

Apache FreeMarker

Download: http://freemarker.org/freemarkerdownload.html

Homepage:  http://freemarker.incubator.apache.org/

 

Sample Code

 Configuration cfg = new Configuration();
 Template template = cfg.getTemplate(<templateFilePath>);
 Map<String, Object> data = new HashMap<String, Object>();
 data.put("<placeHolder1>", "<placeHolderValue1>");
 data.put("<placeHolder2>", "<placeHolderValue2>");
 Writer out = new OutputStreamWriter(System.out);
 template.process(data, out);
 out.flush();