ここでは、私のJavaサーブレットとajaxリクエストの間に通信の問題があります。それについての詳細:(ジャバスクリプト/ AJAX要求を含む)のindex.htmlにサーブレットとAJAX通信
絶対パス:同じフォルダに http://localhost:9080/chat/index.html
サーブレット: MyChat.class
、要求は次のように働いていますこれは:
var url = "http://localhost:9080/chat";
var name = document.getElementById( "username")。getAttribute( "value"); var message = document.getElementById( "message")。getAttribute( "value");
var tosend = name + "、" + message;
request.open( 'GET'、url、true); request.send(tosend); request.onreadystatechange = interpretRequest;
私はユーザーが名前とメッセージだけを入力し、 "username"と "message"はHTMLファイルのタグである形式を持っています。 Ajaxリクエストは機能しますが、サーブレットと通信しません。私はまた、System.out.println()からの出力がどこにあるのか考えていません。ログファイルがいっぱいではありません。サーブレットは次のようになります。
パブリッククラスMyChat extends HttpServlet { private static final long serialVersionUID = 1L;
private ArrayList<String> myMessages = new ArrayList<String>();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
BufferedReader r = request.getReader();
while(r.readLine() != null)
{
// split the words at the ','
String[] tmp = r.readLine().split(".\\s");
myMessages.add(tmp[0]+" "+tmp[1]);
}
//response.setContentType("text/html");
PrintWriter out = response.getWriter();
Iterator<String> it = myMessages.iterator();
while(it.hasNext())
{
out.println(it.next());
System.out.println(it.next());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
アドレスバーからサーブレットを呼び出して、問題がJavaScriptかサーブレットにあるかどうかを確認してください。 (ローカルホスト:9080/chat/index.html?username = Lenniのようなもの) –
は、入力タグからユーザー名を取得しているため動作しません。次のようになります。 – Andre
さて、ちょっとダブってHello Worldを返してみてください。 –