MVCモデル使って以下のアプリを作成せよ。Modelにはbeans,Viewにはjsp,Controllerにはservletを使うこと。正常系のみを考慮し、バリデーション(入力値チェック)は行わなくて良い。

Q1.フォームから情報を入力すると確認画面を表示するアプリを作成せよ。
[実行例]
●以下のようなフォームを表示

●情報を入力し送信ボタンを押すと確認画面が表示される。

[解答例]
●model.Member.java

package model;

import java.io.Serializable;

public class Member implements Serializable{
	private String name;
	private String gender;
	private String birth;
	private String blood;
	
	public Member(){}
	
	public Member(String name, String gender, String birth, String blood) {
		this.name = name;
		this.gender = gender;
		this.birth = birth;
		this.blood = blood;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getBirth() {
		return birth;
	}
	public void setBirth(String birth) {
		this.birth = birth;
	}
	public String getBlood() {
		return blood;
	}
	public void setBlood(String blood) {
		this.blood = blood;
	}
	
}

●controller.Index.java

package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Member;

@WebServlet("/index")
public class Index extends HttpServlet {
	private static final long serialVersionUID = 1L;
         
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/jsp/form.jsp");
		rd.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String name=request.getParameter("name");
		String gender=request.getParameter("gender");
		String year=request.getParameter("year");
		String month=request.getParameter("month");
		String day=request.getParameter("day");
		String birth=year+"-"+month+"-"+day;
		String blood=request.getParameter("blood");
		Member m=new Member(name,gender,birth,blood);
		request.setAttribute("member", m);
		RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/jsp/result.jsp");
		rd.forward(request, response);	
	}
}

●WEB-INF/jsp/form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Lesson</title>
</head>
<body>
<p>以下の項目を入力してください。</p>
<form action="/joytas4/index" method="post">
名前:<input type="text" name="name"><br>
性別:<label><input type="radio" name="gender" value="男">男</label>
<label><input type="radio" name="gender" value="女">女</label><br>
生年月日:<select name="year">
<%
for(int i=1930;i<2017;i++){
	out.print(String.format("<option value='%d'>%d</option>",i,i));
}
%>
</select>年
<select name="month">
<%
for(int i=1;i<=12;i++){
	out.print(String.format("<option value='%d'>%d</option>",i,i));
}
%>
</select>月
<select name="day">
<%
for(int i=1;i<=31;i++){
	out.print(String.format("<option value='%d'>%d</option>",i,i));
}
%>
</select>日<br>
血液型:<select name="blood">
<option value="A">A</option>
<option value="B">B</option>
<option value="O">O</option>
<option value="AB">AB</option>
</select><br>
<input type="submit" value="送信">

</form>
</body>
</html>

●WEB-INF/jsp/result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"  import="model.Member"%>
<%
 Member m=(Member)request.getAttribute("member");   
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<p>以下の内容でよろしいですか?</p>
<dl>
<dt>名前</dt><dd><%=m.getName() %></dd>
<dt>性別</dt><dd><%=m.getGender() %></dd>
<dt>生年月日</dt><dd><%=m.getBirth() %></dd>
<dt>血液型</dt><dd><%=m.getBlood() %></dd>
</dl>
</body>
</html>