登録画面を踏んだ回数を表示したいのですがnullになってしまいます。
何回か登録画面を踏んでも「登録画面を踏んだ回数null」と出てしまいます。
説明が足りないろころがありましたら教えてください!
ホーム
JSP
1<?xml version="1.0" encoding="UTF-8" ?> 2<%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5<html xmlns="http://www.w3.org/1999/xhtml"> 6<head> 7<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 8<title>Home</title> 9</head> 10<body> 11 <h1>ようこそ</h1> 12 <hr/> 13 <p><a href="login.jsp">ログイン</a></p> 14 <p><a href="signup.jsp">登録</a></p> 15 登録画面を踏んだ回数<%=request.getAttribute("data.count") %> 16</body> 17</html>
登録JSP
<?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Home</title> </head> <body> <h1>ようこそ</h1> <hr/> <p><a href="login.jsp">ログイン</a></p> <p><a href="signup.jsp">登録</a></p> 登録画面を踏んだ回数<%=request.getAttribute("data.count") %> </body> </html>
登録Servlet
Servlet
1 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 // TODO Auto-generated method stub 3 request.setCharacterEncoding("UTF-8"); 4 HttpSession session = request.getSession(true); 5 6 String message; 7 message = "ご登録ありがとうございました"; 8 request.setAttribute("message", message); 9 10 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/end.jsp"); 11 dispatcher.forward(request, response); 12 13 Integer count = (Integer) request.getAttribute("data.count"); 14 if(count == null) { 15 count = 0; 16 } 17 request.setAttribute("data.count", count); 18 19 } 20 21 /** 22 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 23 */ 24 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 // TODO Auto-generated method stub 26 request.setCharacterEncoding("UTF-8"); 27 String name = request.getParameter("name"); 28 String pass = request.getParameter("pass"); 29 String message; 30 31 message = "ご登録ありがとうございました"; 32 request.setAttribute("message", message); 33 34 data.nameData.add(name); 35 data.passData.add(pass); 36 37 request.getAttribute("data.count"); 38 data.count += 1; 39 request.setAttribute("data.count", data.count); 40 41 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/end.jsp"); 42 dispatcher.forward(request, response); 43 } 44 45}
データを保管するところ
JAVA
1package SimpleBankSystem; 2 3import java.util.ArrayList; 4 5public class data { 6 public static final ArrayList nameData = new ArrayList<String>(); 7 public static final ArrayList passData = new ArrayList(); 8 public static final ArrayList amountData = new ArrayList(); 9 public static int count = 0; 10} 11
end.jsp
JSP
1<?xml version="1.0" encoding="UTF-8" ?> 2<%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5<html xmlns="http://www.w3.org/1999/xhtml"> 6<head> 7<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 8<title>ありがとうございました</title> 9</head> 10<body> 11 <h1><%=request.getAttribute("message")%></h1> 12 <a href="home.jsp">ホームへ戻る</a> 13</body> 14</html>
回答1件
あなたの回答
tips
プレビュー