実行ボタンを押すと入力された値をリストに格納して、リストの要素を全て表示するというふうにしたいのですがうまくいかず詰まってしまいました。スコープはアプリケーションを使いたいです。
どのように記述を直せばいいのかご教授願いたいです。
public class test extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("JISAutoDetect"); ServletContext application = getServletContext(); ArrayList list = (ArrayList)application.getAttribute("scope1"); String scope = request.getParameter("scope"); list.add(scope); application.setAttribute("scope1", list); RequestDispatcher rd = request.getRequestDispatcher("●.jsp"); rd.forward(request, response); } }
jsp
1<%@ page language="java" contentType="text/html; charset=windows-31j" 2 pageEncoding="windows-31j"%> 3<%@ page import="java.util.ArrayList"%> 4<% 5ArrayList list = (ArrayList) application.getAttribute("scope1"); 6%> 7 8<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9<html> 10<head> 11<meta http-equiv="Content-Type" content="text/html; charset=windows-31j"> 12<title></title> 13</head> 14<body> 15<form action="test" method="POST"> 16<input type="text" name="scope"> 17<input type="submit" value="送信"> 18<% 19 for (int i = 0; i < list.size(); i++) { 20%> 21<%=list.get(i)%> 22<br /> 23<% 24 } 25%> 26</form> 27</body> 28</html>
とりあえず自分のおもうようにやってみな
回答1件
あなたの回答
tips
プレビュー