метод аплета
Код | private void sendPostMessage(String message) throws UnknownHostException, IOException { String host = "localhost"; int port = 80; //веб-приложение "WebTest", сервлет "/Controller" String path = "WebTest/Controller"; String data = "передаваемые данные";
Socket s = new Socket(host, port);
String requestBody = "data=" + data; String request = "POST " + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + requestBody.length() + "\r\n\r\n" + requestBody;
OutputStream os = s.getOutputStream(); os.write(request.getBytes());
}
|
метод doPost() сервлета
Код |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Controller : doPost();"); String data = request.getParameter("data"); System.out.println(data);
}
|
метод doPost() не срабатывает... 
метод sendPostMessage(String message) вызывается нажатием кнопки в окне аплета
Код | public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) { JButton b = (JButton) e.getSource(); if (b.getName().equals("send")) {
try {
sendPostMessage(text.getText());
} catch (MalformedURLException ex) { Logger.getLogger(Viewer.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Viewer.class.getName()).log(Level.SEVERE, null, ex); } }
} }
|
|