mvcを使って数当てアプリを作成してみよう!

実行例

ブラウザに
http://localhost:8080/HighLow/Main
と入力するとフォームが表示される。

数字を入れて送信。

もっと上か、もっと下というヒントとともに入力履歴が表示される。
(表示の際にはolとliタグを利用)
当たるまでこれを繰り返し、正解したら正解と表示される。

もう一度を押すと最初から

解答例

package model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HighLow implements Serializable{
	private int correctAns;
	private int userAns;
	private String msg;
	private List<Integer> list;
	public HighLow(){
		this.correctAns=new Random().nextInt(100)+1;
		list=new ArrayList<Integer>();
	}
	
	public int getCorrectAns() {
		return correctAns;
	}
	public void setCorrectAns(int correctAns) {
		this.correctAns = correctAns;
	}
	
	public int getUserAns() {
		return userAns;
	}

	public void setUserAns(int userAns) {
		this.userAns = userAns;
	}

	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<Integer> getList() {
		return list;
	}
	public void setList(List<Integer> list) {
		this.list = list;
	}
}
package model;

public class HighLowLogic {
	
	public void execute(HighLow hl,int userAns){
		hl.setUserAns(userAns);
		hl.getList().add(userAns);
		String msg;
		if(userAns>hl.getCorrectAns()){
			msg="もっと下だよ";
		}else if(userAns<hl.getCorrectAns()){
			msg="もっと上だよ";
		}else{
			msg="正解!";
		}
		hl.setMsg(msg);
	}
}
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 javax.servlet.http.HttpSession;

import model.HighLow;
import model.HighLowLogic;

@WebServlet("/Main")
public class Main extends HttpServlet {
	private String path="/WEB-INF/view/main.jsp";
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HighLow hl=new HighLow();
		HttpSession session=request.getSession();
		session.setAttribute("hl", hl);
		RequestDispatcher rd=request.getRequestDispatcher(this.path);
		rd.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userAns=request.getParameter("userAns");
		HttpSession session=request.getSession();
		HighLow hl=(HighLow)session.getAttribute("hl");
		HighLowLogic hll=new HighLowLogic();
		hll.execute(hl,Integer.parseInt(userAns));
		session.setAttribute("hl", hl);
		RequestDispatcher rd=request.getRequestDispatcher(this.path);
		rd.forward(request, response);
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="model.*"%> 
<%
	HighLow hl=(HighLow)session.getAttribute("hl");

%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<p>1〜100の乱数を生成したよ!</p>
<p>いくつかな?</p>
<form action="/HighLow/Main" method="post">
<input type="number" name="userAns" value="<%=hl.getUserAns()%>">
<input type="submit" value="送信">
</form>
<% if (hl.getList().size() >0){ %>
	<p><%=hl.getMsg() %></p>
	<ol>
	<% for(int i:hl.getList()){%>
		<li><%=i %></li>
	<%} %>
	</ol>
<%} %>
<%if(hl.getUserAns()==hl.getCorrectAns()){ %>
	<a href="/HighLow/Main">もう一度</a>
<%} %>
</body>
</html>