IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Servlets/JSP Java Discussion :

Appeler une servlet à partir d'un projet J2ME


Sujet :

Servlets/JSP Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2011
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mars 2011
    Messages : 40
    Par défaut Appeler une servlet à partir d'un projet J2ME
    Salut
    J'ai développé une servlet qui se connecte à la base de données postgresql et récupere des requetes, ensuite je l'ai déployé dans tomcat, et mnt je suis entrain de développer un projet j2me dans lequel je veux appeler cette servlet, je suis dans la phase d'authentification et je veux comparer le mot de passe saisi avec celui récupéré de la base de données voila le code que j'utilise:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    package hello;
    
    
    import javax.microedition.io.*;
    import java.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    
    
    public class Connection extends MIDlet implements CommandListener {
       private Display display;
       private TextField userName;
       private TextField password;
       private Form form;
       private Command cancel;
       private Command login;
       String url = "http://192.168.0.12:8082/mobile/listcmdssss";
    
       public Connection() {
          userName = new TextField("LoginID:", "", 10, TextField.ANY);
          password = new TextField("Password:", "", 10, TextField.PASSWORD);
          form = new Form("Sign in");
          cancel = new Command("Cancel", Command.CANCEL, 2);
          login = new Command("Login", Command.OK, 2);
        }
    
    
    
    
    
    
       public void startApp() {
          display = Display.getDisplay(this);
          form.append(userName);
          form.append(password);
          form.addCommand(cancel);
          form.addCommand(login);
          form.setCommandListener(this);
          display.setCurrent(form);
       }
    
       public void pauseApp() {
       }
    
       public void destroyApp(boolean unconditional) {
          notifyDestroyed();
       }
    
      public void menu() {
         List services = new List("Choose one", Choice.EXCLUSIVE);
         services.append("Check Mail", null);
         services.append("Compose", null);
         services.append("Addresses", null);
         services.append("Options", null);
         services.append("Sign Out", null);
         display.setCurrent(services);
       }
    
       public void tryAgain() {
         Alert error = new Alert("Login Incorrect", "Please try again", null, AlertType.ERROR);
         error.setTimeout(Alert.FOREVER);
         userName.setString("");
         password.setString("");
         display.setCurrent(error, form);
       }
    
       public void commandAction(Command c, Displayable d) {
          String label = c.getLabel();
          if(label.equals("Cancel")) {
            destroyApp(true);
          } else if(label.equals("Login")) {
             
             try {
                     invokeServlet(url);
    
              } catch (IOException ex) {
    
              }
    
          }
       }
    
       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("CONTENT-TYPE","application/x-www-form-urlencoded");
             c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
             c.setRequestProperty("Content-Language", "en-CA");
    
             os = c.openOutputStream();
             ..............
             }
            
             os.flush();
    
             is = c.openDataInputStream();
             int ch;
             while ((ch = is.read()) != -1) {
                b.append((char) ch);
                System.out.print((char)ch);
             }
             t = new TextBox("Second Servlet", b.toString(), 1024, 0);
            // t.addCommand(backCommand);
             t.setCommandListener(this);
          } finally {
             if(is!= null) {
                is.close();
             }
             if(os != null) {
                os.close();
             }
             if(c != null) {
                c.close();
             }
          }
          display.setCurrent(t);
       }
    
    
    }
    Les points en rouge c'est la ou je me bloque je sais pas comment faire la comparaison. SVP si vous avez une idée

  2. #2
    Membre averti
    Femme Profil pro
    Étudiant
    Inscrit en
    Mars 2011
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Mars 2011
    Messages : 40
    Par défaut
    Svp parsonne n'a uune idée???
    Voila ma servlet
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    package test;
     
    import java.io.*;
    //import java.net.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
     
    public class listcmd extends HttpServlet {
    	private static final long serialVersionUID = 1L;
     
    	private Open open=new Open();
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
     
            out.close();
        }
     
     
    	protected void doGet(HttpServletRequest request, HttpServletResponse response)
    	throws ServletException, IOException {
    		BufferedReader br = request.getReader();
    		 String buf ="";
    		 buf=br.readLine();
    		open.authentif(buf);
     
    		        processRequest(request, response);
    	}
     
    	protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
             response.setContentType("text/plain");
            PrintWriter out = response.getWriter();
            BufferedReader br = request.getReader();
            //BufferedReader brr = request.getReader();
     
           String buf ="";
           String buf2 ="";
           buf=br.readLine();
           //buf=brr.readLine();
           try{
        	   System.out.println("hello");
        	   System.out.println(buf);
     
                 if(buf!=null ){
                	if (buf.contains("login")){
     
                		  String r="";
                		  r=open.authentif(buf);
                		  out.flush();
                		  out.print(r);
                		  System.out.print(r);
                	  }  catch(NullPointerException ex){
              out.print("");
            }
     
     
        }
     
     
    	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    	throws IOException, ServletException{
     
     
     
    	}
     
     
        public String getServletInfo() {
            return "Short description";
        }
        // </editor-fold>
    }
    qui verifie la validité du mot de passe.
    j'ai essayé de l'appeler depuis j2me j'ai essayé tout d'abord d'appeler la methode doPost mais le buffer ne se charge pas et aussi la méme chose pour la methode doGet, Dans le fichier log de tomcat je trouve qu'il se connecte à la servlet et il m'affiche les println que j'ai mis dans la servlet mais lorsque je met le buffer dans println il me retourne null
    voila le code j2me:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    package hello;
     
     
    import javax.microedition.io.*;
    import java.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
     
     
    public class Connection extends MIDlet implements CommandListener {
       private Display display;
       private TextField userName;
       private TextField password;
       private Form form;
       private Command cancel;
       private Command login;
       String url = "http://localhost:8082/mobile/listcmd";
       static final Command backCommand = new Command("Back", Command.BACK, 0);
     
       public Connection() {
          userName = new TextField("LoginID:", "", 10, TextField.ANY);
          password = new TextField("Password:", "", 10, TextField.PASSWORD);
          form = new Form("Sign in");
          cancel = new Command("Cancel", Command.CANCEL, 2);
          login = new Command("Login", Command.OK, 2);
        }
       private String Rep="";
       private String Req="";
     
     
       public void startApp() {
          display = Display.getDisplay(this);
          form.append(userName);
          form.append(password);
          form.addCommand(cancel);
          form.addCommand(login);
          form.setCommandListener(this);
          display.setCurrent(form);
       }
     
       public void pauseApp() {
       }
     
       public void destroyApp(boolean unconditional) {
          notifyDestroyed();
       }
     
     
       public void menu() {
         List services = new List("Choose one", Choice.EXCLUSIVE);
         services.append("Check Mail", null);
         services.append("Compose", null);
         services.append("Addresses", null);
         services.append("Options", null);
         services.append("Sign Out", null);
         display.setCurrent(services);
       }
     
       public void tryAgain() {
         Alert error = new Alert("Login Incorrect", "Please try again", null, AlertType.ERROR);
         error.setTimeout(Alert.FOREVER);
         userName.setString("");
         password.setString("");
         display.setCurrent(error, form);
       }
     
       public void commandAction(Command c, Displayable d) {
          String label = c.getLabel();
          if(label.equals("Cancel")) {
            destroyApp(true);
          } else if(label.equals("Login")) {
                try {
                     Req=password.getString();
                     invokeServlet(Req);
     
              } catch (IOException ex) {
     
              }
     
          }
       }
     
       void invokeServlet(String Req) 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("CONTENT-TYPE","application/x-www-form-urlencoded");
             c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
             c.setRequestProperty("Content-Language", "en-CA");
     
             os = c.openOutputStream();
     
            byte postmsg[] = Req.getBytes();
     
                     for(int i=0;i<postmsg.length;i++) {
     
                         os.write(postmsg[i]);
                         System.out.println(postmsg[i]);
                    }
     
     
     
             os.flush();
     
             is = c.openDataInputStream();
             int ch;
             while ((ch = is.read()) != -1) {
                b.append((char) ch);
     
             }
            Rep=b.toString();
               System.out.println(" reponse: "+Rep);
     
               //Req="";
     
          }catch (IOException excep) {
            System.out.println(excep.getMessage());
          }
          finally {
             if(is!= null) {
                is.close();
             }
             if(os != null) {
                os.close();
             }
             if(c != null) {
                c.close();
             }
          }
          display.setCurrent(t);
       }
     
    }
    SVP j'ai besoin d'aide

Discussions similaires

  1. Appeler une servlet à partir d'une image
    Par momjunior dans le forum Servlets/JSP
    Réponses: 4
    Dernier message: 26/04/2012, 13h16
  2. appeler une servlet à partir d'un projet j2me
    Par chimouch dans le forum Servlets/JSP
    Réponses: 3
    Dernier message: 19/04/2011, 10h50
  3. Appeler une servlet d'un autre projet
    Par YASIR dans le forum Servlets/JSP
    Réponses: 1
    Dernier message: 27/02/2010, 21h58
  4. Réponses: 3
    Dernier message: 05/04/2007, 10h57
  5. appel de servlet à partir de jsp
    Par eyango dans le forum Servlets/JSP
    Réponses: 13
    Dernier message: 20/01/2007, 00h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo