웹 계산기 만들기_입력된 값을 배열로 받기

ColinSong·2020년 12월 21일
0

WebApplicationServer

목록 보기
7/26
post-thumbnail
post-custom-banner

목차

입력된 값을 배열로 받기

연결 자료 :

add.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="add" method="post">
		<input type="text" name="num" > // request.getParameterValues("num")
		<input type="text" name="num" > 
		<input type="text" name="num" > 
		<input type="text" name="num" > 
		<input type="submit" name="operator" value="덧셈">
	</form>
</body>
</html>

add.java

package com.yj.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/add2")
public class add2 extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");

		String[] num_ = request.getParameterValues("num");
		String op_ = request.getParameter("operator");

		int result = 0;

		if(op_.equals("덧셈")) {
			for(int i = 0; i < num_.length; i++) {
				int num = Integer.parseInt(num_[i]);
				System.out.println(num_[i]);
				result+=num;
			}
		}
		response.getWriter().printf("result is %d\n", result);
	}

}

코드 설명 정리

  • getParameterValues("num");
    같은 name의 값이 여러 개 있을 때, 배열로 받을 수 있다.
// request.getParameterValues("num")
<input type="text" name="num" value="num[0]"> // 1
<input type="text" name="num" value="num[1]"> // 2
<input type="text" name="num" value="num[2]"> // 3
<input type="text" name="num" value="num[3]"> // 4

References

  • 뉴렉처: Servlet / JSP
  • 🎈vscode 정리
  • 🎈2020.12.17

profile
안녕하세요:)
post-custom-banner

0개의 댓글