Get Method와 JSON

김유정·2023년 11월 29일
2

서블릿-퀴즈

목록 보기
4/11
post-custom-banner
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>연산하기</title>
</head>
<body>
	<a href="/lesson01/quiz06?number1=1500&number2=70">1500과 70을 연산한 결과 보러가기</a>
</body>
</html>
package lesson01;

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

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

@WebServlet("/lesson01/quiz06")
public class GetMethodQuiz06 extends HttpServlet {
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/json");
		
		int number1 = Integer.valueOf(request.getParameter("number1"));
		int number2 = Integer.valueOf(request.getParameter("number2"));

		
		PrintWriter out = response.getWriter();
		out.print(
				"{\"addition\": " + (number1 + number2) + "," +
				"\"subtraction\": " + (number1 - number2) + "," + 
				"\"multiplication\": " + (number1 * number2) + "," +
				"\"division\": " + (number1 / number2) + "}");

	}
}
profile
열심히 하자
post-custom-banner

0개의 댓글