kiem tra j2me

Màu nền
Font chữ
Font size
Chiều cao dòng

1.      client gửi một từ tiếng anh, Server nhận được từ này thì dịch ra tiếng việt và trả lại kết quả là từ tiếng việt để client hiển thị (minh họa bằng từ điển 10 từ - nghĩa là server chỉ có khả năng dịch 10 từ cố định)3%

2.     client có 3 lựa chọn: tắt máy, reset máy, logoff máy, người dùng chọn tùy chọn nào thì client sẽ gửi lệnh tương ứng lên server, server nhận được lệnh sẽ tiến hành tắt, reset hoặc logoff máy tùy vào lệnh nhận được.(t7)19%

3.      client gửi lên server thông tin là: mã sinh viên, mã môn học, server nhận được thông tin thì trả lại kết quả là điểm tổng kết của môn học cho client hiển thị (minh họa bằng 5 môn học – nghĩa là server chỉ lưu thông tin điểm của 5 môn của 5 sinh viên cố định) (13)34%

4.      server có một danh sách:

Client sẽ gửi thông tin là: username (person1) lên server, server sẽ duyệt danh sách, lấy ra các thông tin person2, status với person1 là username gửi về client, client hiển thị danh sách một cách dễ hiểu nhất(mô phỏng cách lấy danh sách bạn bè về khi chat)(20)50%

5.      client gửi ba mã màu lên server, server nhận được mã màu điện trở, sẽ đổi mã màu điện trở sang màu (0: đen), gửi tên ba màu theo thứ tự về cho client hiển thị.(26)66%

6.     client gửi một số thập phân (là một số nguyên hoặc số thực) lên server, server đổi số thập phân ra nhị phân rồi gửi số nhị phân đó về cho client hiển thị. Nếu client gửi số âm, hoặc một số nằm ngoài khoảng server của em có thể xử lý, server của em phải thông báo về client để client hiển thị thông báo đến người dùng.(31)81%

Câu 1

Client

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import javax.microedition.lcdui.*;

import java.io.*;

/**

 * @author Capkama

 */

public class Cau1Mobile extends MIDlet implements CommandListener,Runnable {

    HttpConnection con;

    InputStream in;

    OutputStream out;

    Thread th;

    Form mf;

    Display ds = Display.getDisplay(this);

    TextField t1,t2;

    StringItem st,st2;

    Command c1,c2;

    String url;

public Cau1Mobile(){

    th=new Thread(this);

    mf=new Form("Dịch từ");

    t1 = new TextField("Tiếng Anh:","",10,TextField.ANY);

    st = new StringItem("Tiếng Việt: ","");

    st2=new StringItem("","Dịch được 10 từ summer,autumn,winter,spring,student,pupil,baby,cat,dog,leon");

    c1=new Command("Connect",Command.OK,1);

    c2=new Command("Exit",Command.EXIT,0);

    mf.append(t1);

    mf.append(st2);

    mf.append(st);

    mf.addCommand(c1);

    mf.addCommand(c2);

    mf.setCommandListener(this);

}

    public void startApp() {

        ds.setCurrent(mf);

    }

    public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {

    }

    public void connect(){

                        try{

                    con=(HttpConnection)Connector.open(url);

                    con.setRequestMethod(HttpConnection.GET);

                    con.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");

                    con.setRequestProperty("Content-Language", "en-US");

                    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                        }

                        catch(Exception e){

                    st.setText("Kết nối lỗi: "+e.getMessage());

                        }

    }

    public void in(){

            String;

                        try{

                                    in=con.openInputStream();

                                    int length=(int)con.getLength();

                                    byte server[];

                                    if(length!=-1){

                                                server=new byte[length];

                                                in.read(server);

                                                str=new String(server);

                                    }

                                    else

                                    {

                                                ByteArrayOutputStream br=new

                                ByteArrayOutputStream();

                                                int ch;

                                                while((ch=in.read())!=-1)

                                                br.write(ch);

                                                str=new String(br.toByteArray());                               

                                                br.close();

                        }

                        st.setText(str);

                        }

                        catch(Exception e){

                        }

            }

        public synchronized void run(){

            this.connect();

            this.in();

        }

    public void commandAction(Command c, Displayable d)

    {

        if(c==c1)

        {

       +t1.getString()+"";

        th=new Thread(this);

        th.start();

        }

    }

}

Sever

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

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;

/**

 *

 * @author Capkama

 */

@WebServlet(name = "Cau1Web", urlPatterns = {"/Cau1Web"})

public class Cau1Web extends HttpServlet {

    /**

     * Processes requests for both HTTP GET and POST methods.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        try {

            /* TODO output your page here

            out.println("");

            out.println("");

            out.println("Servlet Cau1Web"); 

            out.println("");

            out.println("");

            out.println("

Servlet Cau1Web at " + request.getContextPath () + "

");

            out.println("");

            out.println("");

             */

        } finally {            

            out.close();

        }

    }

    /**

     * Handles the HTTP GET method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String kt = request.getParameter("TA");

        PrintWriter out = response.getWriter();

        String eng[]={"summer","autumn","winter","spring","student","pupil","baby","cat","dog","leon"};

        String vi[]={"Mùa hè","Mùa thu","Mùa đông","Mùa xuân","Sinh viên","Học sinh","Em bé","Con mèo","Con chó","Con sư tử"};

        for(int i=0;i<10;i++)

        {

            if(kt.equals(eng[i]))

                out.print(vi[i]);

        }

    }

    /**

     * Handles the HTTP POST method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }

    /**

     * Returns a short description of the servlet.

     * @return a String containing servlet description

     */

    @Override

    public String getServletInfo() {

        return "Short description";

    }//

}

Câu 2

Client

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import javax.microedition.lcdui.*;

import java.io.*;

/**

 * @author Capkama

 */

public class Cau2Mobile extends MIDlet implements CommandListener,Runnable {

    HttpConnection con;

    InputStream in;

    OutputStream out;

    Thread th;

    Form mf;

    Display ds = Display.getDisplay(this);

    TextField t1,t2;

    StringItem st;

    Command c1,c2;

    String url;

public Cau2Mobile(){

    th=new Thread(this);

    mf=new Form("Điều khiển server");

    t1 = new TextField("Lệnh:","",10,TextField.ANY);

    st = new StringItem("Connect","");

    c1=new Command("Connect",Command.OK,1);

    c2=new Command("Exit",Command.EXIT,0);

    mf.append(t1);

    mf.append(st);

    mf.addCommand(c1);

    mf.addCommand(c2);

    mf.setCommandListener(this);

}

    public void startApp() {

        ds.setCurrent(mf);

    }

    public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {

    }

    public void connect(){

                   try{

                    con=(HttpConnection)Connector.open(url);

                    con.setRequestMethod(HttpConnection.GET);

                    con.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");

                    con.setRequestProperty("Content-Language", "en-US");

                    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                   }

                   catch(Exception e){

                    st.setText("Kết nối lỗi: "+e.getMessage());

                   }

    }

    public void in(){

            String;

                   try{

                             in=con.openInputStream();

                             int length=(int)con.getLength();

                             byte server[];

                             if(length!=-1){

                                      server=new byte[length];

                                      in.read(server);

                                      str=new String(server);

                             }

                             else

                             {

                                      ByteArrayOutputStream br=new

                                ByteArrayOutputStream();

                                      int ch;

                                      while((ch=in.read())!=-1)

                                      br.write(ch);

                                      str=new String(br.toByteArray());                               

                                      br.close();

                        }

                        st.setText(str);

                   }

                   catch(Exception e){

                   }

          }

        public synchronized void run(){

            this.connect();

            this.in();

        }

    public void commandAction(Command c, Displayable d)

    {

        if(c==c1)

        {

       +t1.getString()+"";

        th=new Thread(this);

        th.start();

        }

    }

}

Sever

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

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;

/**

 *

 * @author Capkama

 */

@WebServlet(name = "Cau2Web", urlPatterns = {"/Cau2Web"})

public class Cau2Web extends HttpServlet {

    /**

     * Processes requests for both HTTP GET and POST methods.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        try {

            /* TODO output your page here

            out.println("");

            out.println("");

            out.println("Servlet Cau2Web"); 

            out.println("");

            out.println("");

            out.println("

Servlet Cau2Web at " + request.getContextPath () + "

");

            out.println("");

            out.println("");

             */

        } finally {           

            out.close();

        }

    }

    /**

     * Handles the HTTP GET method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String kt = request.getParameter("Ycau");

        PrintWriter out = response.getWriter();

        if(kt.equals("shutdown"))

        {

           Runtime.getRuntime().exec("shutdown.exe -s -t 1");

           out.print("Server se tat sau 10s");

        }

        if(kt.equals("restart"))

        {

           Runtime.getRuntime().exec("shutdown.exe -r -t 1");

           out.print("Server se restart sau 10s");

        }

        if(kt.equals("logoff"))

        {

           Runtime.getRuntime().exec("shutdown.exe -l -t 1");

           out.print("Server log off sau 10s");

        }

    }

    /**

     * Handles the HTTP POST method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }

    /**

     * Returns a short description of the servlet.

     * @return a String containing servlet description

     */

    @Override

    public String getServletInfo() {

        return "Short description";

    }//

}

Câu 3

Client

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import javax.microedition.lcdui.*;

import java.io.*;

/**

 * @author Capkama

 */

public class Cau3Mobile extends MIDlet implements CommandListener,Runnable {

    HttpConnection con;

    InputStream in;

    OutputStream out;

    Thread th;

    Form mf;

    Display ds = Display.getDisplay(this);

    TextField t1,t2;

    StringItem st;

    Command c1,c2;

    String url;

public Cau3Mobile(){

    th=new Thread(this);

    mf=new Form("Tra điểm");

    t1 = new TextField("Mã SV","",10,TextField.ANY);

    t2 = new TextField("Mã môn học","",10,TextField.ANY);

    st = new StringItem("Điểm của bạn: ","");

    c1=new Command("Connect",Command.OK,1);

    c2=new Command("Exit",Command.EXIT,0);

    mf.append(t1);

    mf.append(t2);

    mf.append(st);

    mf.addCommand(c1);

    mf.addCommand(c2);

    mf.setCommandListener(this);

}

    public void startApp() {

        ds.setCurrent(mf);

    }

    public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {

    }

    public void connect(){

              try{

                    con=(HttpConnection)Connector.open(url);

                    con.setRequestMethod(HttpConnection.GET);

                    con.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");

                    con.setRequestProperty("Content-Language", "en-US");

                    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

              }

              catch(Exception e){

                    st.setText("Kết nối lỗi: "+e.getMessage());

              }

    }

    public void in(){

            String;

              try{

                     in=con.openInputStream();

                     int length=(int)con.getLength();

                     byte server[];

                     if(length!=-1){

                           server=new byte[length];

                           in.read(server);

                           str=new String(server);

                     }

                     else

                     {

                           ByteArrayOutputStream br=new

                                          ByteArrayOutputStream();

                           int ch;

                           while((ch=in.read())!=-1)

                                  br.write(ch);

                           str=new String(br.toByteArray());                               

                           br.close();

                        }

                        st.setText(str);

              }

              catch(Exception e){

              }

       }

        public synchronized void run(){

            this.connect();

            this.in();

        }

    public void commandAction(Command c, Displayable d)

    {

             +t1.getString()+"&MaMon="+t2.getString()+"";

              th=new Thread(this);

              th.start();

    }

}

sever

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

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;

/**

 *

 * @author Capkama

 */

@WebServlet(name = "Cau3Web", urlPatterns = {"/Cau3Web"})

public class Cau3Web extends HttpServlet {

    /**

     * Processes requests for both HTTP GET and POST methods.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        try {

            /* TODO output your page here

            out.println("");

            out.println("");

            out.println("Servlet Cau3Web"); 

            out.println("");

            out.println("");

            out.println("

Servlet Cau3Web at " + request.getContextPath () + "

");

            out.println("");

            out.println("");

             */

        } finally {           

            out.close();

        }

    }

    /**

     * Handles the HTTP GET method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String SV[]={"sv1","sv2","sv3","sv4","sv5"};

        String mm[]={"no1","no2","no3","no4","no5"};

        int diem[][]={{6,7,8,5,5},{8,6,7,5,9},{5,6,7,4,8},{8,5,6,4,8},{9,10,7,8,9},{7,6,7,8,9}};

        String masv = request.getParameter("MaSv");

        String mamon = request.getParameter("MaMon");

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        for(int i=0;i<5;i++)

        {

            if(SV[i].equals(masv))

            {

                for(int j=0;j<5;j++)

                {

                    if(mamon.equals(mm[j]))

                    {

                        out.print(diem[i][j]);

                    }

                }

            }

        }

    }

    /**

     * Handles the HTTP POST method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }

    /**

     * Returns a short description of the servlet.

     * @return a String containing servlet description

     */

    @Override

    public String getServletInfo() {

        return "Short description";

    }//

}

Câu 4

Client

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import javax.microedition.lcdui.*;

import java.io.*;

/**

 * @author Capkama

 */

public class Cau4Mobile extends MIDlet implements CommandListener,Runnable {

    HttpConnection con;

    InputStream in;

    OutputStream out;

    Thread th;

    Form mf;

    Display ds = Display.getDisplay(this);

    TextField t1,t2;

    StringItem st;

    Command c1,c2;

    String url;

public Cau4Mobile(){

    th=new Thread(this);

    mf=new Form("Kiểm tra STT");

    t1 = new TextField("Person 1:","",10,TextField.ANY);

    st = new StringItem("","");

    c1=new Command("Connect",Command.OK,1);

    c2=new Command("Exit",Command.EXIT,0);

    mf.append(t1);

    mf.append(st);

    mf.addCommand(c1);

    mf.addCommand(c2);

    mf.setCommandListener(this);

}

    public void startApp() {

        ds.setCurrent(mf);

    }

    public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {

    }

    public void connect(){

                   try{

                    con=(HttpConnection)Connector.open(url);

                    con.setRequestMethod(HttpConnection.GET);

                    con.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");

                    con.setRequestProperty("Content-Language", "en-US");

                    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                   }

                   catch(Exception e){

                    st.setText("Kết nối lỗi: "+e.getMessage());

                   }

    }

    public void in(){

            String kt ="",str,kt2="";

            String;

                   try{

                             in=con.openInputStream();

                             int length=(int)con.getLength();

                             byte server[];

                             if(length!=-1){

                                      server=new byte[length];

                                      in.read(server);

                                      str=new String(server);

                             }

                             else

                             {

                                      ByteArrayOutputStream br=new

                                ByteArrayOutputStream();

                                      int ch;

                                      while((ch=in.read())!=-1)

                                      br.write(ch);

                                      str=new String(br.toByteArray());                               

                                      br.close();

                        }

                        for(int i=0;i<str.length();i++)

                        {

                           kt2=""+str.charAt(i);

                           if(tam.equals(kt2))

                           {

                               kt=kt+"

";

                           }

                           else kt=kt+str.charAt(i);

                        }

                        st.setText(kt);

                        //st.setText(str);

                   }

                   catch(Exception e){

                   }

          }

        public synchronized void run(){

            this.connect();

            this.in();

        }

    public void commandAction(Command c, Displayable d)

    {

        if(c==c1)

        {

       +t1.getString()+"";

        th=new Thread(this);

        th.start();

        }

    }

}

Server

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

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;

/**

 *

 * @author Capkama

 */

@WebServlet(name = "Cau4Web", urlPatterns = {"/Cau4Web"})

public class Cau4Web extends HttpServlet {

    /**

     * Processes requests for both HTTP GET and POST methods.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        try {

            /* TODO output your page here

            out.println("");

            out.println("");

            out.println("Servlet Cau4Web"); 

            out.println("");

            out.println("");

            out.println("

Servlet Cau4Web at " + request.getContextPath () + "

");

            out.println("");

            out.println("");

             */

        } finally {           

            out.close();

        }

    }

    /**

     * Handles the HTTP GET method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String Per[]={"a","b","d"};

        String Sta[]={"B:on C:off D:off","A:on D:off","B:off"};

        response.setContentType("text/html");

        String ten = request.getParameter("Per");

        PrintWriter out = response.getWriter();

        for(int i=0;i<3;i++)

        {

            if(ten.equals(Per[i]))

            {

                out.print(Sta[i]);

            }

        }

    }

    /**

     * Handles the HTTP POST method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

            processRequest(request, response);

    }

    /**

     * Returns a short description of the servlet.

     * @return a String containing servlet description

     */

    @Override

    public String getServletInfo() {

        return "Short description";

    }//

}

Câu 5

Client

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import javax.microedition.lcdui.*;

import java.io.*;

/**

 * @author Capkama

 */

public class Cau5Mobile extends MIDlet implements CommandListener,Runnable {

    HttpConnection con;

    InputStream in;

    OutputStream out;

    Thread th;

    Form mf;

    Display ds = Display.getDisplay(this);

    TextField t1,t2;

    StringItem st;

    Command c1,c2;

    String url;

public Cau5Mobile(){

    th=new Thread(this);

    mf=new Form("Kiểm tra điện trở");

    t1 = new TextField("Mã màu:","",10,TextField.ANY);

    st = new StringItem("KQ","");

    c1=new Command("Connect",Command.OK,1);

    c2=new Command("Exit",Command.EXIT,0);

    mf.append(t1);

    mf.append(st);

    mf.addCommand(c1);

    mf.addCommand(c2);

    mf.setCommandListener(this);

}

    public void startApp() {

        ds.setCurrent(mf);

    }

    public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {

    }

    public void connect(){

                   try{

                    con=(HttpConnection)Connector.open(url);

                    con.setRequestMethod(HttpConnection.GET);

                    con.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");

                    con.setRequestProperty("Content-Language", "en-US");

                    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                   }

                   catch(Exception e){

                    st.setText("Kết nối lỗi: "+e.getMessage());

                   }

    }

    public void in(){

            String;

                   try{

                             in=con.openInputStream();

                             int length=(int)con.getLength();

                             byte server[];

                             if(length!=-1){

                                      server=new byte[length];

                                      in.read(server);

                                      str=new String(server);

                             }

                             else

                             {

                                      ByteArrayOutputStream br=new

                                ByteArrayOutputStream();

                                      int ch;

                                      while((ch=in.read())!=-1)

                                      br.write(ch);

                                      str=new String(br.toByteArray());                               

                                      br.close();

                        }

                        st.setText(str);

                   }

                   catch(Exception e){

                   }

          }

        public synchronized void run(){

            this.connect();

            this.in();

        }

    public void commandAction(Command c, Displayable d)

    {

        if(c==c1)

        {

       +t1.getString()+"";

        th=new Thread(this);

        th.start();

        }

    }

}

Server

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

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;

* @author Capkama

 */

@WebServlet(name = "Cau5Web", urlPatterns = {"/Cau5Web"})

public class Cau5Web extends HttpServlet {

    /**

     * Processes requests for both HTTP GET and POST methods.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        try {

            /* TODO output your page here

            out.println("");

            out.println("");

            out.println("Servlet Cau5Web"); 

            out.println("");

            out.println("");

            out.println("

Servlet Cau5Web at " + request.getContextPath () + "

");

            out.println("");

            out.println("");

             */

        } finally {           

            out.close();

        }

    }

    /**

     * Handles the HTTP GET method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String matro=request.getParameter("Matro");

        String;

        String ma[]={"Đen","Nâu","Đỏ","Cam","Vàng","Xlá","XLam","Tím","Xám","Trắng"};

        PrintWriter out = response.getWriter();

        for(int i=0;i<3;i++)

        {

            String+matro.charAt(i);

            int kq = Integer.valueOf(kt).intValue();

            ketqua = ketqua+ma[kq]+" ";

        }

        out.print(ketqua);

    }

    /**

     * Handles the HTTP POST method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }

    /**

     * Returns a short description of the servlet.

     * @return a String containing servlet description

     */

    @Override

    public String getServletInfo() {

        return "Short description";

    }//

}

Câu 6

Client

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

import javax.microedition.midlet.*;

import javax.microedition.io.*;

import javax.microedition.lcdui.*;

import java.io.*;

/**

 * @author Capkama

 */

public class Cau6Mobile extends MIDlet implements CommandListener,Runnable {

    HttpConnection con;

    InputStream in;

    OutputStream out;

    Thread th;

    Form mf;

    Display ds = Display.getDisplay(this);

    TextField t1,t2;

    StringItem st;

    Command c1,c2;

    String url;

public Cau6Mobile(){

    th=new Thread(this);

    mf=new Form("Chuyển nhị phân");

    t1 = new TextField("Thập phân:","",10,TextField.ANY);

    st = new StringItem("KQ: ","");

    c1=new Command("Connect",Command.OK,1);

    c2=new Command("Exit",Command.EXIT,0);

    mf.append(t1);

    mf.append(st);

    mf.addCommand(c1);

    mf.addCommand(c2);

    mf.setCommandListener(this);

}

    public void startApp() {

        ds.setCurrent(mf);

    }

    public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {

    }

    public void connect(){

                   try{

                    con=(HttpConnection)Connector.open(url);

                    con.setRequestMethod(HttpConnection.GET);

                    con.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");

                    con.setRequestProperty("Content-Language", "en-US");

                    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                   }

                   catch(Exception e){

                    st.setText("Kết nối lỗi: "+e.getMessage());

                   }

    }

    public void in(){

            String;

                   try{

                             in=con.openInputStream();

                             int length=(int)con.getLength();

                             byte server[];

                             if(length!=-1){

                                      server=new byte[length];

                                      in.read(server);

                                      str=new String(server);

                             }

                             else

                             {

                                      ByteArrayOutputStream br=new

                                ByteArrayOutputStream();

                                      int ch;

                                      while((ch=in.read())!=-1)

                                      br.write(ch);

                                      str=new String(br.toByteArray());                               

                                      br.close();

                        }

                        st.setText(str);

                   }

                   catch(Exception e){

                   }

          }

        public synchronized void run(){

            this.connect();

            this.in();

        }

    public void commandAction(Command c, Displayable d)

    {

        if(c==c1)

        {

       +t1.getString()+"";

        th=new Thread(this);

        th.start();

        }

    }

}

Server

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

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;

/**

 *

 * @author Capkama

 */

@WebServlet(name = "Cau6Web", urlPatterns = {"/Cau6Web"})

public class Cau6Web extends HttpServlet {

    /**

     * Processes requests for both HTTP GET and POST methods.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        try {

            /* TODO output your page here

            out.println("");

            out.println("");

            out.println("Servlet Cau6Web"); 

            out.println("");

            out.println("");

            out.println("

Servlet Cau6Web at " + request.getContextPath () + "

");

            out.println("");

            out.println("");

             */

        } finally {           

            out.close();

        }

    }

    /**

     * Handles the HTTP GET method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        boolean i=true;

        String,tam="";

        String st=request.getParameter("Sokt");

        PrintWriter out = response.getWriter();

        int kt = Integer.valueOf(st).intValue();

        if((kt65535))

        {

            out.print("Vượt quá khả năng cho phép");

        }

        else{

        while(i)

        {

           +kt%2;

            kt=kt/2;

            kq=tam+kq;

            if(kt==0)

                i=false;

        }

        out.print(kq);

        }

    }

    /**

     * Handles the HTTP POST method.

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }

    /**

     * Returns a short description of the servlet.

     * @return a String containing servlet description

     */

    @Override

    public String getServletInfo() {

        return "Short description";

    }//

}

Bạn đang đọc truyện trên: Truyen2U.Pro