Showing posts with label Technologies. Show all posts
Showing posts with label Technologies. Show all posts

Thursday, July 2, 2009

Getting started with Java

1) download the Java EE Software Development Kit (SDK)
- download link: http://www.oracle.com/technetwork/java/javaee/downloads/index.html

What Java Do I Need?
You must have a copy of the JRE (Java Runtime Environment) on your system to run Java applications and applets. To develop Java applications and applets, you need the JDK (Java Development Kit), which includes the JRE.

What's the difference between J2SE and J2EE?
J2SE has access to all of the SE libraries. However, EE adds a set of libraries for dealing with enterprise applications such as Servlets, JSP and Enterprise
Javabeans.


2) install JDK
- Windows instructions/troubleshooting: http://java.sun.com/javase/6/webnotes/install/jdk/install-windows.html

Why shouldn't I install in "C:\Program Files\Java"?
Some apps (e.g. maven, plugins) uses your Java path without considering potential whitespace on the path causing "C:\Program Files\Java" to become "C:\Program" w/c leads to errors. So either use a path w/out whitespace or set you path to JAVA_HOME=C:\Progra~1\Java not JAVA_HOME=C:\Program Files\Java


3) update environment variables (NOT case-sensitive under Windows)
PATH
- defines the search paths for executable programs (with file extension of ".exe", ".bat" or ".com" for Windows systems) invoked from a command shell ("cmd.exe")
- allows the use of javac and java
- if not set, you need to specify the full path to the executable every time you run it, such as: C:\Program Files\Java\jdk1.6.0\bin\javac MyClass.java
- so add here the JDK binary (bin) directory (e.g., "c:\jdk1.6\bin")
- Note: The JDK binary directory should be listed before "c:\windows\system32" and "c:\windows" in the PATH. This is because some Windows systems provide their own Java runtime (which is often outdated) in these directories (try search for "java.exe" in your computer!).

CLASSPATH
- defines the directories and Java's jar-files for searching for the Java classes referenced in a Java program
- normally, no explicit CLASSPATH setting is required
- if not set, default is current working directory (since JDK 1.3)
- if set, include the current working directory '.'
- link: How Classes are found

JAVA_HOME
- needed for running Tomcat and many Java applications
- set here the JDK installation directory, e.g., "c:\jdk1.6

How to set environment variables in Mac/Unix
1. Set environment variables for your user in ~/.bash_profile (will affect bash shells only).
Create the file if it does not exist:
touch ~/.bash_profile
Open the file:
open ~/.bash_profile
Add environment variables in the file:
export JAVA_HOME=$(/usr/libexec/java_home)
export JRE_HOME=$(/usr/libexec/java_home)
2. Set temporary environment variables for the current bash shell. Just type the same command in 1. to the current bash shell.

Ref: http://www3.ntu.edu.sg/home/ehchua/programming/howto/environment_variables.html

Monday, February 16, 2009

Java Pointers / Links

JSP Pointers

  • secure/nonsecure alerts on IE6 & 7
  • http://www.zorked.com/security/ie-mixed-content-secure-nonsecure-items/

  • no caching on browsers
  • add: (remove space b/w < and META)
      
    < META HTTP-EQUIV="Expires" CONTENT="0"> - any # less than 1 means no cache
    < META HTTP-EQUIV="Pragma" CONTENT="no-cache"> - for backward compatability
    < META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> - for HTTP 1.1 spec  

  • day in Date
  • function fnGetPrevDayDate(objDate){
     // 1 day = 86400000 ms 
     // var datPrevDate = new Date(objDate.getTime() - 86400000);
     // code above returns incorrect result when time changes from DST to standard time
     
     var datPrevDate = new Date(objDate);
     datPrevDate.setDate(datPrevDate.getDate() - 1);
     return datPrevDate;
    }

  • html:checkbox
  • -If the form is in session and you change the checkbox from true(checked) to false(unchecked), the checkbox attribute in form is not set back to false. This is because the unchecked checkbox is not sent in the request and is therefore not set in the form.
    -solution: Add a reset method in form & reset checkbox to false. Everytime you submit the form, form is reset and then populated before moving to action, thus having correct value for the checkbox.

  • how to use indexId on logic:iterate
  • < logic:iterate id="entityRow" name="<%= DerivativeEntityConstantsIF.SEARCH_RESULT %>" scope="session" offset="<%= String.valueOf(offset)%>" length="<%= String.valueOf(maxLength)%>" indexId="indexNum">
      < html:select name="entityRow" property="linkageClassCode"
    onchange='<%= "fnEditValue(this, " + indexNum + ")" %>' >...

  • html element default values - defaultValue/defaultSelectedd
  • if (frm["termDeal.accountNo"].value != frm["termDeal.accountNo"].defaultValue) return true;
    if (fnIsSelectChanged(frm["termDeal.currency"])) return true;
    
    function fnIsSelectChanged(selectObj) {
      var index = selectObj.selectedIndex;
      var selectedOption = selectObj.options[index];
      if (selectedOption.defaultSelected) return false; 
      return true;
    }

  • use popUpCalendar.js
  • if (!document.layers){
    var startDate = document.getElementById("personalInfoForm.dateOfBirth");
    // use showCalendarWithHolidays if you want to show holidays
    document.write("<[toRemove]a href='javascript:void(0)' onclick='showCalendarWithHolidays(this, startDate, \"mm-dd-yyyy\",null,1,-1,-1, true)'><[toRemove]img src='' border='0'/>");
    }

  • regex of replace
  • .value.replace(/_|-/g, '')  --> replace _ and - with empty str

  • use of sessionScope
  • CL.isNewClient = ${sessionScope[ClientConstants.IS_NEW_CLIENT]} --> do not put semi colon at end (not sure if for AMS only)

Deep copy of Java objects

The class Object's clone method only performs a shallow copy. This means that it creates a new instance of the object but the fields of both the new and original object end up referencing the same objects because only the reference of the fields is copied to the new object. To make deep copies of an object, we can use this class:

import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;

/**
* Utility for making deep copies (vs. clone()'s shallow copies) of
* objects. Objects are first serialized and then deserialized. Error
* checking is fairly minimal in this implementation. If an object is
* encountered that cannot be serialized (or that references an object
* that cannot be serialized) an error is printed to System.err and
* null is returned. Depending on your specific application, it might
* make more sense to have copy(...) re-throw the exception.
*
* A later version of this class includes some minor optimizations.
*/
public class ObjectCloner {

// so that nobody can accidentally create an ObjectCloner object
private ObjectCloner() {
}

/**
* Returns a copy of the object, or null if the object cannot
* be serialized.
*/
public static Object deepCopy(Object orig) {
Object obj = null;
try {
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(orig);
out.flush();
out.close();

// Make an input stream from the byte array and read
// a copy of the object back in.
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(bos.toByteArray()));
obj = in.readObject();
}
catch(IOException e) {
e.printStackTrace();
}
catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return obj;
}
}

Sample call: Object clone = ObjectCloner.deepCopy(oldObject);
A more optimized version can be found here: http://javatechniques.com/blog/faster-deep-copies-of-java-objects/

Monday, January 5, 2009

Understanding Web Service

Web Service

- a software system designed to support interoperable machine-to-machine interaction over a network.
- makes software functionality available over the Internet so that programs like PHP, ASP, JSP, JavaBeans, the COM object, and all our other favorite widgets can make a request to a program running on another server (a web service) and use that program’s response in a website, WAP service, or other application.
- describes a standardized way of integrating Web-based applications using the XML, SOAP, WSDL and UDDI open standards over an Internet protocol backbone.

2 Types of Web Service

  1. SOAP (Simple Object Access Protocol)
    - a lightweight XML-based messaging protocol used to encode the information in Web service request and response messages before sending them over a network.
    - SOAP messages are formatted in XML and are typically sent using HTTP but can be also transported using a variety of Internet protocols, including SMTP and MIME.
    - older type, successor of XML-RPC
    - uses JAX-WS specification
  2. REST
    - is simpler and more light weighted than SOAP
    - can send and receive data as JSON, XML or even plain text
    - uses JAX-RS specification

Related Terms


XML (Extensible Markup Language)
- a general-purpose specification for creating custom markup languages developed by the W3C.
- is a markup language much like HTML. A markup language is a mechanism to identify structures in a document.
- allows designers to create their own customized tags, enabling the definition, transmission, validation, and interpretation of data between applications and between organizations.

WSDL (Web Services Description Language)
- an XML-formatted language used to describe a Web service's capabilities as collections of communication endpoints capable of exchanging messages. WSDL is the language that UDDI uses, developed jointly by Microsoft and IBM.
- describes the interface of Web services

UDDI (Universal Description, Discovery and Integration)
- it is a Web-based distributed directory that enables businesses to list themselves on the Internet and discover each other, similar to a traditional phone book's yellow and white pages.

SEI (Service Endpoint Interface)
- is a Java interface class that defines the methods to be exposed as a Web service
- coverts the web service call including the (Java, C++, etc.) objects to a SOAP message

Protocol
- is a convention or standard that controls or enables the connection, communication, and data transfer between two computing endpoints. In its simplest form, a protocol can be defined as the rules governing the syntax, semantics, and synchronization of communication. Protocols may be implemented by hardware, software, or a combination of the two.

References:
http://www.alistapart.com/articles/webservices
http://en.wikipedia.org
http://www.webopedia.com/TERM/W/Web_services.html
http://www.webopedia.com/DidYouKnow/Computer_Science/2005/web_services.asp
http://www.ibm.com/developerworks/library/ws-intwsdl
http://www.ibm.com/developerworks/library/ws-intwsdl2