0

私はJSPに非常に慣れていないので、ご容赦ください。よろしくお願いします。大変感謝しています。

バックグラウンド

ユーザーのログイン セッション中に特定の Java オブジェクトを「記憶」する Web アプリケーションを構築しようとしています。現在、RequestDispatcher を使用して doPost メソッドを介して JSP ページにオブジェクトを送信するサーブレットがあります。

サーブレットの doPost は次のとおりです。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws     ServletException, IOException {

    String strBook = "Test Book";

    // Send strBook to the next jsp page for rendering
    request.setAttribute("book", strBook);
    RequestDispatcher dispatcher = request.getRequestDispatcher("sample.jsp");
    dispatcher.include(request, response);

}

sample.jsp は strBook オブジェクトを取得して処理します。sample.jsp の本体は次のようになります。

<body>

<% 
    // Obtain strBook for rendering on current page
    String strBook = (String) request.getAttribute("book"); 

    /*
      // TODO: A way to conditionally call code below, when form is submitted 
      // in order for sample2.jsp to have strBook object.

      RequestDispatcher dispatcher = request.getRequestDispatcher("sample2.jsp");
      dispatcher.include(request, response);
    */

%>


<h1> Book: </h1>
<p> <%=strBook%> </p>

<form action="sample2.jsp"  method="post">
    <input type="submit" id="buttonSubmit" name="buttonSubmit" value="Buy"/>
</form>

</body>

問題

送信ボタンがクリックされたときに、sample.jsp 内から strBook オブジェクトを取得し、それを sample2.jsp に送信するにはどうすればよいですか(strBook オブジェクトを sample2.jsp で利用できるようにするため)。

現在、sample2.jsp の本体は次のようになっており、その中の strBook は null です。

<body>

<% 
    // Obtain strBook for rendering on current page
    String strBook = (String) request.getAttribute("book"); 
%>


<h1> Book: </h1>
<p> <%="SAMPLE 2 RESULT " + strBook%> </p>

</body>
4

1 に答える 1