JSP & Servlet(numapp作成)

JSP&Servlet

mvcを使って簡単な数字分析アプリを作成してみよう!

実行例

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

数字をカンマ区切りで入力する。(この際、数字は整数)

送信ボタンを押すと結果が表示される

戻るを押すと最初から

解答例

package model;

import java.io.Serializable;

public class Num implements Serializable{
	private String org;
	private int len;
	private int max;
	private int min;
	private int sum;
	public Num(){}
	public String getOrg() {
		return org;
	}
	public void setOrg(String org) {
		this.org = org;
	}
	public int getLen() {
		return len;
	}
	public void setLen(int len) {
		this.len = len;
	}
	public int getMax() {
		return max;
	}
	public void setMax(int max) {
		this.max = max;
	}
	public int getMin() {
		return min;
	}
	public void setMin(int min) {
		this.min = min;
	}
	public int getSum() {
		return sum;
	}
	public void setSum(int sum) {
		this.sum = sum;
	}
}
package model;

public class NumLogic {
	public void execute(Num num){
		String[] nums=num.getOrg().split(",");	
		int first=Integer.parseInt(nums[0]);
		int max,min,sum;
		max=min=sum=first;
		
		for(int i=1;i<nums.length;i++){
			int pick=Integer.parseInt(nums[i]);
			if(pick>max){
				max=pick;
			}
			if(pick<min){
				min=pick;
			}
			sum+=pick;
		}
		num.setLen(nums.length);
		num.setMax(max);
		num.setMin(min);
		num.setSum(sum);
	}
}
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.Num;
import model.NumLogic;


@WebServlet("/Main")
public class Main extends HttpServlet {
	
	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 {
		String org=request.getParameter("org");
		Num num=new Num();
		num.setOrg(org);
		NumLogic nl=new NumLogic();
		nl.execute(num);
		request.setAttribute("num", num);
		RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/view/result.jsp");
		rd.forward(request, response);	
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<p>数字をカンマ区切りで入力してください</p>
<form action="/numapp/Main" method="post">
<input type="text" name="org"><br>
<input type="submit" value="送信">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="model.Num"%>
<%
	Num num=(Num)request.getAttribute("num");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
<p>元の文字列:<%=num.getOrg() %></p>
<p>要素数:<%=num.getLen() %></p>
<p>最大値:<%=num.getMax() %></p>
<p>最小値:<%=num.getMin() %></p>
<p>合計:<%=num.getSum() %></p>
<a href="/numapp/Main">戻る</a>
</body>
</html>

コメント

タイトルとURLをコピーしました