You are on page 1of 69

C2

Bi 2.1: Khi nhn vo nt OK chng trnh s thc hin php ton trn hai s hng v
hin th trn StringItem.
- Khi nhn nt EXIT s thot khi chng trnh.
- Thc hin kim tra s liu xem c hp l khng (s hay ch, di ca s,) trc khi
tin hnh thc hin php ton, nu s liu nhp vo khng hp l th hin th cnh bo
(c th thc hin bng ItemStateListener hoc CommandListener)
gii
package bai1;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class cau1 extends MIDlet implements ItemStateListener,CommandListener {
Display di; Form f; Command c1,c2;
TextField tf1,tf2,tf3;
StringItem st; ChoiceGroup ch;
public void startApp() {
di=Display.getDisplay(this);
f=new Form("hien thi");
c1=new Command("OK", Command.OK, 1);
c2=new Command("EXIT", Command.EXIT, 1);
tf1=new TextField("nhap so 1", null, 10,TextField.ANY);
tf2=new TextField("nhap so 2", null, 10,TextField.ANY);
tf3=new TextField("nhap phep tinh", null, 10, TextField.ANY);
ch = new ChoiceGroup("tf3", ChoiceGroup.EXCLUSIVE);
st=new StringItem("ket qua", null);
f.addCommand(c1);
f.append(tf1);
f.append(tf2);
f.append(tf3);
f.addCommand(c2);
di.setCurrent(f);
f.append(st);
f.setCommandListener(this);
f.setItemStateListener(this);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void itemStateChanged(Item i){
st.setText(" ");
String t1= tf1.getString();
String t2=tf2.getString();
if(i==tf1){
for(int n=0;n<t1.length();n++){
if(((t1.charAt(n)>='a')&& (t1.charAt(n)<='z'))||(((t1.charAt(n)>='A'&&
t1.charAt(n)<='Z')))){
st.setText("Vui421412 long nhap chu so");
}
}
if(t1.length()>=6)
st.setText("khong duoc nhap qua 6 chu so");
}
if(i==tf2){
for(int n=0;n<t2.length();n++){
if(((t2.charAt(n)>='a')&& (t2.charAt(n)<='z'))||(((t2.charAt(n)>='A'&&
t2.charAt(n)<='Z')))){
st.setText("Vui long nhap chu so");
}
}
if(t2.length()>=6)
st.setText("khong duoc nhap qua 6 chu so");
}
}
public void commandAction(Command c, Displayable b){
int i1=Integer.parseInt(tf1.getString());
int i2 = Integer.parseInt(tf2.getString());
String t= tf3.getString();
if(c==c1 && t.equals("cong") ){
int tong= i1+i2;
st.setText(Integer.toString(tong));
}else
st.setText("");
if(c==c1 && t.equals("tru") ){
int hieu= i1-i2;
st.setText(Integer.toString(hieu));
}
if(c==c1 && t.equals("nhan") ){
int nhan= i1*i2;
st.setText(Integer.toString(nhan));
}
if(c==c1 && t.equals("chia") ){
int chia= i1/i2;
st.setText(Integer.toString(chia));}
// else
//st.setText("");
if(c==c2){
notifyDestroyed();
}
}
}
Bi 2.3: Chui k t c dng: a1:b1;a2:b2;a3:b3; (a, b l cc chui bt k v d: love: yu
thng; song: bi ht;).
- Nhn OK1 s tch cc chui con ra bi du ;
- Nhn nt OK2 s tch cc chui con ra bi du :

package b3c2;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Midlet extends MIDlet implements CommandListener{
Display di; Form f;
TextField tf;
Command c1,c2;
public void startApp() {
di=Display.getDisplay(this);
f=new Form("abc");
tf=new TextField("nhap","",30,TextField.ANY);
f.append(tf);
c1=new Command("ok1",Command.OK,1);
f.addCommand(c1);
c2=new Command("ok2",Command.EXIT,1);
f.addCommand(c2);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d){
String t=tf.getString();
if(c==c1){
// f.deleteAll();
// f.append(tf);
String t1;
while(t.length()>0){
t1=t.substring(0,t.indexOf(";"));
t=t.substring(t.indexOf(";")+1, t.length());
f.append(t1);
}
}
else
if(c==c2){
f.deleteAll();
f.append(tf);
String t1,t2,t3;
while(t.length()>0){
t1=t.substring(0,t.indexOf(";"));
t=t.substring(t.indexOf(";")+1, t.length());
t2=t1.substring(0, t1.indexOf(":"));
//f.append(t2);
t3=t1.substring(t1.indexOf(":")+1, t1.length());
f.append(t3);

}
}
}
}
Bi 2.5: Khi nhn nt OK chng trnh s tch cc k t s ra khi chui s v hin th
ln StringItem (v d nhp 123 th tch thnh s 1, s 2, s 3).
package b3c2;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Midlet extends MIDlet implements CommandListener{
Display di; Form f;
TextField tf;
Command c1,c2;
public void startApp() {
di=Display.getDisplay(this);
f=new Form("abc");
tf=new TextField("nhap","",30,TextField.ANY);
f.append(tf);
c1=new Command("ok1",Command.OK,1);
f.addCommand(c1);
c2=new Command("ok2",Command.EXIT,1);
f.addCommand(c2);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d){
String t=tf.getString();
if(c==c1){
f.deleteAll();
f.append(tf);
String t1,t3,t4;
String t2= new String("So");
while(t.length()>0){
t1=t.substring(0,1);
t3=t2.concat(t1+",") ;
f.append(t3);
t=t.substring(1, t.length());
//`1 f.append(t4);
}
}
Bi 2.6: Khi nhp chui s vo TextField th cc nh c sp xp theo th t ca chui
s (v d nhp 132 th nh 1 hin th, sau l nh 3 v cui cng l nh 2).
package mobileapplication1;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class nham extends MIDlet implements CommandListener{
Form f; Image m1,m2,m3; TextField tf; Command c1;
Display di; ImageItem t1,t2,t3;
public void startApp() {
di= Display.getDisplay(this);
f= new Form("hien thi anh");
try{
m1= Image.createImage("/icon.png");
m2= Image.createImage("/icon_1.png");
m3= Image.createImage("/icon_2.png");
}catch(Exception e){}
c1=new Command("chon",Command.OK,1);
t1= new ImageItem("anh1",m1,ImageItem.LAYOUT_CENTER,"");
t2= new ImageItem("anh2",m1,ImageItem.LAYOUT_CENTER,"");
t3= new ImageItem("anh3",m1,ImageItem.LAYOUT_CENTER,"");
tf= new TextField ("chon anh","",10,TextField.NUMERIC);
f.addCommand(c1);
f.append(t1);
f.append(tf);
f.append(t2);
f.append(t3);
di.setCurrent(f);
f.setCommandListener(this);
}
public void pauseApp() {
}
public void destroyApp(boolean b) {
}
public void commandAction(Command c,Displayable db){
String st= tf.getString();
if(c==c1)
f.deleteAll();
f.append(tf);
for(int i=0;i<=2;i++){
if(st.charAt(i)=='1')
f.append(t1);
else
if(st.charAt(i)=='2')
f.append(t2);
else
if(st.charAt(i)=='3')
f.append(t3);
}
}
}
Bi 2.7: Vit chng trnh c mt TextField v 3 nh. Mt nt OK. Khi nhp chui s
vo TextField th cc nh tng ng c xa (thm) vo trong Form. (v d nhp 21 th
nh 2 v nh 1 b xa/c thm)
package btchuong2;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class bai2_7 extends MIDlet implements CommandListener {
Display di;Form f;Command c1,c2;
TextField tf;Image i1,i2,i3;ImageItem it1,it2,it3;
int index1=-1,index2=-1,index3=-1;
public void startApp() {
di=Display.getDisplay(this);
f=new Form("BAI 2_7");
tf=new TextField("nhap_text"," ",20,TextField.ANY);
c1=new Command("Ok1",Command.BACK,1);
c2=new Command("OK2",Command.OK,1);
f.addCommand(c1);
f.addCommand(c2);
i1=null;i2=null;i3=null;
f.append(tf);
f.setCommandListener(this);
di.setCurrent(f);
try{
i1=Image.createImage("/anh1.PNG");
i2=Image.createImage("/anh2.PNG");
i3=Image.createImage("/anh3.PNG");
}
catch(Exception e){}
it1=new ImageItem("null",i1,ImageItem.LAYOUT_CENTER,null);
it2=new ImageItem("null",i2,ImageItem.LAYOUT_CENTER,null);
it3=new ImageItem("null ",i3,ImageItem.LAYOUT_CENTER,null);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable d) {
String t=tf.getString();
if(c==c1){
for(int i=0;i<=t.length();i++){
if(t.charAt(i)=='1'){
if(index1==-1)
index1=f.append(i1);
}
if(t.charAt(i)=='2'){
if(index2==-1)
index2=f.append(i2);
}
if(t.charAt(i)=='3'){
if(index3==-1)
index3=f.append(i3);
} } }
if(c==c2){
for(int i=0;i<=t.length();i++){
if(t.charAt(i)=='1'){
if(index1 !=-1){
f.delete(index1);
if(index2>index1) index2--;
if(index3>index1) index3--;
index1=-1;
}}
if(t.charAt(i)=='2'){
if(index2!=-1){
f.delete(index2);
if(index1>index2) index1--;
if(index3>index2) index3--;
index2=-1;
}}
if(t.charAt(i)=='3'){
if(index3 !=-1){
f.delete(index3);
if(index2>index3) index2--;
if(index1>index3) index1--;
index3=-1;
}} }}}}

Bi 2.8: Vit chng trnh c 3 TextField, 1 Form v 3 nt OK1, OK2, OK3. Ban u
Form khng c TextField no. Khi nhn nt OK1 th c 2 TextField hin th. Khi nhn
nt OK2 th c 3 TextField hin th. Khi nhn nt OK3 th khng c TextField no trn
Form.
package btc2;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class bai2_8 extends MIDlet implements CommandListener {
Display di;Form f;
TextField tf1,tf2,tf3;
Command c1,c2,c3,c4;
public void startApp() {
di=Display.getDisplay(this);
f=new Form("BAI2_8");
tf1=new TextField("textfield1","",20,TextField.ANY);
tf2=new TextField("textfield2","",20,TextField.ANY);
tf3=new TextField("textfield2","",20,TextField.ANY);
c1=new Command("ok1",Command.SCREEN,2);
c2=new Command("ok2",Command.SCREEN,2);
c3=new Command("ok3",Command.SCREEN,2);
c4=new Command("exit",Command.EXIT,1);
f.addCommand(c1);f.addCommand(c2);f.addCommand(c3);f.addCommand(c4);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable d) {
if(c==c4)
notifyDestroyed();
if(c==c1){
f.deleteAll();
f.append(tf1);
f.append(tf2);
}
if(c==c2){
f.deleteAll();
f.append(tf1);
f.append(tf2);
f.append(tf3);
}
if(c==c3)
f.deleteAll();
}}

Bi 2.9: Khi nhn nt nu trn Form c TextField th i thnh ChoiceGroup v ngc
li. hin th tn thnh phn c mt trn Form ln StringItem (nu trn Form c TextField
th hin th dng ch TextField)
package bai9;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Midlet extends MIDlet implements CommandListener{
Display di; Form f1; Command c1,c2; TextField tf1;
ChoiceGroup ch; StringItem it; int a;
public void startApp() {
di= Display.getDisplay(this);
f1= new Form ("hien thi");
c1= new Command("ok1",Command.OK,1);
c2= new Command("change",Command.OK,2);
tf1= new TextField("Tien Nham1",null, 10, TextField.ANY);
ch= new ChoiceGroup("nham", ChoiceGroup.EXCLUSIVE);
ch.append("abc", null);
it = new StringItem("Anh", null);
f1.addCommand(c1);
f1.append(tf1);
a=1;
f1.append(it);
di.setCurrent(f1);
f1.setCommandListener(this);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction( Command c, Displayable d){
if(c==c1){
f1.delete(0);
if(a==1) {
f1.insert(0, ch);
a=2;
it.setText("choiceGroup");
}
else
if(a==2){
f1.insert(0, tf1);
a=1;
it.setText("TextFeild");
}}}}
Bi 2.10: Nu nhn nt change th s TextField thnh ChoiceGroup v ngc li.
- Hin th kt qu ca php ton gia s hng 5 v 6 (php ton c nhp vo
TextField hoc c chn trong ChoiceGroup)
package choice;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Midlet extends MIDlet implements CommandListener,
ItemStateListener{
Display di;Form f; TextField tf1,tf2,tf3;
StringItem it;Command c1,c2;int key;
ChoiceGroup ch;
public void startApp() {
di=Display.getDisplay(this);f=new Form("as");
tf1=new TextField("so1","",20,TextField.NUMERIC);
tf2=new TextField("so2","",20,TextField.NUMERIC);
tf3=new TextField("phep toan","",20,TextField.ANY);
it=new StringItem("kq","");
ch=new ChoiceGroup("pheptoan",ChoiceGroup.EXCLUSIVE);
ch.append("cong", null);
ch.append("tru", null);
ch.append("nhan", null);
ch.append("chia", null);
c1=new Command("change",Command.OK,1);
c2=new Command("ok",Command.OK,1);
key=1;
f.append(tf1);f.append(tf2);f.append(tf3);f.append(it);
f.addCommand(c1);f.addCommand(c2);
f.setCommandListener(this);f.setItemStateListener(this);
di.setCurrent(f);
}
public void commandAction(Command c, Displayable d){
if(c==c1){
f.delete(2);
if(key==1)
{
f.insert(2, ch);
key=2;
}
else
if(key==2){
f.insert(2, tf3);
key=1;
}
}
}
public void itemStateChanged(Item item){
String t1=tf1.getString();
float so1=Float.parseFloat(t1);
String t2=tf2.getString();
float so2=Float.parseFloat(t2);
String t3;
int i;
float kq=0;
if(item==tf3)
{
t3=tf3.getString();
if(t3.equals("+"))
kq=so1+so2;
if(t3.equals("-"))
kq=so1-so2;
it.setText(String.valueOf(kq));
}
if(item==ch){
i=ch.getSelectedIndex();
if(i==2)
kq=so1*so2;
if(i==1)
kq=so1-so2;
it.setText(String.valueOf(kq));
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}}
Bi 2.11: Thc hin gn ri g nt nhn ra khi form theo mun
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Bai2_11 extends MIDlet implements CommandListener {
Display di;
Form f;
Command c1,c2,c3,c4;
StringItem st;
public void startApp() {
di = Display.getDisplay(this);
f = new Form("khung");
c1 = new Command("next1",Command.OK,1);
c2 = new Command("OK2",Command.OK,2);
//c3 = new Command("next3",Command.OK,3);
//c4 = new Command("OK4",Command.OK,4);
st = new StringItem(null,null);
f.append(st);
f.addCommand(c1);
f.addCommand(c2);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c,Displayable d){
if(c==c1){
f.removeCommand(c1);
st.setText("go bo nut bam c1");
}
if(c==c2){
f.addCommand(c1);
st.setText("gan c1 len khung");
}
if(c==c3){
f.removeCommand(c4);
st.setText("go bo nut bam c4");
}
if(c==c4){
f.addCommand(c4);
st.setText("gan c4 len khung");
}}}
Bi 2.12: Vit chng trnh Client cho php g ting vit (mc n gin)
package btc2;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class bai2_12 extends MIDlet implements ItemStateListener {
Display di; Form f;TextField tf;
public void startApp() {
di=Display.getDisplay(this);
f=new Form("BAI_2_12");
tf=new TextField("nhap_text","",100,TextField.ANY);
f.append(tf);
f.setItemStateListener(this);
di.setCurrent(f);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void itemStateChanged(Item item) {
String t=tf.getString();
String t1,t2;
if(t.indexOf("aa")!=-1){
t1=t.substring(0,t.indexOf("aa"));
//t2=t.substring(t.indexOf("aa"),t.length());
t=t1+"";
tf.setString(t);}
else
if(t.indexOf("aw")!=-1){
t1=t.substring(0,t.indexOf("aw"));
// t2=t.substring(t.indexOf("aw"),t.length());
t=t1+"";
tf.setString(t);}
else
if(t.indexOf("oo")!=-1){
t1=t.substring(0,t.indexOf("oo"));
// t2=t.substring(t.indexOf("oo"),t.length());
t=t1+"";
tf.setString(t);}
else
if(t.indexOf("ow")!=-1){
t1=t.substring(0,t.indexOf("ow"));
// t2=t.substring(t.indexOf("ow"),t.length());
t=t1+"";
tf.setString(t);}
else
if(t.indexOf("ee")!=-1){
t1=t.substring(0,t.indexOf("ee"));
//t2=t.substring(t.indexOf("ee"),t.length());
t=t1+"";
tf.setString(t);}
else
if(t.indexOf("dd")!=-1){
t1=t.substring(0,t.indexOf("dd"));
// t2=t.substring(t.indexOf("dd"),t.length());
t=t1+"";
tf.setString(t);}
else
if(t.indexOf(" ")!=-1) {
if(t.indexOf(" ")-1=='s'){
t1=t.substring(0,t.indexOf(" ")-1);
t=t1+"'";
tf.setString(t);} }
}}

Bi 2.13: Khi nhn nt add s cho thm thnh phn Item i, vi i nhp vo TextField.
Tng t vi nt delete. Tng kh ca bi khi ngi dng nhp vo mt chui s nh
345, 54 th nhng thnh phn c s th t tng t s c thm/xa
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Bai2_13 extends MIDlet implements CommandListener {
Display di;
Form f;
TextField tf1,tf2,tf3;
Command c1,c2;
Image i1;
ImageItem it1;
public void startApp() {
di = Display.getDisplay(this);
f = new Form("khung");
tf1 = new TextField("nhap","",200,TextField.NUMERIC);
tf2 = new TextField("","",200,TextField.ANY);
tf3 = new TextField("","",200,TextField.ANY);
c1 = new Command("ADD",Command.OK,1);
c2 = new Command("Delete",Command.OK,2);
try {
i1 = Image.createImage("d1.png");
it1 = new ImageItem("anh1",i1,ImageItem.LAYOUT_CENTER,null);
} catch (IOException ex) {
ex.printStackTrace();
}
f.append(tf1);
f.addCommand(c1);
f.addCommand(c2);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable d){
if(c==c1){
String t = tf1.getString();
tf1.setString("");
for(int i=0; i<t.length(); i++){
String t2 = t.substring(i, i+1);
int f1 = Integer.parseInt(t2);
switch (f1) {
case 1: f.append(tf2); break;
case 2: f.append(tf3); break;
case 3: f.append(it1); break;
} } }
if(c==c2){
String t = tf1.getString();
tf1.setString("");
for(int i=0; i<t.length();i++){
String t2 = t.substring(i,i+1);
int f2 = Integer.parseInt(t2);
switch (f2){
case 1: f.delete(1); break;
case 2: f.delete(2); break;
case 3: f.delete(3); break;
} } } }}
Bi 2.14: Nu nhn next th chng trnh s lu gi tr c li ni vo thnh mt chui c
k t , phn tch. Khi nhn nt ok th hin th chui ln mn hnh. Tng t vi
TextField.
package btc2;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class bai2_14 extends MIDlet implements CommandListener{
Display di;
Form f;
ChoiceGroup ch; StringItem st;
String t=" ";
Command c1,c2;
public void startApp() {
di=Display.getDisplay(this);
f=new Form("BAI 14");
ch=new ChoiceGroup("muc_chon_lua",ChoiceGroup.MULTIPLE);
ch.append("lua_chon1", null);
ch.append("lua_chon2", null);
ch.append("lua_chon3", null);
ch.append("lua_chon4", null);
f.append(ch);
c1=new Command("next",Command.EXIT,1);
c2=new Command("OK",Command.OK,2);
f.addCommand(c1);
f.addCommand(c2);
st=new StringItem("Hien_thi"," ");
f.append(st);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable d) {
if(c==c1){
for(int i=0;i<ch.size();i++){
if(ch.isSelected(i))
t=t+ch.getString(i)+":";
t=t.substring(0,t.length()-1);
t=t+";";
ch.setSelectedIndex(i, false);
}}
if(c==c2){
st.setText(t);
t=" ";
}}
Bi 2.15: Thc hnh gn cc chui con vo chui m thnh mt chui ln phn cch
nhau bi mt k t bt k (t chn). v ngc li.
package btchuong2;
import avax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class bai2_15 extends MIDlet implements CommandListener{
Display di; Form f;
TextField tf;Command c1,c2,c3;int i;
public void startApp() {
di=Display.getDisplay(this);
f=new Form("BAI 2_15");
tf=new TextField("nhap","",100,TextField.ANY);
c1=new Command("exit",Command.EXIT,1);
c2=new Command("ghepchuoi",Command.SCREEN,1);
c3=new Command("tachchuoi",Command.SCREEN,1);
f.append(tf);
f.addCommand(c1);f.addCommand(c2);f.addCommand(c3);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable d) {
String t=tf.getString();
if(c==c1)
notifyDestroyed();
if(c==c2){
f.append(t+";");
tf.delete(0, tf.size());
}
if(c==c3){
while(t.length()>0){
String t1=t.substring(0, t.indexOf(";"));
t=t.substring(t.indexOf(";")+1, t.length());
f.append("chuoi: "+t1);
}} }}
Bi 2.16: Thc hnh to ra mt chng trnh Client nhiu Form, thc hnh chuyn t
Form ny sang Form khc.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Bai2_16 extends MIDlet implements CommandListener {
Display di;
Form f1,f2,f3,f4,f5;
Command c1;
public void startApp() {
di = Display.getDisplay(this);
f1 = new Form("khung1 ");
f2 = new Form("khung2");
f3 = new Form("khung 3");
f4 = new Form("khung 4");
f5 = new Form("khung 5");
c1 = new Command("next",Command.OK,1);
f1.addCommand(c1);
f2.addCommand(c1);
f3.addCommand(c1);
f4.addCommand(c1);
f5.addCommand(c1);
f1.setCommandListener(this);
f2.setCommandListener(this);
f3.setCommandListener(this);
f4.setCommandListener(this);
f5.setCommandListener(this);
di.setCurrent(f1);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable d){
if(c==c1 && d==f1){
di.setCurrent(f2);
}
if(c==c1 && d==f2){
di.setCurrent(f3);
}
if(c==c1 && d==f3){
di.setCurrent(f4);
}
if(c==c1 && d==f4){
di.setCurrent(f5);
}
if(c==c1 && d==f5){
di.setCurrent(f1);
} }}
Bi 2.17: Vit chng trnh i mt chui k t sang cc m ascii v ngc li.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Bai2_17 extends MIDlet implements CommandListener {
Display di;
Form f;
TextField tf1;
StringItem st;
Command c;
public void startApp() {
di = Display.getDisplay(this);
f = new Form("khung");
tf1 = new TextField("nhap chuoi","",200,TextField.ANY);
st = new StringItem("chuoi vua nhap co ma ACSII",null);
c = new Command("conv",Command.OK,1);
f.append(tf1);
f.append(st);
f.addCommand(c);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable d){
int a[] = new int[100];
String t = tf1.getString();
for(int i=0; i<t.length(); i++){
a[i]= (int)t.charAt(i);
}
for(int i=0; i<t.length(); i++){
String t1 = String.valueOf(a[i]);
f.append(t1);
} }}
Bi 2.18: Vit chng trnh cho php ngi dng nhp cc k t vo trong 1 TextField,
nhng khi ngi dng nhp n k t th 10 th thng bo khng c nhp na.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Bai2_18 extends MIDlet implements ItemStateListener {
Display di;
Form f;
TextField tf1;
StringItem st;
public void startApp() {
di = Display.getDisplay(this);
f =new Form("so");
tf1 = new TextField("nhap","",200,TextField.ANY);
st = new StringItem(null,null);
f.append(tf1);
f.append(st);
f.setItemStateListener(this);
di.setCurrent(f); }
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void itemStateChanged(Item it){
String t = tf1.getString();
for(int i=0; i<t.length();i++){
if( i>9){
st.setText("khong cho phep nhap nua");
String t1 =t.substring(0,9);
tf1.setMaxSize(10);
} } }}

Chng 3
Bi 3.1: Vit Client cho php login qua mng, Client gi ln Server tn ti khon v
password i nhn kt qu tr v v hin th ln mn hnh.
Bi 3.2: Vit Client cho php thc hin tnh kt qu php ton qua mng. Client gi ln
server hai s hng v mt php ton. Client i nhn kt qu t Server v v hin th ln
mn hnh.
Bi 3.3: Vit Client tra t in, client gi ln server t cn tra, server tr v chui t tra
c.
Bi 3.4: Vit chng trnh c 1 TextField v 4 nt nhn. Mi nt nhn cho php ngi
dng gi chui trong TextField ln mt server khc nhau. Kt qu tr v l mt chui cn
c hin th trn mn hnh.
Bi 3.5: Vit chng trnh Client c 1 TextField (ChoiceGroup), mt nt next v mt nt
ok.
- Chng trnh cn gi 2 chui ln server
- Khi ngi dng nhp/ chn song ni dung 1 v nhn next th lu ni dung ny li
- Khi ngi dng nhp/chn song ni dung ln 2 v nhn ok th gi 2 ni dung ln
server v hin th kt qu nhn c.
package chuong3;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Bai3_5 extends MIDlet implements CommandListener, Runnable{
Display di;
Form f;
TextField tf1;
Command c1,c2;
String url;
HttpConnection con;
InputStream in;
Thread th;
String t1,t2;
StringItem it;
public void startApp() {
di =Display.getDisplay(this);
f = new Form("khung");
tf1 = new TextField("nhap","",50,TextField.ANY);
c1 = new Command("next",Command.OK,1);
c2 = new Command("OK1",Command.OK,2);
it = new StringItem(null,null);
f.append(tf1);
f.addCommand(c1);
f.addCommand(c2);
f.append(it);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {}

public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c , Displayable di){
th = new Thread(this);
if(c==c1){
t1 = tf1.getString();
tf1.setString("");
}
if(c==c2){
t2 = t1+tf1.getString();
url = "http://localhost:8080/Bai3_5/Bai3_5?chuoi="+t2;
th.start();
} }
public void run(){
try {
con = (HttpConnection) Connector.open(url);
con.setRequestMethod(HttpConnection.GET);
in = con.openInputStream();
ByteArrayOutputStream data= new ByteArrayOutputStream();
int ch;
while((ch=in.read())!=-1){
data.write(ch);
byte b[]= new byte[100];
b = data.toByteArray();
String result = new String(b);
//f.append(result);
System.out.println(result);
it.setText(result);
}
} catch (IOException ex) {
ex.printStackTrace();
} }}
Bi 3.6: Vit chng trnh Client c 1 TextField v 2 nt nhn ok1, ok2:
Nhn vo ok 1 th chng trnh cho php gi ni dung ln 1 server v nhn kt qu tr v
- Nhn vo nt ok2 th chng trnh cho php kt ni n a ch trong textfield.
Client
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
public class bai6 extends MIDlet implements Runnable, CommandListener{
Display di;
Form f;
Command c1,c2;
TextField tf;
String url;
Thread th;
HttpConnection con;
InputStream in;
public void startApp() {
di= Display.getDisplay(this);
f= new Form("bai 6");
tf= new TextField ("nhap","",50,TextField.ANY);
c1= new Command("ok1",Command.OK,1);
c2= new Command ("ok2",Command.OK,1);
f.append(tf);
f.addCommand(c1);
f.addCommand(c2);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void run() {
try{
con=(HttpConnection) Connector.open(url);
con.setRequestMethod(HttpConnection.GET);
in= con.openInputStream();
ByteArrayOutputStream data= new ByteArrayOutputStream();
int ch;
while ((ch= in.read()) !=-1)
{
data.write(ch);
}
byte b[]= new byte[100];
b=data.toByteArray();
String ht= new String(b);
f.append(ht);
}
catch (Exception e) {System.out.print(e.toString());}
}
public void commandAction(Command c, Displayable d) {
th= new Thread (this);
th.start();
if (c==c1) {
url="http://localhost:8080/mc_bai6_chuong3/bai6?xau=lien";
}
if (c==c2) {
url=tf.getString();
} }}
-------------------Sever-------------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class bai6 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String xau;
xau= req.getParameter("xau");
PrintWriter out= res.getWriter();
out.print("da ket noi thanh cong");
} }
Bi 3.7: Chng trnh cn gi ln Server 2 tham s, v vy ngi dng nhp c 2 tham s
v trong TextField (c k t phn tch), chng trnh s thc hin tch 2 tham s trong
TextField ra v gi ln Client
Client--------------------------------------------------------
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
public class bai7 extends MIDlet implements Runnable, CommandListener{
Display di;
Form f;
TextField tf;
Command c1;
HttpConnection con;
InputStream in;
Thread th;
String url;
public void startApp() {
di= Display.getDisplay(this);
f= new Form ("bai 7");
tf= new TextField ("nhap","",20,TextField.ANY);
c1= new Command ("ok",Command.OK,1);
f.append(tf);
f.addCommand(c1);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {
}
public void destroyApp(boolean a) {
}
public void run() {
try{
url="http://localhost:8080/mc_bai7_chuong3/bai7?xau="+tf.getString();
con= (HttpConnection) Connector.open(url);
con.setRequestMethod(HttpConnection.GET);
in=con.openInputStream();
ByteArrayOutputStream data= new ByteArrayOutputStream();
int ch;
while ( (ch=in.read()) !=-1)
data.write(ch);
byte b[]= new byte[100];
b=data.toByteArray();
String kq= new String(b);
f.append(kq);
}catch (Exception e) {System.out.print(e.toString());
}}
public void commandAction(Command c, Displayable d) {
th= new Thread (this);
th.start();
} }
Sever---------------------------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class bai7 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String xau;
xau= req.getParameter("xau");
PrintWriter out= res.getWriter();
xau= xau.substring(0, xau.indexOf(",")) +"\n "+ xau.substring((xau.indexOf(",") +1
), xau.length());
out.print(xau);
} }
Bi 3.8: Vit chng trnh cho php ngay khi khi ng tin hnh kt ni n 1 server
bt k nhn kt qu gi v v hin th.
Bi 3.9: Vit chng trnh client c nhiu TextField v mt s nt nhn, mi nt nhn
thc hin ghp ni dung ca 2 hay nhiu TextField gi ln Server
Bi 3.10: Thc hnh Client c nhiu nt nhn mi nt kt ni n mt server ring nhn
kt qu tr v khc nhau.
Bi 3.11: Vit chng trnh kt ni n server 2 ln, ln 1 sau khi kt ni s nhn c
mt s, ln 2 gi s nhn c t ln 1 v mt chui nhp trong TextField ln server.
Client----------------------------------
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
public class bai11 extends MIDlet implements Runnable, CommandListener {
Display di;
Form f;
TextField tf;
Command c1,c2;
HttpConnection con;
Thread th;
InputStream in;
String url="";
String so="";
public void startApp(){
di= Display.getDisplay(this);
f= new Form ("bai11");
tf= new TextField ("nhap","",50,TextField.ANY);
c1= new Command ("ok1",Command.OK,1);
c2= new Command ("ok2",Command.OK,1);
f.append(tf);
f.addCommand(c1);
f.addCommand(c2);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) { }
public void run() {
try {
con=(HttpConnection) Connector.open(url);
con.setRequestMethod( HttpConnection.GET);
in= con.openInputStream();
ByteArrayOutputStream data= new ByteArrayOutputStream();
int ch;
while ( (ch= in.read()) != -1)
data.write(ch);
byte b[]= new byte[100];
b=data.toByteArray();
String kq= new String(b);
f.append("client nhan duoc " + kq);
so=kq;
} catch (Exception e) {
} }
public void commandAction(Command c, Displayable d) {
if (c==c1) {
url="http://localhost:8080/mc_bai11_chuong3/bai11?xau=lien";
th= new Thread(this);
th.start();
}
if (c==c2) {
url="http://localhost:8080/mc_bai11_chuong3/bai11?xau=1"+tf.getString();
th= new Thread(this);
th.start();
} }}
Sever---------------------------------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class bai11 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter p= res.getWriter();
String xau = req.getParameter("xau");
while (true) {
if (xau.equals("lien"))
{
p.print("1");
// p.print("\t");
}
else
p.print("server da nhan dc" + ":" +xau);
} }}
Bi 3.12: Vit chng trnh c 3 form, trn mi form c 1 TextField, 1 nt next, form3
c nt OK. u tin form1 c hin th. Nt next s cho php chuyn t ca s 1-2, 2-3,
ng thi lu li cc gi tr nhp trong cc textfield ca cc form i qua. Nt ok
cho php gi cc gi tr nhp trong cc TextField ln server. Thc hnh vi c
ChoiceGroup.
Bi 3.13: Thc hnh gi ln Server nhiu d liu nhng c gn trong mt chui, ng
thi mi d liu c gn mt s hiu ng trc. Server s tch cc tham s d liu ra,
sau tch cc s hiu ra.
Client--------------------------------------
package hello;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.lcdui.*;
public class bai13 extends MIDlet implements Runnable, CommandListener{
Display di;
Form f;
TextField tf;
Command c1;
HttpConnection con;
InputStream in;
Thread th;
public void startApp() {
di= Display.getDisplay(this);
f= new Form("bai13");
tf= new TextField ("nhap","",9,TextField.ANY);
c1= new Command("ok",Command.OK,1);
f.append(tf);
f.addCommand(c1);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void run() {
// throw new UnsupportedOperationException("Not supported yet.");// CO CAI
NAY KO CHAY PHAN DUOI'
try{
String url="http://localhost:8080/mc_bai13_chuong3/bai13?xau="+ tf.getString();
con=(HttpConnection)Connector.open(url);
con.setRequestMethod(HttpConnection.GET);
in=con.openInputStream();
ByteArrayOutputStream data= new ByteArrayOutputStream();
int ch;
while((ch=in.read()) !=-1)
data.write(ch);
byte b[]= new byte[40];
b=data.toByteArray();
String kq= new String(b);
f.append(kq);
}catch(Exception e){}
}
public void commandAction(Command c, Displayable d) {
// throw new UnsupportedOperationException("Not supported yet."); // CO CAI
NAY KO CHAY PHAN DUOI'
if (c==c1) {
th= new Thread(this) ;
th.start();
} }}
Sever-----------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class bai13 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { // CAI NAY PHAI CO
// processRequest(request, response); // KO CO CAI NAY THI CHAY VI NO O
PHUONG THUC KHAC
PrintWriter p= res.getWriter();
String xau= req.getParameter("xau");
xau= xau.trim();
char kitu;
char[]tg1 = new char [2];
char[]tg2 = new char[2];
int k=0 , m=0;
for (int i=0;i<xau.length();i++) {
kitu = xau.charAt(i);
if ( (int) kitu >=48 && (int) kitu <=57) { // KO CO (INT) CUNG DC NO TU
HIEU SANG ASCII
tg1[k]=kitu;
k=k+1;
}
else {
tg2[m]=kitu;
m=m+1;
}}
p.print("ma server nhan duoc la:");
p.print(tg1);
p.print("du lieu server nhan duoc la:");
p.print(tg2);
}}
Bi 3.14: Thc hnh Client gi ln server cc tham s tch bit nhau. Hoc Client gi ln
server nhiu gi tr nhng c gn vo cng mt tham s.
Client------------------------------
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

public class bai14 extends MIDlet implements Runnable, CommandListener {
Display di;
Form f;
TextField tf;
Command c1;
HttpConnection con;
InputStream in;
Thread th;
String mang[]= new String[20];
String url;
public void startApp() {
di= Display.getDisplay(this);
f= new Form ("bai 14");
tf= new TextField ("nhap","",20,TextField.ANY);
c1= new Command ("ok1",Command.OK,1);
f.append(tf);
f.addCommand(c1);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {}
public void run() {
try{
con= (HttpConnection ) Connector.open(url);
con.setRequestMethod(HttpConnection.GET);
in=con.openInputStream();
ByteArrayOutputStream data= new ByteArrayOutputStream();
int ch;
while ( (ch= in.read()) != -1 )
data.write(ch);
byte b[]= new byte[20];
b=data.toByteArray();
String kq= new String(b);
f.append(kq);

}catch(Exception e) {System.out.print(e.toString()); }
}
public void commandAction(Command c, Displayable d) {
if (c==c1) {
url= "http://localhost:8080/mc_bai14_chuong3/bai14?xau=" +tf.getString();
th= new Thread(this);
th.start();
tf.setString("");
} }}
Sever------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class bai14 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter p= res.getWriter();
String xau = req.getParameter("xau");
p.print("server nhan duoc cac gia tri" + xau);
}}
Bi 3.15: Thc hnh Server gi v cho Client nhiu gi tr c gn trong mt chui, cc
gi tr c phn tch bi mt k t phn tch no .
Bi 3.16: Thc hin kt ni n c s d liu. sau thc hin cc thao tc so khp xem
c d liu no ging d liu a ra khng.
Bi 3.17: Thc hin kt ni n CSDL, sau tm d liu theo mt yu cu no .
Sach
MaS TenS MaNXB TheLoai


NXB
MaNXB TenNXB DienThoai


ThongKe
ID maS TinhTrang SoLuong




a) Tm sch thuc th loi Vn hc ca nh xut bn Lao ng
b) Tm cc sch ca nh xut bn Thng k
c) Tm sch c s lng ln hn 1000
d, Vit chng trnh Server nhn cc tham s t Client sau tm kim trong CSDL
v gi kt qu cho Client theo cc a,b,c.
client---------------------------------
package hello;
import javax.microedition.midlet.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
public class bai17 extends MIDlet implements Runnable, CommandListener{
Display di;
Form f;
Command c1,c2,c3;
HttpConnection con;
InputStream in;
Thread th;
String url;
String chuoi;
public void startApp() {
di= Display.getDisplay(this);
f= new Form ("bai17");
c1= new Command ("ok1",Command.OK,1);
f.addCommand(c1);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) { }
public void run() {
try{

url="http://localhost:8080/mc_bai17_chuong3/bai17?xau1=vanhoc&xau2=laodong&xau
3=thongke&xau4=1000";
con=(HttpConnection ) Connector.open(url);
con.setRequestMethod(HttpConnection.GET);
in=con.openInputStream();
ByteArrayOutputStream data= new ByteArrayOutputStream();
int ch;
while ( (ch=in.read()) != -1)
data.write(ch);
byte b[]= new byte[100];
b=data.toByteArray();
String kq= new String(b);
f.append(kq);
}catch(Exception e) {System.out.print(e.toString());}
}
public void commandAction(Command c, Displayable d) {
if(c==c1) {
th= new Thread(this);
th.start();
}}}
Sever-----------------------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import java.sql.*;
public class bai17 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String xau1= req.getParameter("xau1");
String xau2= req.getParameter("xau2");
String xau3= req.getParameter("xau3");
String xau4= req.getParameter("xau4");
try{
PrintWriter p= res.getWriter();
String url,sql1,sql2,sql3,user,pass,driver;
driver="net.sourceforge.jtds.jdbc.Driver";
url="jdbc:jtds:sqlserver://localhost:1433/BAI17";
sql1="select * from SACH inner join NXB on SACH.maNXB=NXB.maNXB
where theloai='"+xau1+"' and tenNXB='"+xau2+"'";
sql2="select * from SACH inner join NXB on SACH.maNXB = NXB.maNXB
where tenNXB='"+xau3+"'";
sql3="select * from SACH inner join THONGKE1 on SACH.maS =
THONGKE1.maS where soluong >'"+xau4+"'";
user="lien"; pass= "lien";
Class.forName(driver).newInstance();
Connection con= DriverManager.getConnection(url, user, pass);
Statement st= con.createStatement();
ResultSet rs= st.executeQuery(sql1);
if (rs.next()) {
p.print("KET QUA PHAN A: "+"\n");
for(int i=1;i<=7;i++)
p.print(rs.getString(i));
}
p.print("\n");
rs=st.executeQuery(sql2);
if (rs.next()) {
p.print("KET QUA PHAN B:"+"\n");
for(int i=1;i<=7;i++)
p.print(rs.getString(i));}
p.print("\n");
rs=st.executeQuery(sql3);
if (rs.next()) {
p.print("KET QUA PHAN C: "+"\n");
for(int i=1;i<=7;i++)
p.print(rs.getString(i));}
p.print("\n");
}catch(Exception e) {System.out.print(e.toString());
} }}
Bi 3.18: Thc hin kt ni n CSDL, sau thm d liu vo CSDL
SinhVien
ID hoten Gioitinh Nam_sinh


MonHoc
ID Ten_mon SoTC


Diem
ID mamon maSV Diem


a, Thm im ton cho sinh vin c m l sv01 vi s im l 10
b, Thm mn hc vo bng mn hc
c, Thm sinh vin vo bng sinh vin
d, Vit chng trnh Server nhn cc tham s t Client sau thm d liu vo trong
CSDL v gi kt qu thc hin v cho Client theo cc a,b,c.
-----------------client-----------------------
package hello;
import javax.microedition.midlet.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
public class bai18 extends MIDlet implements Runnable, CommandListener{
Display di;
Form f;
Command c1,c2,c3;
HttpConnection con;
InputStream in;
Thread th;
String url;
String chuoi;
public void startApp() {
di= Display.getDisplay(this);
f= new Form ("bai18");
c1= new Command ("ok1",Command.OK,1);
f.addCommand(c1);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void run() {
try{
url="http://localhost:8080/mc_bai18_chuong3/bai18?xau1=sv01&xau2=10&xau3=toan&
xau4=1&xau5=nguyenlien";
con=(HttpConnection ) Connector.open(url);
con.setRequestMethod(HttpConnection.GET);
in=con.openInputStream();
ByteArrayOutputStream data= new ByteArrayOutputStream();
int ch;
while ( (ch=in.read()) != -1)
data.write(ch);
byte b[]= new byte[100];
b=data.toByteArray();
String kq= new String(b);
f.append(kq);
}catch(Exception e) {System.out.print(e.toString());
} }
public void commandAction(Command c, Displayable d) {
if(c==c1) {
th= new Thread(this);
th.start();
}}}
-------------------sever---------------


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import java.sql.*;
public class bai18 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String xau1= req.getParameter("xau1");
String xau2= req.getParameter("xau2");
String xau3= req.getParameter("xau3");
String xau4= req.getParameter("xau4");
String xau5= req.getParameter("xau5");
try{
PrintWriter p= res.getWriter();
String url,sql,sql1,sql2,sql3,user,pass,driver;
driver="net.sourceforge.jtds.jdbc.Driver";
url="jdbc:jtds:sqlserver://localhost:1433/BAI18";
sql1="insert into DIEMSV values (1,'TOAN','"+xau1+"','"+xau2+"')";
sql2="insert into MONHOC values ('1','toan','2')";
sql3="insert into SINHVIEN values ('"+xau4+"','"+xau5+"','nu','1991')";
user="lien"; pass="lien";
Class.forName(driver).newInstance();
Connection con= DriverManager.getConnection(url, user, pass);
Statement st= con.createStatement();
int i= st.executeUpdate(sql3);
if (i>0) p.print("PHAN C OK ");
else p.print("ERROR");
p.print("\n");
i=st.executeUpdate(sql1);
if(i>0) p.print("PHAN A OK");
else p.print("ERROR");
p.print("\n");
i= st.executeUpdate(sql2);
if(i>0) p.print("PHAN B OK");
else p.print("ERROR");
p.print("\n");
}catch(Exception e) {System.out.print(e.toString());
} }}
Bi 3.19: Thc hin kt ni n CSDL, sau xa d liu vo CSDL
SinhVien
ID hoten Gioitinh Nam_sinh


MonHoc
ID Ten_mon SoTC


Diem
ID mamon maSV Diem


a, Xa sinh vin c tn l Nguyn Vn A
b, Xa mn hc ABC
c, Xa im mn A ca sinh vin B
d, Vit chng trnh Server nhn cc tham s t Client sau xa thng tin trong CSDL
v gi kt qu thc hin cho Client theo cc a,b,c.
Bi 3.20: Thc hin kt ni n CSDL, sau sa d liu trong CSDL
SinhVien
ID hoten Gioitinh Nam_sinh


MonHoc
ID Ten_mon SoTC


Diem
ID mamon maSV Diem


a, Sa im ca sinh vin c m l X hc mn Y thnh im Z
b, Sa s tn ch ca mn hc X thnh Y
c, Tnh im trung bnh ca sinh vin A
d, Vit chng trnh Server nhn cc tham s t Client sau sa thng tin trong CSDL
v gi kt qu thc hin cho Client theo cc a,b,c.
-------------client-------------------
package hello;
import javax.microedition.midlet.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
public class bai20 extends MIDlet implements Runnable, CommandListener{
Display di;
Form f;
Command c1,c2,c3;
HttpConnection con;
InputStream in;
Thread th;
String url;
String chuoi;
public void startApp() {
di= Display.getDisplay(this);
f= new Form ("bai20");
c1= new Command ("ok1",Command.OK,1);
f.addCommand(c1);
f.setCommandListener(this);
di.setCurrent(f);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void run() {
try{
url="http://localhost:8080/mc_bai20_chuong3/bai20?xau1=01&xau2=u&xau3=b";
con=(HttpConnection ) Connector.open(url);
con.setRequestMethod(HttpConnection.GET);
in=con.openInputStream();
ByteArrayOutputStream data= new ByteArrayOutputStream();
int ch;
while ( (ch=in.read()) != -1)
data.write(ch);
byte b[]= new byte[100];
b=data.toByteArray();
String kq= new String(b);
f.append(kq);
}catch(Exception e) {System.out.print(e.toString());}
}
public void commandAction(Command c, Displayable d) {
if(c==c1) {
th= new Thread(this);
th.start();
}}}
-------------------sever-----------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import java.sql.*;
public class bai20 extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String xau1= req.getParameter("xau1");
String xau2= req.getParameter("xau2");
String xau3= req.getParameter("xau3");
try{
PrintWriter p= res.getWriter();
String url,sql,sql1,sql2,sql3,user,pass,driver;
driver="net.sourceforge.jtds.jdbc.Driver";
url="jdbc:jtds:sqlserver://localhost:1433/BAI20";
sql1="select *from DIEM where MASV='"+xau1+"'";
sql2="select * from MONHOC where TENMON='"+xau2+"'";
sql3="select DIEM.DIEM from DIEM where ID in (SELECT SINHVIEN.ID from
SINHVIEN where HOTEN='"+xau3+"')";
user="lien"; pass="lien";
Class.forName(driver).newInstance();
Connection con= DriverManager.getConnection(url, user, pass);
Statement st= con.createStatement();
ResultSet rs=st.executeQuery(sql1);
if (rs.next()) {
sql1="update DIEM set DIEM='3' where MASV='"+xau1+"'";
int i=st.executeUpdate(sql1);
sql1="select * from DIEM";
ResultSet rs1= st.executeQuery(sql1);
if (i>0) {
rs1.next();
p.print("ok");
}
rs=st.executeQuery(sql2);
if (rs.next()) {
sql2="update MONHOC set SOTINCHI=5 where TENMON='"+xau2+"'";
int j =st.executeUpdate(sql2);
sql2="select * from DIEM";
ResultSet rs2= st.executeQuery(sql2);
if (i>0) {
rs2.next();
p.print("ok");
} }
rs=st.executeQuery(sql3); float k=0; int m= 0;
if (rs.next()) {
k= k+ Float.parseFloat(rs.getString(1));
m=m+1;
}
k=k+Float.parseFloat(rs.getString(1));
p.print(k/m);
}}catch(Exception e) {System.out.print(e.toString());
} }}
Bi 3.21: Vit chng trnh Server cho php tch chui gi ln t Client (chui c k t
phn tch), sau kim tra gi tr ca tham s th nht thao tc trn 2 tham s cn li.
------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class b21 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String t=request.getParameter("para");
String t1,t2,t3;
float s1,s2,s3=0;
t1=t.substring(0, t.indexOf(":"));
t=t.substring(t.indexOf(":")+1, t.length());
t2=t.substring(0,t.indexOf(":"));
t3=t.substring(t.indexOf(":")+1, t.length());
s1=Float.parseFloat(t2);
s2=Float.parseFloat(t3);
if(t1.equals(" ")) s3=s1+s2;
if(t1.equals("-")) s3=s1-s2;
out.print(s3);
} }
Bi 3.22: Vit chng trnh Server cho php sp xp mt chui s c gi ln t Client
(cc s c phn tch nhau bi du cch)
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class b22 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String t=request.getParameter("para");
t=t+" "; int s[];
s=new int[10];
int i,k,j,c; i=0; String t1,t2;
while(t.length()>0){
t1=t.substring(0, t.indexOf(" "));
t=t.substring(t.indexOf(" ")+1, t.length());
int so=Integer.parseInt(t1);
s[i]=so;
i++;
}
k=i;
for(i=0;i<k-1;i++)
for(j=i+1;j<k;j++)
if(s[i]>s[j])
{
c=s[i];s[i]=s[j];s[j]=c;
}
for(i=0;i<k;i++)
out.print(s[i]);
}}
Bi 3.23: Vit chng trnh Server cho php chn thm mt k t vo chui do client gi
ln v gi li chui c sa.
Bi 3.24: Vit chng trnh Server nhn c t chng trnh Client chui
x1y1x2y2x3y3x4y4;
Trong x l s nguyn m t s k t ca y: v d 1a2bc3def
Tch cc k t ra khi chui s to thnh mt chui mi:a bc def
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class b24 extends HttpServlet {
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- String t=request.getParameter("para");
- String t1,t2;
- int i=0;
- while(t.length()>0){
- t1=String.valueOf(t.charAt(0));
- i=Integer.parseInt(t1);
- t=t.substring(1, t.length());
- t2=t.substring(0,i);
- t=t.substring(i, t.length());
- out.println(t2);
- } } }
Bi 3.25: Thc hin bi 3.24 vi y l cc s
Bi 3.26: Thc hin bi 3.24 nhng x l mt chui ring, y l mt chui ring
Baif 3.27: Thc hin ngc li bi 3.24 ngha l i: h ni vit nam thnh
1ha3noi4viet3nam;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class b27 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String t=request.getParameter("para");
String t1,t2,x="";
int i=0;
t=t+" ";
while(t.indexOf(" ")!=-1){
i=t.indexOf(" ");
t1=t.substring(0, i);
x=x+String.valueOf(t1.length())+t1;
t=t.substring(i+1,t.length());
}
out.print(x);
} }