• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

HOW TO COMMUNICATE JAVA APPLICATION TO SERVLETS

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please send a code to communication with javaapplication to servlets r_ramchander@yahoo.com [CODE]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import java.lang.*;
import java.lang.Thread;

public class Authenticate extends MIDlet implements CommandListener,Runnable {

Display display = null;

TextField txtUserID;
TextField txtPassword;
String strBlank;
Form Form;

String url = "http://localhost/authenticate.jsp";

static final Command cmdLogin = new Command("Login", Command.OK, 2);
static final Command cmdClear = new Command("clear", Command.STOP, 2);

String clientUserID, clientPassword;

public Authenticate(){

display = Display.getDisplay(this);
txtUserID = new TextField("Enter UserID:","",15, TextField.ANY);
txtPassword = new TextField("Enter Password:","",15, TextField.PASSWORD);
form = new Form("Authenticate Urself");
strBlank = new String(" ");
}

public void startApp() throws MIDletStateChangeException{

form.append(txtUserID);
form.append(strBlank);
form.append(txtPassword);
form.addCommand(cmdClear);
form.addCommand(cmdLogin);
form.setCommandListener(this);
display.setCurrent(form);

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

notifyDestroyed();
}

void invokeJSP(String url) throws IOException {

HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
StringBuffer b = new StringBuffer();

TextBox txtDetails = null;

try{

c = (HttpConnection)Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("IF-Modified-Since", "25 Nov 2001 15:17:19 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();
os.write(("&UserID=" + clientUserID).getBytes());
os.write(("&Password=" + clientPassword).getBytes());

is = c.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {

b.append((char) ch);
System.out.print((char)ch);
}
txtDetails = new TextBox("", b.toString(), 1024, 0);
txtDetails.setCommandListener(this);
is.close();
os.close();
c.close();
}
catch(Exception exc){

System.out.println(exc.toString());
}
display.setCurrent(txtDetails);
}

public void commandAction(Command c, Displayable d) {

String label = c.getLabel();
if(label.equals("clear")) {

destroyApp(true);
}
else if (label.equals("Login")) {

System.out.println(url);
Thread t = new Thread(this);
t.start();
System.out.println(url);
}
}

public void run(){

clientUserID = txtUserID.getString();
clientPassword = txtPassword.getString();
clientUserID = Trim(clientUserID);
clientPassword = Trim(clientPassword);
try{

System.out.println(clientUserID + "\t" + clientPassword);
invokeJSP(url);
}
catch(IOException e) {
}
}

String LTrim(String str)
{
String whitespace = new String(" \t\n\r");

String s = new String(str);

if (whitespace.indexOf(s.charAt(0)) != -1) {

int j=0, i = s.length();

while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
j++;

// Get the substring from the first non-whitespace
// character to the end of the string...
s = s.substring(j, i);
}
return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
String RTrim(String str)
/*
PURPOSE: Remove trailing blanks from our string.
IN: str - the string we want to RTrim

*/
{
// We don't want to trip JUST spaces, but also tabs,
// line feeds, etc. Add anything else you want to
// "trim" here in Whitespace
String whitespace = new String(" \t\n\r");

String s = new String(str);

if (whitespace.indexOf(s.charAt(s.length()-1)) != -1) {
// We have a string with trailing blank(s)...

int i = s.length() - 1; // Get length of string

// Iterate from the far right of string until we
// don't have any more whitespace...
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
i--;


// Get the substring from the front of the string to
// where the last non-whitespace character is...
s = s.substring(0, i+1);
}

return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
String Trim(String str)
/*
PURPOSE: Remove trailing and leading blanks from our string.
IN: str - the string we want to Trim

RETVAL: A Trimmed string!
*/
{
return RTrim(LTrim(str));
}


}//end class
 
Joel Karimpil
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry...am in a hurry so i'm pasting the whole code.
 
Skool. Stay in. Smartness. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic