<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ page import="trail.injection.*, javax.naming.*, java.text.*,
                 javax.jms.*,
                 java.sql.Timestamp"%>

<%
  if ("send".equals(request.getParameter ("action"))) {

    QueueConnection cnn = null;
    QueueSender sender = null;
    QueueSession sess = null;
    Queue queue = null;

    try {
      InitialContext ctx = new InitialContext();
      queue = (Queue) ctx.lookup("queue/injection");
      QueueConnectionFactory factory =
          (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
      cnn = factory.createQueueConnection();
      sess = cnn.createQueueSession(false,
                  QueueSession.AUTO_ACKNOWLEDGE);

    } catch (Exception e) {
      e.printStackTrace ();
    }

    TextMessage msg = sess.createTextMessage(
        request.getParameter ("start") + "," +
        request.getParameter ("end") + "," +
        request.getParameter ("growthrate") + "," +
        request.getParameter ("saving")
    );
    // The sent timestamp acts as the message's ID
    long sent = System.currentTimeMillis();
    msg.setLongProperty("sent", sent);

    sender = sess.createSender(queue);
    sender.send(msg);
    // sess.commit ();
    sess.close ();
%>

<html>
<head><meta http-equiv="REFRESH" content="3;
                  URL=check.jsp?sent=<%=sent%>"></head>
  <body>
    しばらくお待ちください。メッセージが届いているか確認中です。<br/>
    <a href="calculator.jsp">計算プログラムに戻る</a>
  </body>
</html>

<%
    return;

  } else {

    int start = 25;
    int end = 65;
    double growthrate = 0.08;
    double saving = 300.0;
%>

<html>
<body>
<p>投資計算プログラム<br/>
<form action="calculator.jsp" method="POST">
  <input type="hidden" name="action" value="send">
  開始年齢 = <input type="text" name="start" value="<%=start%>"><br/>
  終了年齢 = <input type="text" name="end" value="<%=end%>"><br/>
  年成長率 = <input type="text" name="growthrate" value="<%=growthrate%>"><br/>
  月掛金額 = <input type="text" name="saving" value="<%=saving%>"><br/>
  <input type="submit" value="計算">
  <INPUT type="button" value="閉じる" onClick="window.close()">

</form>
</p>
</body>
</html>

<%
    return;
  }
%>