Servlets 未从servlet获取midlet中的值

Servlets 未从servlet获取midlet中的值,servlets,java-me,Servlets,Java Me,我试图将数据从midlet发送到servlet,并从servlet接收到响应,但响应中没有任何值。我试着在命令窗口上打印它,它看起来是空的,但我期望只有两个值“ok”或“error”。请帮我检查代码,让我知道我应该怎么做才能在midlet上获得正确的响应。我的代码如下: import javax.microedition.midlet.*;//midlet class package import import javax.microedition.lcdui.*;//package

我试图将数据从midlet发送到servlet,并从servlet接收到响应,但响应中没有任何值。我试着在命令窗口上打印它,它看起来是空的,但我期望只有两个值“ok”或“error”。请帮我检查代码,让我知道我应该怎么做才能在midlet上获得正确的响应。我的代码如下:

    import javax.microedition.midlet.*;//midlet class package import  
import javax.microedition.lcdui.*;//package for ui and commands  
import javax.microedition.io.*;//  
import java.io.*;  

/** 
 * @author k'owino 
 */  
//Defining the midlet class  
public class MvsMidlet extends MIDlet implements CommandListener {  

    private boolean midletPaused = false;//variable for paused state of midlet  
    //defining variables  
    private Display display;// Reference to Display object  
    private Form welForm;  
    private Form vCode;//vote code  
    private StringItem welStr;  
    private TextField phoneField;  
    private StringItem phoneError;  
    private Command quitCmd;  
    private Command contCmd;  

    //constructor of the midlet  
    public MvsMidlet() {  

        display = Display.getDisplay(this);//creating the display object  
        welForm = new Form("THE MVS");//instantiating welForm object  
        welStr = new StringItem("", "Welcome to the MVS, Busitema's mobile voter."  
                + "Please enter the your phone number below");//instantiating welStr string item  
        phoneError = new StringItem("", "");//intantiating phone error string item  
        phoneField = new TextField("Phone number e.g 0789834141", "", 10, 3);//phone number field object  
        quitCmd = new Command("Quit", Command.EXIT, 0);//creating quit command object  
        contCmd = new Command("Continue", Command.OK, 0);//creating continue command object  
        welForm.append(welStr);//adding welcome string item to form  
        welForm.append(phoneField);//adding phone field to form  
        welForm.append(phoneError);//adding phone error string item to form  
        welForm.addCommand(contCmd);//adding continue command  
        welForm.addCommand(quitCmd);//adding quit command  
        welForm.setCommandListener(this);  
        display.setCurrent(welForm);  

    }  

    //start application method definition  
    public void startApp() {  
    }  

    //pause application method definition  
    public void pauseApp() {  
    }  

    //destroy application method definition  
    public void destroyApp(boolean unconditional) {  
    }  

    //Command action method definition  
    public void commandAction(Command c, Displayable d) {  
        if (d == welForm) {  
            if (c == quitCmd) {  
                exitMidlet();//call to exit midlet  
            } else {//if the command is contCmd  
                //place a waiting activity indicator  
                System.out.println("ken de man");  
                Thread t = new Thread() {  

                    public void run() {  
                        try {  

                            //method to connect to server  
                            sendPhone();  

                        } catch (Exception e) {  
                        }//end of catch  
                    }//end of ru()  
                };//end of thread  
                t.start();//starting the thread  
            }//end of else  
        }//end of first if  

    }//end of command action  

    //defining method to exit midlet  
    public void exitMidlet() {  
        display.setCurrent(null);  
        destroyApp(true);  
        notifyDestroyed();  
    }//end of exitMidlet()  

    //defining sendPhone method  
    public void sendPhone() throws IOException {  

        System.out.println("ken de man");//check  

        HttpConnection http = null;//HttpConnection variable  
        OutputStream oStrm = null;//OutputStream variable  
        InputStream iStrm = null;//InputStream variable  
        String url = "http://localhost:8084/MvsWeb/CheckPhone";//server url  

        try {  
            http = (HttpConnection) Connector.open(url);//opening connection  
            System.out.println("connection made");//checking code  
            oStrm = http.openOutputStream();//opening output stream  
            http.setRequestMethod(HttpConnection.POST);//setting request type  
            http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//setting content type  
            byte data[] = ("phone=" + phoneField.getString()).getBytes();  
            oStrm.write(data);  
            iStrm = http.openInputStream();//openning input stream  
            if (http.getResponseCode() == HttpConnection.HTTP_OK) {  
                int length = (int) http.getLength();  
                String str;  
                if (length != -1) {  
                    byte servletData[] = new byte[length];  
                    iStrm.read(servletData);  
                    str = new String(servletData);  
                } else // Length not available...  
                {  
                    ByteArrayOutputStream bStrm = new ByteArrayOutputStream();  

                    int ch;  
                    while ((ch = iStrm.read()) != -1) {  
                        bStrm.write(ch);  
                    }  

                    str = new String(bStrm.toByteArray());  
                    bStrm.close();  
                }  
                System.out.println("de man");  

                System.out.println(str);  

                if (str.equals("ok")) {  
                    //change to vcode form  

                } else {  
                    //add error message to phone_error stingItem  
                }  
            }  
        } catch (Exception e) {  
        }  
    }  
}//end of class definition  


//servlet  
import java.io.*;//package for io classes  
import javax.servlet.ServletException;//package for servlet exception classes  
import javax.servlet.http.*;  
import java.sql.*;//package for sql classes  

/** 
 * 
 * @author k'owino 
 */  
public class CheckPhone extends HttpServlet {  

    @Override  
    protected void doGet(HttpServletRequest req, HttpServletResponse res)  
    throws ServletException, IOException {  

    }   


    @Override  
    protected void doPost(HttpServletRequest req, HttpServletResponse res)  
    throws ServletException, IOException {  

        //declaring variables  
        PrintWriter pw;//PrintWriter object  
        String pnum;//phone parameter sent from client  
        Connection con = null;//connection variable  
        String dbDriver = "com.jdbc.mysql.Driver";//the database driver  
        String dbUrl = "jdbc:mysql://localhost/mvs_db";//the database url  
        String dbUser = "root";//database user  
        String dbPwd = "";//database password  
        PreparedStatement pstmt = null;//Prepared statement variable  
        String query = "select * from student where stud_phone = ?;";  
        ResultSet rs = null;//resultset variable  
        String dbPhone=null;//phone from database  

        //getting the "phone" parameter sent from client  
        pnum = req.getParameter("phone");//getting the "phone" parameter sent from client  
        System.out.println(pnum);  

        //setting the content type of the response  
        res.setContentType("text/html");  

        //creating a PrintWriter object  
        pw = res.getWriter();  
        pw.println("ken");  

        try{//trying to load the database driver and establish a connection to the database  

            Class.forName(dbDriver).newInstance();//loading the database driver  
            con = DriverManager.getConnection(dbUrl,dbUser,dbPwd);//establishing a connection to the database  
            System.out.println("connection established");  
        }  
        catch(Exception e){  

        }  

        //trying to query the database  
        try{  

            pstmt = con.prepareStatement(query);//preparing a statement  
            pstmt.setString(1, pnum);//setting the input parameter  
            rs = pstmt.executeQuery();//executing the query assigning to the resultset object  

            //extracring data from the resultset  
            while(rs.next()){  
                dbPhone = rs.getString("stud_phone");//getting the phone number from database  
            }//end of while  

            if(pnum.equals(dbPhone)){  
                pw.print("ok");  
                pw.close();  
            }  
            else{  
                pw.print("error");  
                pw.close();  
            }  

        }  
        catch(Exception e){  

        }  

    }  


    @Override  
    public String getServletInfo() {  
        return "Short description";  
    }// </editor-fold>  

}  
import javax.microedition.midlet.*//midlet类包导入
导入javax.microedition.lcdui.*//ui和命令包
导入javax.microedition.io.*;//
导入java.io.*;
/** 
*@作者科维诺
*/  
//定义中产阶级
公共类MvsMidlet扩展MIDlet实现CommandListener{
私有布尔值midletPaused=false;//用于midlet暂停状态的变量
//定义变量
私有显示;//对显示对象的引用
私人形式;
私有表单vCode;//投票代码
私人物品福利;
私有文本字段和电话字段;
私有字符串项错误;
私有命令quitCmd;
私有命令contCmd;
//midlet的构造函数
公共MvsMidlet(){
display=display.getDisplay(this);//创建显示对象
welForm=新表单(“MVS”);//实例化welForm对象
welStr=new StringItem(“,”欢迎来到MVS,Busitema的移动投票者。“
+“请在下面输入您的电话号码”);//实例化welStr字符串项
phoneError=new StringItem(“,”);//表示电话错误字符串项
phoneField=newtextfield(“电话号码,例如0789834141”,“”,10,3);//电话号码字段对象
quitCmd=new命令(“Quit”,Command.EXIT,0);//正在创建Quit命令对象
contCmd=new命令(“Continue”,Command.OK,0);//创建Continue命令对象
append(welStr);//将欢迎字符串项添加到表单
append(phoneField);//将phone字段添加到表单
append(phoneError);//将电话错误字符串项添加到表单
welForm.addCommand(contCmd);//添加continue命令
welForm.addCommand(quitCmd);//添加quit命令
setCommandListener(this);
display.setCurrent(welForm);
}  
//启动应用程序方法定义
public void startApp(){
}  
//暂停应用程序方法定义
public void pauseApp(){
}  
//销毁应用程序方法定义
公共应用程序(布尔无条件){
}  
//命令操作方法定义
公共无效命令操作(命令c,可显示d){
如果(d==welForm){
如果(c==quitCmd){
exitMidlet();//调用exit midlet
}else{//如果命令是contCmd
//放置等待活动指示器
System.out.println(“ken de man”);
线程t=新线程(){
public void run(){
试试{
//方法连接到服务器
sendPhone();
}捕获(例外e){
}//捕获结束
}//ru()的结尾
};//线程结束
t、 start();//启动线程
}//别有用心
}//第一个if结束
}//命令结束动作
//定义退出midlet的方法
public void exitMidlet(){
display.setCurrent(空);
销毁应用程序(真);
通知销毁();
}//exitMidlet()的结尾
//定义sendPhone方法
public void sendPhone()引发IOException{
System.out.println(“ken de man”);//检查
HttpConnection http=null;//HttpConnection变量
OutputStream oStrm=null;//OutputStream变量
InputStream iStrm=null;//InputStream变量
字符串url=”http://localhost:8084/MvsWeb/CheckPhone“;//服务器url
试试{
http=(HttpConnection)连接器。打开(url);//打开连接
System.out.println(“建立连接”);//检查代码
oStrm=http.openOutputStream();//打开输出流
http.setRequestMethod(HttpConnection.POST);//设置请求类型
http.setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded”);//设置内容类型
字节数据[]=(“phone=“+phoneField.getString()).getBytes();
ostm.write(数据);
iStrm=http.openInputStream();//打开输入流
如果(http.getResponseCode()==HttpConnection.http_OK){
int length=(int)http.getLength();
字符串str;
如果(长度!=-1){
字节servletData[]=新字节[长度];
iStrm.read(servletData);
str=新字符串(servletData);
}否则//长度不可用。。。
{  
ByteArrayOutputStream BSTR=新建ByteArrayOutputStream();
int-ch;
而((ch=iStrm.read())!=-1){
bStrm.write(ch);
}  
str=新字符串(bStrm.toByteArray());
bStrm.close();
}  
系统输出打印(“de man”);
系统输出打印项次(str);
如果(str.equals(“ok”){
//更改为vcode格式
}否则{
//将错误消息添加到电话\错误项
}  
}  
}捕获(例外e){
}  
}  
}//类结束定义
//servlet
导入java.io.*//io类的软件包
导入javax.servlet.ServletException//servlet EXE的包