0

I want to send some data from desktop application to google awt servelet
I am currently trying this in my system (localhost) as I have installed eclipse plugin
I am sending data like this

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);

AND MY SERVLET IS LIKE THIS

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);
    }
}

But what I am getting back is NULL.

Buhake Sindi
  • 82,658
  • 26
  • 157
  • 220

1 Answers1

1
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();

Reference

Community
  • 1
  • 1
jmj
  • 225,392
  • 41
  • 383
  • 426