Q1_1.Servletを使って、以下のようなページを作成しよう。Servletファイル名は[Lesson1.java]リクエストurlは下の画像参照のこと。(計算はプログラムですること)

必要であれば以下をコピペしてつかってよい。

out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset='utf-8' />");
out.println("<title>Lesson</title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");

[解答例]

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/lesson1")
public class Lesson1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int x=1000,y=3000;
		int sum=0;
		for(int i=x;i<=y;i++){
			sum+=i;
		}
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta charset='utf-8' />");
		out.println("<title>Lesson</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<p>"+x+"から"+y+"までの和は"+sum+"です。");
		out.println("</body>");
		out.println("</html>");
	}
}

Q1_2.Q1_1と同じ内容を出力するページをJSPを使って記せ。ファイル名は[lesson1.jsp]リクエストURLは以下の実行例と同一にすること。

[解答例]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
int x=1000,y=3000;
int sum=0;
for(int i=x;i<=y;i++){
	sum += i;
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Lesson1</title>
</head>
<body>
<p><%=x %>から<%=y %>までの和は<%=sum %>です。</p>
</body>
</html>

Q2.JSPを使って以下のページを作成せよ。合計点の計算はプログラムにさせること。

必要であれば以下のクラスを使って良い
●model.Team.java

package model;

public class Team {
	private String name;
	private String[] score;
	public Team(String name,String[] score){
		this.name=name;
		this.score=score;
	}
	public String getName(){
		return this.name;
	}
	public String[] getScore(){
		return this.score;
	}
	public String getTotalScore(){
		int sum=0;
		for(String s:this.score){
			if(s.equals("x")){
				break;
			}
			if(s.endsWith("x")){
				s=s.substring(0, s.length()-1);
			}		
			sum+=Integer.parseInt(s);			
		}
		return String.valueOf(sum);
	}
}

[解答例]
●score.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="model.*"%>
<%
Team[] teams=new Team[2];
teams[0]=new Team("巨人",new String[]{"0","0","2","2","0","3","2","0","0","0","1"});
teams[1]=new Team("阪神",new String[]{"2","0","0","1","0","0","1","0","5","0","2x"});
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>スコアボード</title>
<link rel="stylesheet" href="css/main.css" />
</head>
<body>
<table>
<tr>
<th></th>
<%for(int i=1;i<=teams[0].getScore().length;i++) {%>
<td><%=i %></td>
<%} %>
<th>計</th></tr>
<%for(int i=0;i<teams.length;i++) {%>
<tr>
<th><%=teams[i].getName() %></th>
<%for(int j=0;j<teams[i].getScore().length;j++){ %>
<td><%=teams[i].getScore()[j] %></td>
<%} %>
<td><%= teams[i].getTotalScore()%></td>
</tr>
<%} %>
</table>
</body>
</html>