validação de usuario
03/09/2007
0
BOA NOITE A TODOS
estou tendo um problema... td está passando no formulário de login... eu fiz os métodos e talz, mas acho q estou errando em alguma coisa... o código está ai... por favor, se alguém tiver alguma idéia, um link, artigo, etc... me passe, ficarei muito agradecida.. :)
estou tendo um problema... td está passando no formulário de login... eu fiz os métodos e talz, mas acho q estou errando em alguma coisa... o código está ai... por favor, se alguém tiver alguma idéia, um link, artigo, etc... me passe, ficarei muito agradecida.. :)
/* * Teste.java * * Created on 1 de Setembro de 2007, 19:34 */ import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import javax.microedition.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * * @author Administrador */ public class Teste extends MIDlet implements CommandListener { /** Creates a new instance of Teste */ public Teste() { initialize(); } private Form formPrincipal; private Form formEscolha; private Command exitCommand1; private Command okCommand1; private TextField textFieldLogin; private TextField textFieldSenha; private Command okCommand2; private Command backCommand1; private Form formChassi; private Command backCommand2; private Command okCommand3; private Command exitCommand2; private Command exitCommand3; private Form formPlaca; private Command backCommand3; private Command exitCommand4; private Command okCommand4; private TextField textFieldPlaca; private TextField textFieldChassi; private Command screenCommand1; private Command screenCommand2; private StringItem stringItem1; private Alert alerta; private Alert alerta2; private String errorMsg = null; /** Called by the system to indicate that a command has been invoked on a particular displayable. * @param command the Command that ws invoked * @param displayable the Displayable on which the command was invoked */ public void commandAction(Command command, Displayable displayable) { // Insert global pre-action code here if (displayable == formPrincipal) { if (command == okCommand1) { try { validarUsuario(textFieldLogin.getString(), textFieldSenha.getString()); } catch (Exception e) { System.err.println("Msg: " + e.toString()); }// Insert pre-action code here // Do nothing // Insert post-action code here } else if (command == exitCommand1) { try { destroyApp(false); notifyDestroyed(); } catch (Exception e) { e.printStackTrace(); }// Insert pre-action code here exitMIDlet(); // Insert post-action code here } } else if (displayable == formEscolha) { if (command == backCommand1) { // Insert pre-action code here getDisplay().setCurrent(get_formPrincipal()); // Insert post-action code here } else if (command == exitCommand3) { // Insert pre-action code here exitMIDlet(); // Insert post-action code here } else if (command == screenCommand1) { // Insert pre-action code here getDisplay().setCurrent(get_formChassi()); // Insert post-action code here } else if (command == screenCommand2) { // Insert pre-action code here getDisplay().setCurrent(get_formPlaca()); // Insert post-action code here } } else if (displayable == formPlaca) { if (command == backCommand3) { // Insert pre-action code here getDisplay().setCurrent(get_formEscolha()); // Insert post-action code here } else if (command == exitCommand4) { // Insert pre-action code here exitMIDlet(); // Insert post-action code here } else if (command == okCommand4) { // Insert pre-action code here // Do nothing // Insert post-action code here } } else if (displayable == formChassi) { if (command == backCommand2) { // Insert pre-action code here getDisplay().setCurrent(get_formEscolha()); // Insert post-action code here } else if (command == exitCommand2) { // Insert pre-action code here // Do nothing // Insert post-action code here } else if (command == okCommand3) { // Insert pre-action code here // Do nothing // Insert post-action code here } } // Insert global post-action code here } /** This method initializes UI of the application. */ private void initialize() { // Insert pre-init code here getDisplay().setCurrent(get_formPrincipal()); // Insert post-init code here } /** * This method should return an instance of the display. */ public Display getDisplay() { return Display.getDisplay(this); } /** * This method should exit the midlet. */ public void exitMIDlet() { getDisplay().setCurrent(null); destroyApp(true); notifyDestroyed(); } /** This method returns instance for formPrincipal component and should be called instead of accessing formPrincipal field directly. * @return Instance for formPrincipal component */ public Form get_formPrincipal() { if (formPrincipal == null) { // Insert pre-init code here formPrincipal = new Form("Autentica\u00E7\u00E3o", new Item[] { get_textFieldLogin(), get_textFieldSenha() }); formPrincipal.addCommand(get_exitCommand1()); formPrincipal.addCommand(get_okCommand1()); formPrincipal.setCommandListener(this); // Insert post-init code here } return formPrincipal; } /** This method returns instance for formEscolha component and should be called instead of accessing formEscolha field directly. * @return Instance for formEscolha component */ public Form get_formEscolha() { if (formEscolha == null) { // Insert pre-init code here formEscolha = new Form("Tipo de Consulta", new Item[] {get_stringItem1()}); formEscolha.addCommand(get_backCommand1()); formEscolha.addCommand(get_exitCommand3()); formEscolha.addCommand(get_screenCommand1()); formEscolha.addCommand(get_screenCommand2()); formEscolha.setCommandListener(this); // Insert post-init code here } return formEscolha; } /** This method returns instance for exitCommand1 component and should be called instead of accessing exitCommand1 field directly. * @return Instance for exitCommand1 component */ public Command get_exitCommand1() { if (exitCommand1 == null) { // Insert pre-init code here exitCommand1 = new Command("Sair", Command.EXIT, 1); // Insert post-init code here } return exitCommand1; } /** This method returns instance for okCommand1 component and should be called instead of accessing okCommand1 field directly. * @return Instance for okCommand1 component */ public Command get_okCommand1() { if (okCommand1 == null) { // Insert pre-init code here okCommand1 = new Command("Logar", Command.OK, 1); // Insert post-init code here } return okCommand1; } /** This method returns instance for textFieldLogin component and should be called instead of accessing textFieldLogin field directly. * @return Instance for textFieldLogin component */ public TextField get_textFieldLogin() { if (textFieldLogin == null) { // Insert pre-init code here textFieldLogin = new TextField("Login", null, 120, TextField.ANY); // Insert post-init code here } return textFieldLogin; } /** This method returns instance for textFieldSenha component and should be called instead of accessing textFieldSenha field directly. * @return Instance for textFieldSenha component */ public TextField get_textFieldSenha() { if (textFieldSenha == null) { // Insert pre-init code here textFieldSenha = new TextField("Senha", null, 120, TextField.ANY | TextField.PASSWORD); // Insert post-init code here } return textFieldSenha; } /** This method returns instance for okCommand2 component and should be called instead of accessing okCommand2 field directly. * @return Instance for okCommand2 component */ public Command get_okCommand2() { if (okCommand2 == null) { // Insert pre-init code here okCommand2 = new Command("Ok", Command.OK, 1); // Insert post-init code here } return okCommand2; } /** This method returns instance for backCommand1 component and should be called instead of accessing backCommand1 field directly. * @return Instance for backCommand1 component */ public Command get_backCommand1() { if (backCommand1 == null) { // Insert pre-init code here backCommand1 = new Command("Voltar", Command.BACK, 1); // Insert post-init code here } return backCommand1; } /** This method returns instance for formChassi component and should be called instead of accessing formChassi field directly. * @return Instance for formChassi component */ public Form get_formChassi() { if (formChassi == null) { // Insert pre-init code here formChassi = new Form("Chassi", new Item[] {get_textFieldChassi()}); formChassi.addCommand(get_backCommand2()); formChassi.addCommand(get_okCommand3()); formChassi.addCommand(get_exitCommand2()); formChassi.setCommandListener(this); // Insert post-init code here } return formChassi; } /** This method returns instance for backCommand2 component and should be called instead of accessing backCommand2 field directly. * @return Instance for backCommand2 component */ public Command get_backCommand2() { if (backCommand2 == null) { // Insert pre-init code here backCommand2 = new Command("Voltar", Command.BACK, 1); // Insert post-init code here } return backCommand2; } /** This method returns instance for okCommand3 component and should be called instead of accessing okCommand3 field directly. * @return Instance for okCommand3 component */ public Command get_okCommand3() { if (okCommand3 == null) { // Insert pre-init code here okCommand3 = new Command("Consultar", Command.OK, 1); // Insert post-init code here } return okCommand3; } /** This method returns instance for exitCommand2 component and should be called instead of accessing exitCommand2 field directly. * @return Instance for exitCommand2 component */ public Command get_exitCommand2() { if (exitCommand2 == null) { // Insert pre-init code here exitCommand2 = new Command("Sair", Command.EXIT, 1); // Insert post-init code here } return exitCommand2; } /** This method returns instance for exitCommand3 component and should be called instead of accessing exitCommand3 field directly. * @return Instance for exitCommand3 component */ public Command get_exitCommand3() { if (exitCommand3 == null) { // Insert pre-init code here exitCommand3 = new Command("Sair", Command.EXIT, 1); // Insert post-init code here } return exitCommand3; } /** This method returns instance for formPlaca component and should be called instead of accessing formPlaca field directly. * @return Instance for formPlaca component */ public Form get_formPlaca() { if (formPlaca == null) { // Insert pre-init code here formPlaca = new Form("Placa", new Item[] {get_textFieldPlaca()}); formPlaca.addCommand(get_backCommand3()); formPlaca.addCommand(get_exitCommand4()); formPlaca.addCommand(get_okCommand4()); formPlaca.setCommandListener(this); // Insert post-init code here } return formPlaca; } /** This method returns instance for backCommand3 component and should be called instead of accessing backCommand3 field directly. * @return Instance for backCommand3 component */ public Command get_backCommand3() { if (backCommand3 == null) { // Insert pre-init code here backCommand3 = new Command("Voltar", Command.BACK, 1); // Insert post-init code here } return backCommand3; } /** This method returns instance for exitCommand4 component and should be called instead of accessing exitCommand4 field directly. * @return Instance for exitCommand4 component */ public Command get_exitCommand4() { if (exitCommand4 == null) { // Insert pre-init code here exitCommand4 = new Command("Sair", Command.EXIT, 1); // Insert post-init code here } return exitCommand4; } /** This method returns instance for okCommand4 component and should be called instead of accessing okCommand4 field directly. * @return Instance for okCommand4 component */ public Command get_okCommand4() { if (okCommand4 == null) { // Insert pre-init code here okCommand4 = new Command("Consultar", Command.OK, 1); // Insert post-init code here } return okCommand4; } /** This method returns instance for textFieldPlaca component and should be called instead of accessing textFieldPlaca field directly. * @return Instance for textFieldPlaca component */ public TextField get_textFieldPlaca() { if (textFieldPlaca == null) { // Insert pre-init code here textFieldPlaca = new TextField("Placa", null, 120, TextField.ANY); // Insert post-init code here } return textFieldPlaca; } /** This method returns instance for textFieldChassi component and should be called instead of accessing textFieldChassi field directly. * @return Instance for textFieldChassi component */ public TextField get_textFieldChassi() { if (textFieldChassi == null) { // Insert pre-init code here textFieldChassi = new TextField("Chassi", null, 120, TextField.ANY); // Insert post-init code here } return textFieldChassi; } /** This method returns instance for screenCommand1 component and should be called instead of accessing screenCommand1 field directly. * @return Instance for screenCommand1 component */ public Command get_screenCommand1() { if (screenCommand1 == null) { // Insert pre-init code here screenCommand1 = new Command("Chassi", Command.SCREEN, 1); // Insert post-init code here } return screenCommand1; } /** This method returns instance for screenCommand2 component and should be called instead of accessing screenCommand2 field directly. * @return Instance for screenCommand2 component */ public Command get_screenCommand2() { if (screenCommand2 == null) { // Insert pre-init code here screenCommand2 = new Command("Placa", Command.SCREEN, 1); // Insert post-init code here } return screenCommand2; } /** This method returns instance for stringItem1 component and should be called instead of accessing stringItem1 field directly. * @return Instance for stringItem1 component */ public StringItem get_stringItem1() { if (stringItem1 == null) { // Insert pre-init code here stringItem1 = new StringItem("Tipo de Consulta: ", "Por Favor, clique no Menu/Bot\u00F5es para escolher o tipo de consulta. Obrigado(a)!"); // Insert post-init code here } return stringItem1; } private void showAlert(String Msg) { alerta = new Alert("Erro", Msg, null, AlertType.ERROR); alerta.setTimeout(Alert.FOREVER); getDisplay().setCurrent(get_formPrincipal()); } public void validarUsuario(String textFieldLogin, String textFieldSenha) { if(textFieldLogin.equals(" ") || textFieldSenha.equals(" ")) { tryAgain(); } else { loginPost(); } } public void tryAgain() { Alert error = new Alert("Erro", "Usuario/Senha não pode ser nulo", null, AlertType.ERROR); error.setTimeout(Alert.FOREVER); textFieldLogin.setString(""); textFieldSenha.setString(""); getDisplay().setCurrent(get_formPrincipal()); } private void loginPost() { Thread t = new Thread(new Runnable() { public void run() { HttpConnection http = null; DataOutputStream oStrm = null; DataInputStream iStrm = null; boolean ret = false; //os dados são passados como um fluxo separado para post String url = "http://localhost:8080/TesteSap/TesteSap"; try { //abrindo a conexao: http = (HttpConnection) Connector.open(url, Connector.READ_WRITE); oStrm = http.openDataOutputStream(); //aqui vem o pedido do cliente: //enviando o tipo de pedido: http.setRequestMethod(HttpConnection.POST); //enviando informações de cabeçalho: http.setRequestProperty("Content-Language","en-US"); http.setRequestProperty("User-Agent","Profile/MIDP-2.0, Configuration/CLDC-1.0"); http.setRequestProperty("Conexão", "close"); //enviando dados e corpo: //gravando o nome de login: byte data[] = ("login=" + textFieldLogin.getString()).getBytes(); oStrm.write(data); //gravando a senha: data = ("&senha=" + textFieldSenha.getString()).getBytes(); oStrm.write(data); oStrm.flush(); //aqui vem a resposta do servidor: iStrm = http.openDataInputStream(); ret = respostaServidor(http, iStrm); } catch (IOException e){ e.printStackTrace(); } finally { if(iStrm != null) try { iStrm.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(http != null) try { http.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ret == false) showAlert(errorMsg); } }); t.start(); } //aqui vem a resposta do servidor que é chamada no método loginPost... private boolean respostaServidor(HttpConnection http, InputStream iStrm) throws IOException { //aqui a mensagem de erro é reconfigurada: errorMsg = null; //aqui a linha de status é obtida if(http.getResponseCode() == HttpConnection.HTTP_OK) { //aqui é obtido o corpo de dados: int length = (int) http.getLength(); String str; if(length != -1) { byte servletData[] = new byte[length]; iStrm.read(servletData); str = new String(servletData); } else { ByteArrayOutputStream bStrm = new ByteArrayOutputStream(); int ch; while ((ch = iStrm.read()) != -1) bStrm.write(ch); str = new String(bStrm.toByteArray()); bStrm.close(); } getDisplay().setCurrent(get_formEscolha()); return true; } else //mensagem da servlet... errorMsg = new String(http.getResponseMessage()); return false; } public void startApp() { } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
Cissagatto
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)