HttpURLConnection を使用してサーブレットに接続する Java アプリケーションがあります。アプリケーションは、サーブレットに接続するときに、サーブレットに渡したいパラメーターを URL に埋め込みます。したがって、サーブレットは doGet() を使用してこれらのパラメーターにアクセスし、処理できます。これでこの部分は完了です (パラメーターにアクセスしてサーブレットで表示できます)。
次にやりたいことは、これらのパラメーターをサーブレットから JSP に渡すことです。私はそれをするために使用request.setAttribute()
しています。しかし、その後でもRequestDispatcherObj.forward(request, response)
、JSP は開きません。私も試してみましresponse.sendRedirect(url)
た。
ただし、サーブレットを個別に実行すると、上記のメソッド (forward() と sendRedirect()) の両方が正常に機能し、JSP ページが開きます。
私は何が間違っているのだろうか。
よろしくお願いします。
コード:
Java アプリ
serverAddress = new URL("http://localhost:8080/WebApp/ServletPath"+"?message1"+"="+message);
(HttpURLConnection)serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept-Charset", charset);
connection.setReadTimeout(10000);
connection.connect();
サーブレット
message = request.getParameter("message1");//working
request.setAttribute("message1", message);//to be read in the jsp
url="/index.jsp";
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);//Works when servlet is run independently but not when the servlet is called from the App
}