Partage
  • Partager sur Facebook
  • Partager sur Twitter

[Java]Connexion Midlet-Servlet

    18 avril 2008 à 12:28:52

    Bonjour,

    Je suis en train de realiser une application web dans laquelle jutilise un Servlet et un Midlet. Voici les codes correspondants:

    MIDLET:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package client;
    
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.io.*;
    import java.io.*;
    
    public class ClientMidlet extends MIDlet implements CommandListener {
        Display display = null;
    
        // email form fields     
        TextField number1 = null;
        TextField number2 = null;
        Form form;
    
        String url = "http://localhost:8084/Server";
        static final Command identifyCommand = new Command("identify", Command.OK, 2);
        static final Command clearCommand = new Command("clear", Command.STOP, 3);
    
        String num1;
        String num2;
    
    
        public ClientMidlet() {
           display = Display.getDisplay(this);
           number1 = new TextField("Name:", "", 10, TextField.ANY);
           number2 = new TextField("Password:", "", 10, TextField.ANY);
           form = new Form("Identification");
    
        }
    
        public void startApp() throws MIDletStateChangeException {
          form.append(number1);
          form.append(number2);
          form.addCommand(clearCommand);
          form.addCommand(identifyCommand);
          form.setCommandListener(this);
          display.setCurrent(form);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
          notifyDestroyed();
        }
    
        void invokeServlet(String url) throws IOException {
            HttpConnection c = null;
            InputStream is = null;
            OutputStream os = null;
            StringBuffer b = new StringBuffer();
            TextBox t = null;
            try {
              c = (HttpConnection)Connector.open(url);
              c.setRequestMethod(HttpConnection.POST);
              c.setRequestProperty("IF-Modified-Since", "20 Oct 2001 16:19:14 GMT");
              c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
              c.setRequestProperty("Content-Language", "en-CA");
              c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
              
              os = c.openOutputStream();
              // encode data
              os.write(("Name="+num1).getBytes());
              os.write(("&Password="+num2).getBytes());
              os.flush();
    
              is = c.openDataInputStream();
              int ch;
              while ((ch = is.read()) != -1) {
                b.append((char) ch);
                System.out.print((char)ch);
              }
              t = new TextBox("Identification", b.toString(), 1024, 0);
              t.setCommandListener(this);
            } finally {
               if(is!= null) {
                  is.close();
               }
               if(os != null) {
                  os.close();
               }
               if(c != null) {
                  c.close();
               }
            }
            display.setCurrent(t);
        } 
    
       public void commandAction(Command c, Displayable d) {
          String label = c.getLabel();
          if(label.equals("clear")) {
            destroyApp(true);
          } else if (label.equals("Identify")) {
             num1 = number1.getString();
             num2 = number2.getString();
             try {
               invokeServlet(url);
             }catch(IOException e) {}
          } 
       }
    }
    


    SERVLET:

    *
    
    public class Server extends HttpServlet{
    
     public void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws IOException, ServletException
        {
            response.setContentType("text/plain");
            PrintWriter out = response.getWriter();
    
            String s1 = request.getParameter("name");
            String s2 = request.getParameter("password");
            //int result = 0; 
             //try {
              //Identify obj = (Identify)Naming.lookup("rmi://127.0.0.1/IdentifyServer");
             // result = obj.identify(s1, s2);
            //} catch (Exception e) {
              //System.out.println("Servlet exception:"+e.getMessage());
              //e.printStackTrace();
            //}
            //if(result == 0)
            out.println("Unknown account");
            //if(result == 1)
            //out.println("Welcome");
        }  
    }
    


    J'utilise normalement une base de donnees ainsi que RMI pour dautres actions, mais jai ici simplifie les choses et je ne demande qu'un renvoi de string "unknown account". Lorsque je declenche l'ActionCommand Identify, rien ne se passe. Les deux programmes compilent parfaitement et mon server Tomcat est aussi operationnel.
    Quelqu'un aurait-il une idee?
    Merci d'avance.
    • Partager sur Facebook
    • Partager sur Twitter

    [Java]Connexion Midlet-Servlet

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown