jspから値を入力し、java(servlet)で処理を行い、また別のjspで表示させる処理を行うプログラムを作成しています。
値を表示させるところまではできたのですが、一度入力した値が上書きされてしまいます。上書きされることなく、残るようにしたいです。
以下、ソースです。
『Input.jsp』から入力し、
『Test.java』で処理を行い、
『Output.jsp』で出力をしようとしています。
ちなみに、入力画面と出力画面は同じページ内に上下で分けて表示する形式になっていますが、
今回はおそらく関係ないのでソース表示を省略しています。
sessionとArraylistを使用するのかな、と考えているのですが、
使い方がわかりません。
Test.java
public class Test extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { this.doPost(req, res); res.setContentType("text/html; charset=UTF-8"); } protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html; charset=UTF-8"); // テキストボックスからデータを受け取る String input = req.getParameter("param"); req.setAttribute("input", input); // 今までの入力分を取得 String[] oldData = req.getParameterValues("input"); String[] Array = oldData; int i = Array.length; if (Array == null) { Array[i] = ""; } HttpSession session = req.getSession(); req.setAttribute("oldData", oldData); RequestDispatcher dispathcer = req.getRequestDispatcher("/jsp/C18_HelloWeb_Output.jsp"); dispathcer.forward(req, res); } }
Input.jsp
<%@ page language="java" contentType="text/html; charset=windows-31j" pageEncoding="windows-31j"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <title>C18_HelloWeb_Input</title> <script type="text/javascript" charset=Shift-JIS> <!-- function startAlert(){ if (document.frm.param.value == "") alert('空です'); }} --> </script> </head> <body> <center> <font size=5px>入力画面</font> </center> <br> <form name = "frm" action="http://localhost:8080/Webtest/Test/url" method="POST" target="output"> <input type="text" name="param" size=20 onclick="startAlert()"> <input type="submit" value="クエリ送信"> </form> </body> </html>
Output.jsp
<%@ page language="java" contentType="text/html; charset=Shift_JIS" pageEncoding="Shift_JIS"%> <%@page import="java.util.*" import="java.io.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <title>C18_HelloWeb_Output</title> </head> <body> <center> <font size=5px>出力画面</font> </center> <br> <% if (request.getAttribute("input") != null) { %> <%=request.getAttribute("input")%> <% } %> <br> <% if (request.getAttribute("oldData") != null) { %> <%=request.getAttribute("oldData")%> <% } %> <!-- <%if (request.getAttribute("AllData") != null) {%> <%=request.getAttribute("AllData")%> <%}%> --> </body> </html>
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/10 14:09
2016/09/10 18:45