Почему метод doPost моего сервера чата не вызывается? - PullRequest
1 голос
/ 24 марта 2010

Я проектирую чат-сервер в Java. Связь основана на Http, а не на сокете. На стороне клиента у меня есть апплет. На стороне сервера у меня есть сервлет.

Апплет: я создаю новый поток для прослушивания входящих сообщений (метод GET). Основной поток используется для отправки сообщений (POST-сообщений).

Частичный код:

public void start() {
    System.out.println("Creating new thread");
    Thread thread = new Thread(this);
    thread.start();
}

private String getNewMessage() {
    System.out.println("Inside getNewMessage");
    String msg = null;
    try {
        while(msg == null) {
            System.out.println("Trying to listen to servlet");
            URL servlet = new URL(getCodeBase(), "NewServlet?mode=msg");
            URLConnection con = servlet.openConnection();

            con.setUseCaches(false);

            DataInputStream din = new DataInputStream(new BufferedInputStream(con.getInputStream()));
            msg = din.readUTF();
            System.out.println("message read :" + msg);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return msg + "\n";
}
public void run() {
    System.out.println("Inside new thread");
    while(true) {
        System.out.println("inside first while");
        String newMsg = getNewMessage();
        chatOutput.append(newMsg);
        System.out.println("Appended!!");
    }
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String message = chatInput.getText();
    chatInput.setText("");
    chatOutput.append(message + "\n");
    try {
        System.out.println("Trying to send msg :" + message);
        URL url = new URL(getCodeBase(), "NewServlet");
        URLConnection servletConnection = url.openConnection();

        servletConnection.setDoInput(true);
        servletConnection.setDoOutput(true);
        servletConnection.setUseCaches(false);
        servletConnection.setRequestProperty("Content-Type", "application/octet-stream");

        ObjectOutputStream out = new ObjectOutputStream(servletConnection.getOutputStream());
        out.writeObject(message);
        out.flush();
        out.close();

        System.out.println("Message sent!");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Этот следующий код со стороны сервлета. он использует интерфейс Observable для идентификации и отправки сообщений клиентам.

public class NewServlet extends HttpServlet {
// getNextMessage() returns the next new message.  // It blocks until there is one.
public String getNextMessage() {
// Create a message sink to wait for a new message from the
// message source.
  System.out.println("inside getNextMessage");
return new MessageSink().getNextMessage(source);}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    System.out.println("Inside Doget");
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();

    out.println(getNextMessage());
} 

// broadcastMessage() informs all currently listening clients that there
// is a new message. Causes all calls to getNextMessage() to unblock.
public void broadcastMessage(String message) {
// Send the message to all the HTTP-connected clients by giving the
// message to the message source
source.sendMessage(message);  }
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    System.out.println("Inside DoPost");
    try {
    ObjectInputStream din= new ObjectInputStream(request.getInputStream());
    String message = (String)din.readObject();

        System.out.println("received msg");
    if (message != null) broadcastMessage(message);
        System.out.println("Called broadcast");
// Set the status code to indicate there will be no response
    response.setStatus(response.SC_NO_CONTENT);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/** 
 * Returns a short description of the servlet.
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}

MessageSource source = new MessageSource();}

class MessageSource extends Observable {
public void sendMessage(String message) {
  System.out.println("inside sendMsg");
setChanged();
notifyObservers(message);
}
}

class MessageSink implements Observer {
String message = null;  // set by update() and read by getNextMessage()
// Called by the message source when it gets a new message
synchronized public void update(Observable o, Object arg) {
// Get the new message
message = (String)arg;
// Wake up our waiting thread
notify();
}
// Gets the next message sent out from the message source
synchronized public String getNextMessage(MessageSource source) {
// Tell source we want to be told about new messages
source.addObserver(this);
  System.out.println("AddedObserver");
// Wait until our update() method receives a message
while (message == null) {
  try { wait(); } catch (Exception ignored) { }
}
// Tell source to stop telling us about new messages
source.deleteObserver(this);
// Now return the message we received
// But first set the message instance variable to null
// so update() and getNextMessage() can be called again.
String messageCopy = message;
message = null;
  System.out.println("Returning msg");
return messageCopy;
}
}

Как видите, я включил System.out.println («Некоторые сообщения»); в некоторых местах. это было только для целей отладки. В консоли Java я получаю следующий вывод:

Создание новой темы
Внутри новая тема.
сначала внутри.
Внутри getNewMessage.
Пытаюсь послушать сервлет.

На стороне сервлета я получаю следующий вывод в логах tomcat:

Внутри Doget.
внутри getNextMessage.
AddedObserver.

После того, как я набрал сообщение в апплете и отправил его, я получил следующий вывод в консоли Java:

Пытаюсь отправить сообщение: вы deR ??
Сообщение отправлено!

Но на стороне сервлета Я ничего не вижу в логах . Я использовал программирование Java-сервлетов O'Reily в качестве эталона (интерфейс наблюдателя оттуда). Но я не общаюсь в чате между двумя клиентами. Как можно понять из журналов, метод doPOST не вызывается. В чем причина?

1 Ответ

1 голос
/ 24 марта 2010

Я исправил проблему, получив сообщение (сообщение о состоянии) после отправки сообщения на стороне апплета. На стороне сервлета, в методе doPost я отправил сообщение о состоянии ("1") после прочтения сообщения.

Я не знаю, как именно это решило проблему, но я предполагаю, что, поскольку у меня было setDoInput(true);, он ожидал какого-то сообщения для чтения.

В любом случае, хорошая новость заключается в том, что я по крайней мере получил желаемый результат вышеуказанного процесса отладки.

Кроме того, было необходимо использовать ObjectInputStream вместо DataInputStream в методе getNewMessage (поскольку сообщение было отправлено ObjectOutputStream). Теперь чат-сервер работает без сбоев.

...