私はJavaとajaxとサーブレットが初めてです。ユーザーからの入力を読み取り、いくつかのチュートリアルの助けを借りて辞書の意味を Web ページに出力するプログラムを作成しました。
サーブレットの doPost メソッドで Web ページから入力を読み取ると、読み取りに失敗して null が返されます。送信ボタンの前に入力を読み取ろうとしている可能性があります。どうすればこれを解決できますか? 私のjspファイルの関連コードは次のとおりです。
function ajaxFunction() {
if(xmlhttp) {
var inword = document.getElementById("inputWord");
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.open("GET","gettime?inputWord="+ inword.value , true ); //gettime will be the servlet name
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(null);
}
}
...
<form name="myForm">
Enter the word: <input type="text" name="inputWord" />
<br />
Meaning:<input type="text" name="time" />
<br />
<input type="button" onClick="javascript:ajaxFunction();" value="Click to get the Meaning on Textbox"/>
<br />
</form>
そして、これが私のサーブレットで入力を取得しようとしているコードの一部です:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String inWord = request.getParameter("inputWord"); // word to search in the dictionary
PrintWriter out = response.getWriter();
...
プロジェクトを実行しようとするたびrequest.getParameter("inputWord");
に null が返されます。
xmlhttp.open("GET","gettime?inputWord="+ inword.value , true );
ここのコードのいくつかの組み合わせを試しましxmlhttp.open("GET","gettime", true );
たが、うまくいきませんでした。
doPost(request,response);
また、doGet メソッドを呼び出します。
どんな助けでも大歓迎です。