Servlet / JSP #07 학습 과제

underlier12·2020년 1월 24일
0

SERVLET&JSP

목록 보기
7/16

07. 학습 과제

제작 조건

add.html 을 작성하여 GET, POST 가능하도록 포맷을 구성
(값 두개를 받아 덧셈의 값을 출력하도록)

image.png

결과

add.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Registration</title>
</head>
<body>
	<div>
		<form action="add" method="post">
			<div>
				<label>x : </label><input name="x" type="text">
			</div>
			<div>
				<label>y : </label><input name="y" type="text">
			</div>
			<div>
				<input type="submit" value="계산"/>
			</div>
		</form>
	</div>
</body>
</html>

Add.java

package com.newlecture.homework;

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

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

@WebServlet("/add")
public class Add extends HttpServlet{
	@Override
	protected void service(HttpServletRequest request
			, HttpServletResponse response) 
					throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		PrintWriter out = response.getWriter();
		
		String x_ = request.getParameter("x");
		String y_ = request.getParameter("y");
		
		int x = 0;
		int y = 0;
		
		if(x_ != null && x_ != "") {
			x = Integer.parseInt(x_);
		}
		
		if(y_ != null && y_ != "") {
			y = Integer.parseInt(y_);
		}
		
		out.println("덧셈 결과 = " + (x + y));		
		
	}
}
profile
logos and alogos

0개의 댓글