実技(Webプログラミング実習2)5

1.SESSIONスコープ
2.SESSIONスコープを利用したアプリ
3.URLに含まれたリクエストパラメータ
4.ジャンケンアプリの改良
5.ジャンケンアプリの制作
〇Jyanken.java

package model;

import java.io.Serializable;

public class Jyanken implements Serializable{
	private String userName;
	private String userHand;
	private String pcName="PC";
	private String pcHand;
	private String result;
	public Jyanken() {}
	public Jyanken(String userName,String userHand) {
		this.userName=userName;
		this.userHand=userHand;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserHand() {
		return userHand;
	}
	public void setUserHand(String userHand) {
		this.userHand = userHand;
	}
	public String getPcName() {
		return pcName;
	}
	public void setPcName(String pcName) {
		this.pcName = pcName;
	}
	public String getPcHand() {
		return pcHand;
	}
	public void setPcHand(String pcHand) {
		this.pcHand = pcHand;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}


}


〇JyankenAggre.java

package model;

import java.io.Serializable;

public class JyankenAggre extends Jyanken implements Serializable{
	private int win;
	private int lose;
	private int draw;
	public JyankenAggre() {}
	public JyankenAggre(String userName, String userHand) {
		super(userName, userHand);
	}
	public int getWin() {
		return win;
	}
	public void setWin(int win) {
		this.win = win;
	}
	public int getLose() {
		return lose;
	}
	public void setLose(int lose) {
		this.lose = lose;
	}
	public int getDraw() {
		return draw;
	}
	public void setDraw(int draw) {
		this.draw = draw;
	}


}

〇JyankenLogic.java

package model;

public class JyankenLogic {
	static final String WIN="WIN!",LOSE="LOSE",DRAW="DRAW";
	public void logic(Jyanken j) {
		String[] hands= {"グー","チョキ","パー"};
		int index=(int)(Math.random()*hands.length);
		j.setPcHand(hands[index]);
		String result;
		if(j.getUserHand().equals(j.getPcHand())) {
			result=DRAW;
		}else if(
				j.getUserHand().equals(hands[0]) && j.getPcHand().equals(hands[1])
				||
				j.getUserHand().equals(hands[1]) && j.getPcHand().equals(hands[2])
				||
				j.getUserHand().equals(hands[2]) && j.getPcHand().equals(hands[0])
				)
		{
			result=WIN;
		}else {
			result=LOSE;
		}
		j.setResult(result);

	}
	public void logicAggre(JyankenAggre ja) {
		this.logic(ja);
		switch(ja.getResult()) {
		case WIN:
			ja.setWin(ja.getWin()+1);
			break;
		case LOSE:
			ja.setLose(ja.getLose()+1);
			break;
		case DRAW:
			ja.setDraw(ja.getDraw()+1);
			break;
		}
	}
}


〇JyankenMain.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 javax.servlet.http.HttpSession;

import model.JyankenAggre;
import model.JyankenLogic;

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

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		JyankenAggre ja=(JyankenAggre)session.getAttribute("ja");
		String userName=request.getParameter("userName");
		String hand=request.getParameter("hand");
		if(ja==null) {
			ja=new JyankenAggre(userName,hand);
		} else {
			ja.setUserName(userName);
			ja.setUserHand(hand);
		}
		JyankenLogic jl=new JyankenLogic();
		jl.logicAggre(ja);
		session.setAttribute("ja", ja);
		RequestDispatcher rd=
				request.getRequestDispatcher("/WEB-INF/view/result.jsp");
		rd.forward(request, response);
	}

}

〇form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="model.JyankenAggre"%>
  <%
  JyankenAggre ja=(JyankenAggre)session.getAttribute("ja");
  String name=ja==null? "":ja.getUserName();
  %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css">
<link rel="stylesheet" href="/jyankenapp/css/main.css">
<title>ジャンケンゲーム</title>
</head>
<body>
<div id="container">
<header>Let's じゃんけん!</header>
<div id="main">
<h1>ジャンケンゲーム</h1>
<div class="formBox">
<form action="/jyankenapp/main" method="post">
<%if(ja != null){ %>
<p>WIN:<%=ja.getWin() %> LOSE:<%=ja.getLose() %> DRAW:<%=ja.getDraw() %></p>
<%} %>
<label id="nameLabel" for="userName" >名前</label>
<input type="text" id="userName" name="userName" value="<%=name%>">
<div class="radioBox">
<label><input type="radio" name="hand" value="グー">グー</label>
<label><input type="radio" name="hand" value="チョキ">チョキ</label>
<label><input type="radio" name="hand" value="パー">パー</label>
</div>
<button type="submit" id="submitBt">いざ勝負!</button>
</form>
</div>
</div>
<footer>&copy;joytas.net</footer>
</div>


</body>
</html>

〇result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="model.JyankenAggre"%>
<%

JyankenAggre ja=(JyankenAggre)session.getAttribute("ja");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css">
<link rel="stylesheet" href="/jyankenapp/css/main.css">
<title>ジャンケンゲーム</title>
</head>
<body>
<div id="container">
<header>Let's じゃんけん!</header>
<div id="main">
<h1>Result</h1>
<table>
<tr><th><%=ja.getUserName() %></th><td><%=ja.getUserHand() %></td></tr>
<tr><th><%=ja.getPcName() %></th><td><%=ja.getPcHand() %></td></tr>
</table>
<h2><%=ja.getResult() %></h2>
<ul>
<li>WIN:<%=ja.getWin() %></li>
<li>LOSE:<%=ja.getLose() %></li>
<li>DRAW:<%=ja.getDraw() %></li>
</ul>
<a href="/jyankenapp/main" id="retryBt">Retry</a>
</div>
<footer>&copy;joytas.net</footer>
</div>
</body>
</html>

〇main.css

#container{
	width:960px;
	margin:0 auto;
	background:#aaa;
}
header{
	box-sizing:border-box;
	text-align:center;
	padding-top:100px;
	padding-bottom:100px;
	font-size:50px;
	background-image:url("../images/header.jpg");
	background-size:cover;

}
#main{
	padding:50px;
}
#main h1{
	text-align: center;
	font-size:40px;
	margin-bottom:30px;
}
.formBox{
	width:500px;
	margin:0 auto;
	border:2px solid #333;
	border-radius:20px;
	padding:30px;
	background:#ddd;
	font-size:28px;
	text-align:center;
}

.radioBox{
	margin-top:20px;

}
#submitBt{
	box-sizing:border-box;
	display:block;
	width:80%;
	height:50px;
	margin:20px auto 10px;
	border-radius:10px;
	border:2px solid #333;
	background:#1b45bb;
	color:white;
	font-size:24px;
}
#submitBt:hover{
	opacity:0.7;
	cursor:pointer;
}
footer{
	text-align:center;
	padding:20px 0;
	background:#444;
	color:white;
	font-size:20px;
}
table{
	width:300px;
	margin:0 auto;
	background:#eee;
}
td,th{
	border:1px solid #555;
	padding:10px 0;
	font-size:30px;
	text-align:center;
}
h2{
	text-align:center;
	font-size:40px;
	margin-top:30px;
}
#retryBt{
	display:block;
	width:200px;
	margin:50px auto;
	padding:10px 0;
	text-align:center;
	border:2px solid #333;
	border-radius:10px;
	background:#1b45bb;
	color:white;
	font-size:40px;
	text-decoration: none;
}
#retryBt:hover{
	opacity:0.7;
}
ul{
	width:300px;
	margin:0 auto;
	font-size:20px;
	text-align:center;
}