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

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

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

[解答例]
●model.Person.java

package model;

import java.io.Serializable;

public class Person implements Serializable{
	private String name;
	private String gender;
	private String birth;
	private String blood;
	public Person() {

	}
	public Person(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.Main.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.Person;

@WebServlet("/Main")
public class Main extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/view/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 birth=request.getParameter("year")+"-"
				+request.getParameter("month")+"-"
				+request.getParameter("day");
		String blood=request.getParameter("blood");
		Person person=new Person(name,gender,birth,blood);
		request.setAttribute("person", person);
		RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/view/confirm.jsp");
		rd.forward(request, response);
	}
}

●WEB-INF/view/form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/mvclesson/Main" method="post">
名前:<input type="text" name="name"><br>
性別:<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br>
生年月日:
<select name="year">
<%for(int i=1930;i<=2019;i++){ %>
<option value="<%=i %>"><%=i %></option>
<%} %>
</select>年
<select name="month">
<%for(int i=1;i<=12;i++){ %>
<option value="<%=i %>"><%=i %></option>
<%} %>
</select>月
<select name="day">
<%for(int i=1;i<=31;i++){ %>
<option value="<%=i %>"><%=i %></option>
<%} %>
</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/view/result.jsp

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