Java 在客户端发送的GoogleAWTServlet中读取数据
我想从桌面应用程序向google awt servelet发送一些数据Java 在客户端发送的GoogleAWTServlet中读取数据,java,networking,servlets,Java,Networking,Servlets,我想从桌面应用程序向google awt servelet发送一些数据 我目前正在我的系统(localhost)中尝试这一点,因为我已经安装了eclipse插件 我正在发送这样的数据 URL url = new URL("http://localhost:8888/calendar"); URLConnection urlConnection = url.openConnection(); urlConnection.setDoOutput(true); PrintWriter out = ne
我目前正在我的系统(localhost)中尝试这一点,因为我已经安装了eclipse插件
我正在发送这样的数据
URL url = new URL("http://localhost:8888/calendar");
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
String stringTosend = URLEncoder.encode(tf.getText(), "UTF-8");
out.write(stringTosend);
我的SERVLET是这样的
public class CalendarServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
String input = req.getReader().readLine();
resp.getWriter().println(input);
}
}
但我得到的是空的
URLConnection connection = new URL("http://localhost:8888/calendar").openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(stringTosend.getBytes(charset));
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
}
InputStream response = connection.getInputStream();
参考