这个是我客户端的程序,主要是通过servlet向它后台传递参数:
这个是我的服务器端的程序:
这个是服务器端输出:
但我们看到,调用String message = request.getParameter("id");得到的是null,但我把url改成:
这样就能够正确的输出message了。 但根据request的GET和POST请求,应该可以用POST方式传参数,我看了一下SUN网站上的一个例子,他是用字符串解析的,难道没有好的方法吗?
[url]http://java.sun.com/docs/books/tutorial/networking/urls/examples/ReverseServlet.java[/url]
但我看到J2ME的HttpConnection,好像可以实现。
[url]http://www.java-tips.org/java-me-tips/midp/sending-data-to-servlet-and-getting-the-response.html[/url]
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class Test {
public static void main(String[] args) throws IOException {
String urlStr = "http://localhost:9999/ServletTest/TestServlet";
URLConnection connection = null;
OutputStream oStream = null;
InputStream iStream = null;
boolean result = false;
try {
URL url = new URL(urlStr);
connection = url.openConnection();
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection
.getOutputStream());
out.write("id=1111");
out.close();
connection.connect();
iStream = connection.getInputStream();
result = processServerResponse((HttpURLConnection)connection, iStream);
System.out.println(result);
} finally {
if (iStream != null)
iStream.close();
if (oStream != null)
oStream.close();
}
}
private static boolean processServerResponse(HttpURLConnection http,
InputStream iStrm) throws IOException {
String errorMsg = null;
if (http.getResponseCode() == HttpURLConnection.HTTP_OK) {
int length = http.getContentLength();
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();
}
System.out.println(str);
return true;
} else {
errorMsg = new String(http.getResponseMessage());
}
return false;
}
}
这个是我的服务器端的程序:
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class TestServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream in = request.getInputStream();
byte[] texts = new byte[request.getContentLength()];
in.read(texts);
System.out.println(new String(texts));
String message = request.getParameter("id");
System.out.println("message"+message);
try {
message += " 200 ok";
} catch (Throwable t) {
message += " 100 " + t.toString();
}
response.setContentType("text/plain");
response.setContentLength(message.length());
PrintWriter out = response.getWriter();
out.println(message);
in.close();
out.close();
out.flush();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
这个是服务器端输出:
id=1111
messagenull
但我们看到,调用String message = request.getParameter("id");得到的是null,但我把url改成:
String urlStr = "http://localhost:9999/ServletTest/TestServlet?id=1111";
这样就能够正确的输出message了。 但根据request的GET和POST请求,应该可以用POST方式传参数,我看了一下SUN网站上的一个例子,他是用字符串解析的,难道没有好的方法吗?
[url]http://java.sun.com/docs/books/tutorial/networking/urls/examples/ReverseServlet.java[/url]
但我看到J2ME的HttpConnection,好像可以实现。
[url]http://www.java-tips.org/java-me-tips/midp/sending-data-to-servlet-and-getting-the-response.html[/url]