1

サーブレットの使用から応答を取得しようとしています

request.setAttribute(error, error);
request.getRequestDispatcher("http://localhost:8080/redicted_test/Home.jsp").forward(request, response);

String redictedURL="http://localhost:8080/redicted_test/Home.jsp";
response.sendRedirect(redictedURL);

しかし、エラーが発生します

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

私は私が応答を2回送信することを理解していますが、他にどのようにそれを行うことができますか?最も簡単な方法を教えてもらえますか?

4

4 に答える 4

1

あなたの例では、jspを実行し、レンダリングされたjspが応答バッファに送信されました。また、応答の送信を開始したため、応答ヘッダーが送信されました。しかし、その直後にリダイレクトを送信する必要があり、リダイレクトは一種のヘッダー操作です(httpステータスコードとロケーションヘッダーを変更します)。

于 2012-06-06T07:28:08.787 に答える
1

すでにリクエストを転送し、リダイレクトしています

request.getRequestDispatcher("http://localhost:8080/redicted_test/Home.jsp").forward(request, response);
..
response.sendRedirect(redictedURL);

サーブレットからいくつかの属性を設定し、それをjspに表示する必要がある場合は、リクエストをjspに転送して、それらの属性を表示するだけです。

于 2012-06-06T07:26:19.883 に答える
1

include私はあなたがではなく使用する必要があると思いforwardます..

forward is used to forward a request, that is control is transfered to the
new servler/jsp. u shud take care to not put any our.println() statements 
in the servlet from where u plan to call forward method. in fact u cant even 
fire response.getWriter() method in this case. u can only do that in the servlet
to which the control is bein fwded 

リクエストを別のサーブレットまたはJSPに転送する場合は、応答するものを記述しないでください。たとえば、Servlet1がリクエストをServlet2に転送する場合、Servlet1は応答に何も書き込まないでください。また、response.getOutputStream()メソッドも呼び出さないでください。これは許可されておらず、servlet1が応答に書き込んだものは出力されないか、IllegalStateExceptionが発生します。 。

include - means that the new servlet/jsp will be procesed and any
out.println()/html stuff will be included and then control will come
back to the servlet/jsp that called include method. 
于 2012-06-06T07:35:28.860 に答える
0

の後RequestDispatcher.forward()に、応答がコミットされて閉じられます。応答にヘッダー/コンテンツを書き込むことはできません。

そのため、実行できないredirect、またはdo operation which adds header or response content実行した後RequestDispatcher.forward()です。

代わりに、転送の代わりにインクルードを使用することもできます。

しかし、私はあなたのユースケースを理解していませんでした。同じページに転送およびリダイレクトしています。

于 2012-06-06T07:35:34.587 に答える