충분히 쌓여가는
html 인코딩이 안맞는 경우(setCharacterEncoding) 본문
package com.buildenough.ch2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Calendar;
@RestController
public class YoilTeller {
@RequestMapping("/getYoil")
public void main(HttpServletRequest request, HttpServletResponse response) throws Exception {
String year = request.getParameter("year");
String month = request.getParameter("month");
String day = request.getParameter("day");
int yyyy = Integer.parseInt(year);
int mm = Integer.parseInt(month);
int dd = Integer.parseInt(day);
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(yyyy, mm-1, dd);
int dayofWeek = cal.get(Calendar.DAY_OF_WEEK);
char yoil = "일월화수목금토".charAt(dayofWeek - 1);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("year = " + year);
out.println("month = " + month);
out.println("day = " + day);
out.println("yoil = " + yoil);
out.println("</body>");
out.println("</html>");
}
}
yoil의 값이 ?로 나온다
인코딩이 맞지 않기 때문
setCharacterEncoding("ms949")
package com.buildenough.ch2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Calendar;
@RestController
public class YoilTeller {
@RequestMapping("/getYoil")
public void main(HttpServletRequest request, HttpServletResponse response) throws Exception {
String year = request.getParameter("year");
String month = request.getParameter("month");
String day = request.getParameter("day");
int yyyy = Integer.parseInt(year);
int mm = Integer.parseInt(month);
int dd = Integer.parseInt(day);
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(yyyy, mm-1, dd);
int dayofWeek = cal.get(Calendar.DAY_OF_WEEK);
char yoil = "일월화수목금토".charAt(dayofWeek - 1);
response.setCharacterEncoding("ms949"); // 한글 윈도우 MS 949
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("year = " + year);
out.println("month = " + month);
out.println("day = " + day);
out.println("yoil = " + yoil);
out.println("</body>");
out.println("</html>");
}
}
'Spring > 연습' 카테고리의 다른 글
컨트롤러 메서드의 반환타입(String, void, ModelAndView) (0) | 2023.10.02 |
---|---|
thymeleaf 기본 설정 (0) | 2023.10.02 |
의존하고 있는 라이브러리 보기 (0) | 2023.09.30 |
기본 실행(IntelliJ 실행) (0) | 2023.09.30 |
Spring Initializr로 프로젝트 만들기 (0) | 2023.09.30 |