Wai Le

Greenhorn
+ Follow
since Mar 17, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Wai Le

Hi all,
I would like to know how can a servlet return an array to the midlet?
I have seen many example which just simply return a string to the midlet.
Can someone gives me some tips about it and I will be glad about it.Thx
19 years ago
Hi all,
I would like to know that how can a servlet return a object(i.e array of string) to a midlet?
I found that a midlet cannot support the ObjectInputStream/ObjectOutputStream class.
So, is it any other types of Stream is spporting this feature.
19 years ago
Hi all,
I have wirte a midlet which simply give 2 parameters (user & pwd) to the servlet which is for authenticating the user to retrieve his/her e-mail inbox account.
the coding is below:
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class ServletMailLogin extends HttpServlet{
String user,pwd;
public void doPost(HttpServletRequest req ,HttpServletResponse res) throws IOException, ServletException
{res.setContentType("text/plain");
PrintWriter out = res.getWriter();
user = req.getParameter("user");
pwd = req.getParameter("pwd");

try{
Properties props = new Properties();
//place the authentication info in
props.put("mail.smtp.host","thomaslee1121.no-ip.com");
props.put("mail.smtp.host","true");
// create an empty authenticator object
Authenticator auth = new SMTPAuthenticator();
// Get a session - pass auth object
Session session = Session.getDefaultInstance(props, auth);
Store store = session.getStore("smtp");
// do not pass any arguments to the connect method
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message messages[] = folder.getMessages();
for (int i=0; i<messages.length; i++) {
out.print(i + ": " + messages[i].getFrom()[0] + "\t" + messages[i].getSubject() + "\t" + messages[i].getSentDate() + "\n\n");
}
folder.close(false);
store.close();
}
catch (MessagingException me) {
System.err.print(user);
}


}

private class SMTPAuthenticator extends javax.mail.Authenticator{
public PasswordAuthentication getPasswordAuthentication(){
String username,password;

username = ServletMailLogin.this.user;
password = ServletMailLogin.this.pwd;
return new PasswordAuthentication(username,password);
}
}
}
compliation is not problem but it will just return me a empty(not null!!) content to the emulator.
Can someone give me advice PLS??
19 years ago
Hi all,
I found some problems that I want to use the emulator to login the mail(smtp) server.
I have a conole example which can successfully login the server and retrieve the mail in different mail box, however, when I "convert" the source to the servlet code, it prompted me so many error.
Lastly, I hope that there are some similar example for the reference. Thx
19 years ago
Actually do you have any examples which is similar to my coding?
19 years ago
import javax.microedition.rms.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Vector;
public class SecondMidletServlet extends MIDlet implements CommandListener {
Display display = null;
List menu = null;
TextBox input = null;
String user = null;
String url = "http://localhost:8080/examples/servlet/RequestServlet";
static final Command backCommand = new Command("Back", Command.BACK, 0);
static final Command submitCommand = new Command("Submit", Command.OK, 2);
static final Command exitCommand = new Command("Exit", Command.STOP, 3);
String currentMenu = null;
public SecondMidletServlet() {
}
public void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
menu = new List("Invoke Servlet", Choice.IMPLICIT);
menu.append("Add a user", null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
mainMenu();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
void mainMenu() {
display.setCurrent(menu);
}
public void addName() {
input = new TextBox("Enter first name:", "", 5, TextField.ANY);
input.addCommand(submitCommand);
input.addCommand(backCommand);
input.setCommandListener(this);
input.setString("");
display.setCurrent(input);
}
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 Jan 2001 16:19:14 GMT");
c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-CA");

os = c.openOutputStream();
os.write(("name="+user).getBytes());
System.out.println("Length: "+("name="+user).getBytes());

// or you can easily do:
// os.write(("name="+user).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("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);
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("Exit")) {
destroyApp(true);
} else if (label.equals("Back")) {
mainMenu();
} else if (label.equals("Submit")) {
user = input.getString();
try {
invokeServlet(url);
}catch(IOException e) {}
} else {
addName();
}
}
}
19 years ago
Hi Mark,
I replace a part of code with the code you provided of the previous post, but it cannot still work, it will return the null value me.
Any suggestions??
19 years ago
Hi all,
I wrote a simple midlet which just pass the parameter to the servlet and the servlet will return the parameter to the midlet.
I am sure that the coding of midlet is correct. The coding below is the servlet I modified. Can someone tell me that is the mistake
of it?
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
Public class RequestServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
String user = (String)request.getParameter("user");
out.print("Rec: "+user);
}

}
19 years ago