第17回セミナー

JSPの練習問題

買い物ページ SampleP4

<html>
<head><title>サンプル</title></head>
<body><center>
<h1>ようこそ</h1>
<hr>
<p>何がほしいですか?</p>
<form action="http://localhost:8080/K07/SampleP4.jsp" method="get">
  <input type=text size=20 name="name">
  <input type=submit value="かごに入れる">
</form>
</body></center>
</html>
<%@ page
  contentType="text/html; charset=Shift_JIS"
  import="java.util.*"
%>
<%
  String tmp=request.getParameter("name");
  String name=new String(tmp.getBytes("8859_1"),"JISAutoDetect");
  Integer ct=(Integer)session.getAttribute("countP4");
  ArrayList<String> bag=(ArrayList<String>)session.getAttribute("bagP4");
  String str1,str2;
  if(ct==null){
    str1="はじめておこしですね。<br>";
    str2=name+"<br>";
    ct=1;
    bag=new ArrayList<String>();
    bag.add(name);
  }
  else{
    ct=ct+1;
    str1=ct+"回目のおこしですね。<br>";
    bag.add(name);
    str2="";
    for(String s:bag)
      str2+=s+"<br>";
  }
  session.setAttribute("countP4",ct);
  session.setAttribute("bagP4",bag);
%>
<html>
<head><title>サンプル</title></head>
<body><center>
<h1>ようこそ</h1>
<p><%= str1 %></p>
<p>買い物籠の中身:</p>
<%= str2 %>
</ul>
<p><a href="SampleP4.html">買い物を続ける</a></p>
</body></center>
</html>

Javaセミナー2014


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS