'Java / JSP/Java'에 해당되는 글 29건
- 2011/06/23 Java에서 웹 브라우저 띄우기
- 2011/03/16 Java Code Convention
- 2011/02/17 Java Shallow Copy and Deep Copy
- 2011/01/19 Java 시스템 정보 가져오기
- 2010/08/05 Java 트랜잭션 처리
- 2010/06/29 [String 비교하기] .equals()와 ==은 다른가요?
- 2010/06/11 Java 컴파일과 실행
import java.lang.reflect.Method;
public class BrowserControl {
/**
* Method to Open the Broser with Given URL
*
* @param url
*/
public static void openUrl(String url) {
String os = System.getProperty("os.name");
Runtime runtime = Runtime.getRuntime();
try {
// Block for Windows Platform
if (os.startsWith("Windows")) {
String cmd = "rundll32 url.dll,FileProtocolHandler " + url;
Process p = runtime.exec(cmd);
}
// Block for Mac OS
else if (os.startsWith("Mac OS")) {
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
openURL.invoke(null, new Object[] { url });
}
// Block for UNIX Platform
// else {
// String[] browsers = {"firefox", "opera", "konqueror", "epiphany",
// "mozilla", "netscape" };
// String browser = null;
// for (int count = 0; count < style="color: rgb(153, 0, 0);">length
// && browser == null; count++)
// if (runtime.exec(new String[] {"which",
// browsers[count]}).waitFor() == 0)
// browser = browsers[count];
// if (browser == null)
// throw new Exception("Could not find web browser");
// else
// runtime.exec(new String[] {browser, url});
// }
} catch (Exception x) {
System.err.println("Exception occurd while invoking Browser!");
x.printStackTrace();
}
}
public static void main(String[] args) {
openUrl("http://javaxden.blogspot.com");
}
}
윈도우에서는 뜨는거 확인했고, 맥이랑 유닉스는 확인 못했씁니다..
[출처] Launch Web Browser through Java
Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the references are copied. Thus, if the object you are copying contains references to yet other objects, a shallow copy refers to the same subobjects.
Deep copy is a complete duplicate copy of an object. If an object has references to other objects, complete new copies of those objects are also made. A deep copy generates a copy not only of the primitive values of the original object, but copies of all subobjects as well, all the way to the bottom. If you need a true, complete copy of the original object, then you will need to implement a full deep copy for the object.
Java supports shallow and deep copy with the Cloneable interface to create copies of objects. To make a clone of a Java object, you declare that an object implements Cloneable, and then provide an override of the clone method of the standard Java Object base class. Implementing Cloneable tells the java compiler that your object is Cloneable. The cloning is actually done by the clone method.
[출처] What is shallow copy and deep copy in Java?
Java에서 현재 시스템의 정보를 가져오기 위해서는 System 클래스의 getPoperty(String key) 메소드를 이용하면 된다.
| Key | Description | |
| 1 | java.version | The version of Java Runtime Environment. |
| 2 | java.vendor | The name of Java Runtime Environment vendor |
| 3 | java.vendor.url | The URL of Java vendor |
| 4 | java.home | The directory of Java installation |
| 5 | java.vm.specification.version | The specification version of Java Virtual Machine |
| 6 | java.vm.specification.vendor | The name of specification vendor of Java Virtual Machine |
| 7 | java.vm.specification.name | Java Virtual Machine specification name |
| 8 | java.vm.version | JVM implementation version |
| 9 | java.vm.vendor | JVM implementation vendor |
| 10 | java.vm.name | JVM implementation name |
| 11 | java.specification.version | The name of specification version Java Runtime Environment |
| 12 | java.specification.vendor | JRE specification vendor |
| 13 | java.specification.name | JRE specification name |
| 14 | java.class.version | Java class format version number |
| 15 | java.class.path | Path of java class |
| 16 | java.library.path | List of paths to search when loading libraries |
| 17 | java.io.tmpdir | The path of temp file |
| 18 | java.compiler | The Name of JIT compiler to use |
| 19 | java.ext.dirs | The path of extension directory or directories |
| 20 | os.name | The name of OS name |
| 21 | os.arch | The OS architecture |
| 22 | os.version | The version of OS |
| 23 | file.separator | The File separator |
| 24 | path.separator | The path separator |
| 25 | line.separator | The line separator |
| 26 | user.name | The name of account name user |
| 27 | user.home | The home directory of user |
| 28 | user.dir | The current working directory of the user |
예제
public class OpertingSystemInfo {
public static void main(String[] args) {
String nameOS = "os.name";
String versionOS = "os.version";
String architectureOS = "os.arch";
System.out.println("\n The information about OS");
System.out.println("\nName of the OS: " + System.getProperty(nameOS));
System.out.println("Version of the OS: "
+ System.getProperty(versionOS));
System.out.println("Architecture of THe OS: "
+ System.getProperty(architectureOS));
}
}
[출처] Operating System Information
트랜잭션(Transaction)은 All or Nothing 으로 표현되는 것으로 데이터베이스와 연동을 하는 어플리케이션에 있어서 아주 중요한 것이다. 데이터베이스와 연동된 모든 프로그램은 트랜잭션이 완벽히 보장되어야 한다.
JSP에서 제공하는 트랜잭션 처리에 대한 메소드들은 다음과 같다.
* 트랜잭션을 위한 메소드 : commit(), rollback()
JDBC API의 Connection 객체는 commit() 메소드와 rollback() 메소드를 제공한다. commit() 는 트랜잭션의 commit 을 수행하고, rollback() 메소드는 트랜잭션의 rollback 을 수행한다.
기본적으로 Connection 객체에 setAutoCommit(boolean autoCommit) 이란 메소드가 있는데 기본값이 true로 설정되어 있다. 기본적으로 JSP는 오토커밋(Autocommit)이다. 그래서 우리가 지금까지 작성한 쿼리문이 오토커밋(Autocommit)에 의해 자동으로 수행되었던 것이다.
그러나 트랜잭션을 처리할 때는 오토커밋(Autocommit)에 의해 자동으로 commit을 사용하면 안 된다. 여러 개의 쿼리 문장이 하나의 작업으로 수행되어야 하기 때문에 JSP의 오토커밋(Autocommit)이 자동으로 작동되지 못하게 해야 한다. 오토커밋(Autocommit)이 자동으로 작동되지 못하게 하려면 setAutoCommit(false); 로 지정해야 한다.
여러 작업을 하나의 트랜잭션으로 묶어서 처리하는 JSP의 예제는 다음과 같다.
Connection conn = null;
try {
conn = DriverManager.getConnection(url,dbId,dbPwd);
conn.setAutoCommit(false); //트랜잭션 처리를 위해서 AutoCommit을 중지한다.
//.... 데이타내의 수정문(Insert, Update, Delete) 등을 수행
//.... 데이타내의 수정문(Insert, Update, Delete) 등을 수행
//.... 데이타내의 수정문(Insert, Update, Delete) 등을 수행
//.... 데이타내의 수정문(Insert, Update, Delete) 등을 수행
conn.commit(); //데이타 처리시 에러가 없다면 commit 수행
}catch(SQLException e) {
e.printStackTrace();
if( conn != null ) {
try {
conn.rollback(); //에러발생시 rollback 처리
} catch(SQLException sqle) {
}
}
}
finally {
try {
conn.setAutoCommit(true); //트랜잭션 처리를 기본상태로 되돌린다.
if( pstmt != null ) {
pstmt.close();
}
if( conn != null ) {
conn.close();
}
} catch (SQLException sqe) {
}
}
aString.equals(bString)
하지만 왜 String을 비교할 때만 이렇게 비교하는지 우리는 알고 코딩을 하고 있는 것일까? int, boolean, long 등은 == 로 비교를 하면서 String은 왜 다를까?지금부터 String을 비교할 때 .equals()와 == 이 어떻게 다른지 알아보자.
일단 코드를 실행해보고 결과를 보면서 진행하겠습니다. 저는 백문이불여일견이라는 말을 좋아하거든요.^^
public class Test {
/** * @param args */
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test().testString();
}
public void testString() {
String a = "TEST";
String b = "TEST";
String c = new String("TEST");
String d = new String("TEST");
System.out.println("1:"+System.identityHashCode(a));
System.out.println("2:"+System.identityHashCode(b));
System.out.println("3:"+System.identityHashCode(c));
System.out.println("4:"+System.identityHashCode(d));
System.out.println("5:"+(a == b));
System.out.println("6:"+a.equals(b));
System.out.println("7:"+(c == d));
System.out.println("8:"+c.equals(d));
}
}
위 코드를 실행해 보면 결과는 다음과 같이 나옵니다.

결과를 보면 a,b는 hashcode값이 같지만 c,d는 값이 다른 것이 보입니다.
그럼 이번엔 비교한 결과를 볼까요? a,b는 equals()로 비교를 하거나 ==로 비교를 해도 결과는 항상 true 입니다. 하지만 c,d는 equals() 비교를 하면 트루지만 ==로 비교를 하면 false죠.
왜 이런 결과가 나올까요?
이런 결과가 나오는 이유는 equals()는 비교대상의 문자열 리터럴(값)을 비교하고 ==는 대상 객체를 비교하기 때문입니다. 또한 자바에서는 a,b처럼 같은 값을 가지면 하나의 메모리 주소를 참조하게 됩니다. 즉, a,b의 "TEST"라는 문자열 리터럴은 컴파일되면 String 객체가 되는데 a,b는 같은 메모리 주소를 가지게 됩니다. 하지만 c,d는 처음부터 별도의 String 객체로 생성되었기 때문에 동일한 "TEST"라는 문자열 리터럴을 가지지만 서로 다른 객체가 됩니다.무슨말인지 모르시겠다구요? 그럼 그림으로 한번 볼까요?

[출처] [String 비교하기] .equals()와 ==은 다른가요?
1. Compile
java compile은 javac 명령을 이용해 가능하다.
javac source_file.java
예) javac MyFirstJava.java
javac -d target_directory source_file.java
예) javac -d ./lib MyFirstJava.java
2. Run
java 실행은 java 명령을 이용해 가능하다
java class_file
예) java MyFirstJava
java -classpath target_directory class_file
예) java -classpath ./lib MyFirstJava
3. Package
3.1 Package Compile
java에서 package 컴파일을 -d 옵션을 이용해 가능하다
javac -d target_directory source_file.java
예) javac -d ./lib MyFirstJava.java
3.2 Package Run
패키지를 실행하기 위해서는 -classpath 옵션을 지정해야 한다.
java -classpath target_directory package.class_file
lib 디렉토리에 저장이 되어 있다면
예)java -classpath ./lib com.company.test.MyFirstJava
와 같이 실행을 해야 한다.
4. 외부 library를 이용한 Compile
외부 library를 이용해 컴파일을 하기 위해서는 -classpath 옵션을 사용한다.
javac -classpath lib_file.jar source_file.java
[참조] Java compile과 실행

codeconventions-150003.pdf
Prev
Rss Feed