Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, 4 June 2009

eFitnesse

FitNesse is a software development collaboration tool

http://fitnesse.org/

Thursday, 21 May 2009

split("|") error

String a = "sssrrrr|ccccc";

String[] b= a.split("\\|");

System.out.println(b[0]);

Wednesday, 8 October 2008

simple File upload

========= HTML =============
[html]
[head]
[meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"]
[title]1[/title]
[body]
[form method="post" action="upload1.jsp" enctype="multipart/form-data"]
Name : [input type="text" name="userName" value="myname"][br]
file : [input type="file" name="upfile"][br]
[input type="submit" value="submit"]
[/form]
[/body]
[/html]

====================

[head]
[meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"]
[title] 1[/title]
[/head]
[body]
[%
String path = "C:/test111/";
DiskFileUpload upload = new DiskFileUpload();
upload.setSizeMax(1024 * 1024);
upload.setSizeThreshold(4096);
upload.setRepositoryPath(path + "temp");
List items = upload.parseRequest(request);


FileItem item1 = (FileItem) items.get(0);
String name = item1.getString("euc-kr");
out.println("Name : "+name + "[br]");


FileItem item2 = (FileItem) items.get(1);
String fileName = item2.getName();
fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
long fileSize = item2.getSize();
File file = new File(path + "/" + fileName);
item2.write(file);
out.println(fileName + " !!!");
%]
[/body]
[/html]

Tuesday, 7 October 2008

Get All parameter (key and value)

Enumeration ss = request.getParameterNames();

while(ss.hasMoreElements()) {

String key = (String)ss.nextElement();
String value = request.getParameter(key);
System.out.println(key + " : " + value+"
");
}

// if value is array type such as check box
/*
String test[] = request.getParameterValues("test");

if(test != null) {
for(int i=0; i < test.length; i++) {
System.out.println("test[" + i + "] : " + test[i] );
}
}
*/

Monday, 7 July 2008

JSON : Array List <> JSON

Converting method between array List and JSON

You need some jar files.
1. json-lib-2.2.2-jdk15.jar
2. commons-beanutils.jar
3. commons-lang.jar
4. commons-collections.jar
5. commons-logging.jar
6. ezmorph-1.0.4.jar

Please set classpath those jar files.
And here is sample source to change from ArrayList to JSON.


import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

public class testA {

/**
* @param args
*/
public static void main(String[] args) {

List mybeanList = new ArrayList();
mybeanList.add("S");
mybeanList.add("b");

JSONArray jsonA = JSONArray.fromObject(mybeanList);
System.out.println(jsonA);
}

}

Wednesday, 16 April 2008

[Spring] Basic Example 1

[Spring] Simple MVC Web application Example

Modify from Spring reference
http://www.springframework.org/docs/Spring-MVC-step-by-step/index.html

- Create Control and just display hello world
click here

Monday, 14 April 2008

Spring Eclipse IDE sample

- install Eclipse IDE for spring and simple demo

* http://www.javabeat.net/articles/2007/09/introduction-to-spring-ide-2-0/


- Sample source
click Here

Thursday, 10 April 2008

JBoss Error : Port already in use: 1098

when you run jboss from "run.bat" file, you might have the error "Port already in use: 1098"

In this case, please open /server/default/conf/jboss-service.xml
and change from "from 1098 to 1122(a port hasn't been taken)".

Then restarted the JBOSS, everything looks ok now.

Monday, 7 April 2008

Exception Example

class NewExceptionTest {
public static void main(String args[]) {
try {
startInstall();
copyFiles();
} catch (SpaceException e) {
System.out.println("error : " + e.getMessage());
e.printStackTrace();
System.out.println("not enough space.");
} catch (MemoryException me) {
System.out.println("error : " + me.getMessage());
me.printStackTrace();
System.gc(); //expand memory from Garbage Collection
System.out.println("install again.");
} finally {
deleteTempFiles(); // delete temp files
} // end of try
} // end of main

static void startInstall() throws SpaceException, MemoryException {
if(!enoughSpace()) // if not enought space...
throw new SpaceException("not enought space.");
if (!enoughMemory()) // if not memory space..
throw new MemoryException("not enought memory space.");
} // end of startInstall

static void copyFiles() { /* copy code. */ }
static void deleteTempFiles() { /* delete temp files code .*/}

static boolean enoughSpace() {
// code for checking enough space.
return false;
}
static boolean enoughMemory() {
// code for checking memory space.
return true;
}
} // end of ExceptionTest

class SpaceException extends Exception {
SpaceException(String msg) {
super(msg);
}
}

class MemoryException extends Exception {
MemoryException(String msg) {
super(msg);
}
}

Thursday, 27 March 2008

Java Interface concept - JDBC way

==== Developer A side ======
class InterfaceTest2 {
public static void main(String[] args) {
A a = new A();
a.methodA();
}
}


==== Developer B side ======
=== Do not change Class name "A" and method "methodA"
=== Except two factors B side can change anything

class A {
void methodA() {
I i = InstanceManager.getInstance();
i.methodB();
}
}

interface I {
public abstract void methodB();
}

class B implements I {
public void methodB() {
System.out.println("methodB in B class");
}
}

class InstanceManager {
public static I getInstance() {
return new B();
}
}

Friday, 21 March 2008

Java Split Example

public class Test {
public static void main(String[] args) {
String temp = "123.456";

String[] temp2 = new String[2];

temp2 = temp.split("[.]");

System.out.println(temp2[0]);
}
}

Thursday, 20 March 2008

Simple JSon Example

<html>
<head>
<title>JSON test</title>

</head>
<body>

<select id="sel">
</select>

<script type="text/javascript">
var cafe = {"cafelist" : [
{"name" : "aaaaaa", "clubid" : "11111111"},
{"name" : "bbbbbbb", "clubid" : "2222222"},
{"name" : "CCC", "clubid" : 123213213}
]
};

for (var i=0; i<cafe.cafelist.length; i++)
{
//alert(cafe.cafelist[i].name + "," + cafe.cafelist[i].clubid);
var s = document.getElementById("sel");
s.options.add(new Option(cafe.cafelist[i].name, cafe.cafelist[i].clubid),i);
}
</script>
</body>
</html>

Tuesday, 18 March 2008

Java Interface concept example2

===== TVBoard.java ===========
public interface TVBoard {

public void channelDown();
public void channelUp();
public void volumeDown();
public void volumeUp();
public void powerOnOff(boolean power);
}

========== SSgTV.java ===========

public class SSgTV implements TVBoard {

private String name = "SSg TV";
private int volume = 5;
private int channel = 7;
private boolean power = false;

public SSgTV() {
System.out.println("SSg TV is created.");
}

@Override
public void channelDown() {
this.channel -= 1;
System.out.println(this.name+"- channelDown");
}

@Override
public void channelUp() {
this.channel += 1;
System.out.println(this.name+" + channelIp");
}

@Override
public void volumeDown() {
this.volume -= 1;
System.out.println(this.name+" - volumnDown");
}

@Override
public void volumeUp() {
this.volume += 1;
System.out.println(this.name+" + volumnUp");
}

@Override
public void powerOnOff(boolean power) {
this.power = power;

if (this.power==false)
System.out.println(this.name+" power off");
else
System.out.println(this.name+" power on");
}

public void SleepTimer(int time) {
System.out.println(time+" please wait for power on or off");

this.powerOnOff(false);
}
}

============== LGsTV.java ==============

public class LGsTV implements TVBoard {

private String name = "LGg TV";
private int volume = 5;
private int channel = 7;
private boolean power = false;

public LGsTV() {
System.out.println("LGs TV is created.");
}

@Override
public void channelDown() {
this.channel -= 1;
System.out.println(this.name+"- channelDown");
}

@Override
public void channelUp() {
this.channel += 1;
System.out.println(this.name+" + channelIp");
}

@Override
public void volumeDown() {
this.volume -= 1;
System.out.println(this.name+" - volumnDown");
}

@Override
public void volumeUp() {
this.volume += 1;
System.out.println(this.name+" + volumnUp");
}

@Override
public void powerOnOff(boolean power) {
this.power = power;

if (this.power==false)
System.out.println(this.name+" power off");
else
System.out.println(this.name+" power on");
}

public void SleepTimer(int time) {
System.out.println(time+" please wait for power on or off");

this.powerOnOff(false);
}

}

=============== TVTestMain.java =============

public class TVTestMain {

/**
* @param args
*/
public static void main(String[] args) {
TVBoard tvBoard = new SSgTV();
tvBoard.powerOnOff(true);
tvBoard.channelUp();
tvBoard.channelDown();
tvBoard.volumeUp();
tvBoard.volumeUp();
tvBoard.volumeUp();
tvBoard.volumeUp();
tvBoard.powerOnOff(false);
System.out.println();

TVBoard gTvBoard = new LGsTV();
gTvBoard.powerOnOff(true);
gTvBoard.channelUp();
gTvBoard.channelDown();
gTvBoard.volumeUp();
gTvBoard.volumeUp();
gTvBoard.volumeUp();
gTvBoard.volumeUp();
gTvBoard.powerOnOff(false);
}

}

Thursday, 13 March 2008

Java Interface concept example

========= Example 1 =================
**** GuguInter.java ***
interface GuguInter {
public static final int input1 = 2;
public static final int input2 = 3;
public void printGugu2();
pulbic void printGugu3();
}

***** PrintGuguClass.java ******

public class PrintGuguClass implements GuguInter {
PrintGuguClass c = new PrintGuguClass();
c.printGugu2();
c.printGugu3();
}

public void printGugu2() {
System.out.println(GuguInter.input1+ " printed");
}

public void printGugu3() {
System.out.println(GuguInter.input2+ " printed");
}

}

============ Example 2 ====================
****** myInterface.java **********
interface myInterface {
public abstract void method();
}

**** CB.java ***********
class CB implements myInterface {
public void method() {
System.out.println("method in CB");
}
}

***** CC.java **********
class CC implements myInterface {
public void method() {
System.out.println("method in CC");
}
}

***** CA.java ********
class CA {
public void method(myInterface mi) {
my.method()
}
}

***** Cmain.java *****
class Cmain {
public static void main(String[] args) {
CA ca = new CA();
ca.method(new CB());
ca.method(new CC());
}
}

Sunday, 6 January 2008

Thursday, 3 January 2008

replaceAll with special words

* replaceAll with special words

======================
public class MyTest {

public static void main(String args[]) {

String st = "\\";

st = st.replaceAll("\\\\","\\\\\\\\");

System.out.println(st);
}
}
======================================

Tuesday, 6 November 2007

Dom parsing as loop

This is an example how we can parse xml file with using loop method.

see here

Map example

import java.util.*;

public class HashMapTest
{
public static void main(String argv[])
{
HashMap hm = new HashMap();
System.out.println(hm.put("aaa", "111"));
System.out.println(hm.put("bbb", "222"));
System.out.println(hm.put("aaa", "444"));
System.out.println(hm.put("ccc", "333"));
System.out.println(hm.put("ccc", null));

System.out.println("HashMap size : " + hm.size());

Set set = hm.keySet();
Object []hmKeys = set.toArray();
for(int i = 0; i < hmKeys.length; i++)
{
String key = (String)hmKeys[i];
System.out.print(key);
System.out.print(" - ");
System.out.println((String)hm.get(key));
}
}
}


/**
run:java HashMapTest
output:
null
null
111
null
333
HashMap size : 3
ccc - null
bbb - 222
aaa - 444

Monday, 5 November 2007

List example

import java.util.List ;
import java.util.Vector ;

public class A {

public static void main(String[] args) {
A a = new A() ;
List b = a.getSchools() ;

// get values
for( int i = 0 ; i < b.size() ; i++ ){
String str = (String)b.get(i) ;
System.out.println("["+i+"]:"+str) ;
}

System.out.println("=================================") ;

// add the value as the last position
b.add("SchoolName4") ;
b.add("SchoolName5") ;

// get values
for( int i = 0 ; i < b.size() ; i++ ){
String str = (String)b.get(i) ;
System.out.println("["+i+"]:"+str) ;
}

System.out.println("=================================") ;

// insert value to 3 position
b.add(2, "SchoolName6") ;

// get values
for( int i = 0 ; i < b.size() ; i++ ){
String str = (String)b.get(i) ;
System.out.println("["+i+"]:"+str) ;
}


}

private List getSchools() {

Vector v = new Vector() ;
v.add("SchoolName1") ;
v.add("SchoolName2") ;
v.add("SchoolName3") ;
return (List)v ;

}

}