성격테스트 배열뜯어서 값 사이에 ~와 끝에는 ~를 로 문장 이어가기 - soonbeankim/web1 GitHub Wiki

jsp web화면

<%@ 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>

<h2>성격 테스트</h2>
당신의 성격을 테스트 합니다. 데이터를 입력 한 후 확인 버튼을 눌러주세요.
<form action="chartest_02">
이름은?<input type="text" name="name"><br>
좋아하는 색은?
<input type="radio" name="color" value="노랑색">노랑
<input type="radio" name="color" value="파랑색">파랑
<input type="radio" name="color" value="빨강색">빨강<br>
좋아하는 동물은?
<select name="animal">
<option>기린</option>
<option>사자</option>
<option>치타</option>
</select><br>
좋아하는 음식은?(모두 고르세요)
<input type="checkbox" name="food" value="떡볶이">떡볶이
<input type="checkbox" name="food" value="피자">피자
<input type="checkbox" name="food" value="치킨">치킨
<input type="checkbox" name="food" value="짜장면">짜장면<br>

<input type="submit" value="확인">

</form>
</body>
</html>

servlet 화면_02

package com.javalec.base;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class chartest_02
 */
@WebServlet("/chartest_02")
public class chartest_02 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public chartest_02() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//이렇게 해줘야 한글이 정상적으로 보임!!!!!!	
		String name = request.getParameter("name");
		String color = request.getParameter("color");
		String animal = request.getParameter("animal");
		String[] food = request.getParameterValues("food");

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head><title>성격테스트</title></head>");
		out.println("<body>");
		out.println(name+" 님의 성격 테스트 결과" +"<br>");
		out.println(color+" 를 좋아하는 당신은 "+animal+" 그리고 ");
		//out.println(Arrays.toString(food)+"를 좋아하는 성격입니다 ");
		for(int i=0; i<food.length; i++) {
			if(i>0) {
				out.println(" 와 "+food[i]);
			}else {
			out.println(food[i]);
			}
		}
		out.println("를 좋아합니다");
		out.println("<br>"+"================================");
		out.println("<br>"+"저장되었습니다!");
		out.println("</body>");
		out.println("</html>");
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

⚠️ **GitHub.com Fallback** ⚠️