管道木杔:帮我看看代码的问题

来源:百度文库 编辑:高校问答 时间:2024/05/10 19:14:21
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
* 利用POST发送request的sample
* 从控制台输出response
*/
public class POSTTest extends MIDlet {

/**
* 利用POST送信息
*/
protected void startApp() throws MIDletStateChangeException {

try {
HttpConnection con = (HttpConnection)Connector.open("http://localhost/ReverseServlet");

//指定POST
con.setRequestMethod(HttpConnection.POST);

//在request中输入数据
String message = "message=helloworld";
DataOutputStream dos = con.openDataOutputStream();
byte[] messageByte = message.getBytes();
for(int i=0;i < messageByte.length;i++){
dos.writeByte(messageByte[i]);
System.out.println(messageByte[i]);
}
dos.flush();
dos.close();

//接收response
DataInputStream in = con.openDataInputStream();
int input;
while((input = in.read())!=-1){
System.out.print((char)input);
}
in.close();

//关闭链接
con.close();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
}

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 把接收的文字列进行转发的SERVLET
*/
public class ReverseServlet extends HttpServlet {

/**
* 处理post request
*/
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

//接收参数
String message = req.getParameter("message");
System.out.println("message=" + message);
//转发文字
String reverse = message;

//写response
PrintWriter out = res.getWriter();
out.write("<html>\n");
out.write("<head>\n");
out.write("<title>Request is POST.</title>\n");
out.write("</head>\n");
out.write("<body>\n");
out.write("message is "+message+"<br>\n");
out.write("Reverse Message is "+reverse+"\n");
out.write("</body>\n");
out.write("</html>");
out.close();
}
}

为什么getParameter获取的值为null?