第5章 データの送信

5.1 サーバの準備

RequestedParameters.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Requested Parameters</title>
</head>
<body>
<table>
<tr><th>name</th><th>value</th></tr><%
  for(Enumeration e=request.getParameterNames();e.hasMoreElements();){
    String name=(String)e.nextElement();
    for(String value:request.getParameterValues(name)){%>
      <tr>
        <td><c:out value="<%=new String(name.getBytes("ISO8859_1"),"UTF-8") %>" /></td>
        <td><c:out value="<%=new String(value.getBytes("ISO8859_1"),"UTF-8") %>" /></td>
      </tr><%
    }
  }%>
</table>
</body>
</html>

5.2 フォーム

SampleForm.html

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sample Form</title>
</head>
<body>
<form action="http://localhost:8080/RequestedParameters.jsp" method="get">
  <table summary="Sample Form"><caption>Request Parameters</caption>
    <tr>
      <td>First Name</td>
      <td><input type="text" name="FirstName" value="" /></td>
    </tr>
    <tr>
      <td>Last Name</td>
      <td><input type="text" name="LastName" value="" /></td>
    </tr>
    <tr>
      <td></td>
      <td><input type="submit" name="submit" value="submit" /></td>
    </tr>
  </table>
</form>
</body>
</html>

5.2.1 フォームで利用できる要素

FormElements.html

<form action='http://localhost:8080/RequestedParameters.jsp' method='get'>
<table border='1'>
  <tr>
    <th>タグ</th>
    <th>type属性値</th>
    <th>表示例</th>
  </tr>
  <tr>
    <td rowspan='8'>input</td>
    <td>text</td>
    <td><input type='text' name='name' value='default' size='30' /></td>
  </tr>
  <tr>
    <td>password</td>
    <td><input type='password' name='password' /></td>
  </tr>
  <tr>
    <td>radio</td>
    <td>
      <label><input type='radio' name='radio1' value='A' checked='checked' />A</label>
      <label><input type='radio' name='radio1' value='B' />B</label>
      <label><input type='radio' name='radio1' value='C' />C</label>
    </td>
  </tr>
  <tr>
    <td>checkbox</td>
    <td>
      <label><input type='checkbox' name='check1' value='1' checked='checked' />1</label>
      <label><input type='checkbox' name='check1' value='2' checked='checked' />2</label>
      <label><input type='checkbox' name='check1' value='3' />3</label>
    </td>
  </tr>
  <tr>
    <td>file</td>
    <td><input type='file' name='file1' /></td>
  </tr>
  <tr>
    <td>hidden</td>
    <td>表示されない<input type='hidden' name='hidden1' value='hidden value' /></td>
  </tr>
  <tr>
    <td>submit</td>
    <td><input type='submit' name='submit1' value='submit' /></td>
  </tr>
  <tr>
    <td>reset</td>
    <td><input type='reset' name='reset1' value='reset' /></td>
  </tr>
  <tr>
    <td>select</td>
    <td></td>
    <td>
      <select name='select1'>
        <option value='10'>10</option>
        <option value='20'>20</option>
        <option value='30'>30</option>
        <option value='40'>40</option>
        <option value='50'>50</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>textarea</td>
    <td></td>
    <td><textarea name='textarea1' rows='3' cols='20'>comment</textarea></td>
  </tr>
</table>
</form>