使用post方式进行applet通信 [英] applet communication using post method
问题描述
我有一个与 servlet 通信的小程序.我正在使用 POST 方法与 servlet 通信.我的问题是如何将参数发送到 servlet.使用 GET 方法,这相当简单(我只是将参数附加到 URL 之后?).但是使用POST方法如何发送参数,以便在servlet端,我可以使用语句:
I have an applet that is communicating with a servlet. I am communicating with the servlet using POST method. My problem is how do I send parameters to the servlet. Using GET method, this is fairly simple ( I just append the parameters to the URL after a ?). But using POST method how do I send the parameters, so that in the servlet side, I can use the statement :
message = req.getParameter("msg");
在小程序端,我建立POST方法连接如下:
In the applet side, I establish POST method connection as follows :
URL url = new URL(getCodeBase(), "servlet");
URLConnection con = url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type","application/octet-stream");
推荐答案
首先,您需要调用(就像您所做的那样):
First, you need to call (as you did):
urlConnection.setDoOutput(true);
然后获取OutputStream
:
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
并写信给它:
out.write("paramName=" + paramValue);
在servlet中,可以调用request.getParameter("paramName")
In the servlet, you can call request.getParameter("paramName")
这篇关于使用post方式进行applet通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!