- [JSP] 25. JSP와 HTML을 사용해 계산기 만들기2024년 07월 04일
- Song hyun
- 작성자
- 2024.07.04.:48
728x90반응형[JSP] 25. JSP와 HTML을 사용해 계산기 만들기
1. HTML로 폼 만들기
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div class="container"> <h1>간단한 계산기</h1> <form action="calculate.jsp" method="POST"> <label for="num1">첫번째 숫자를 입력하세요.</label> <input type="number" id="num1" name="num1" required="required"> <label for="num2">두번째 숫자를 입력하세요.</label> <input type="number" id="num2" name="num2" required="required"> <input type="submit" value="계산 요청하기"> </form> </div> </body> </html>
2. CSS로 폼 꾸미기
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> body{ display: flex; height: 100vh; justify-content: center; align-items: center; margin: 0; background-color: #f0f0f0; } .container{ background-color:white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); width: 300px; } form { display: flex; flex-direction:column; } label{ margin-bottom:5px; font-weight:bold; } input[type="number"]{ margin-bottom:15px; padding:10px; border: 1px solid #ccc; border-radius:4px; } input[type="submit"]{ padding:10px; background-color: #45a049; color: white; cursor: pointer; border-radius:4px; border: none; } input[type="submit"]:hover{ background-color: #FFBF00; } </style> </head> <body> <div class="container"> <h1>간단한 계산기</h1> <form action="calculate.jsp" method="POST"> <label for="num1">첫번째 숫자를 입력하세요.</label> <input type="number" id="num1" name="num1" required="required"> <label for="num2">두번째 숫자를 입력하세요.</label> <input type="number" id="num2" name="num2" required="required"> <input type="submit" value="계산 요청하기"> </form> </div> </body> </html>
3. JSP로 작동시키기
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> body{ display: flex; height: 100vh; justify-content: center; align-items: center; margin: 0; background-color: #f0f0f0; } .container{ background-color:white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); width: 300px; } form { display: flex; flex-direction:column; } label{ margin-bottom:5px; font-weight:bold; } input[type="number"]{ margin-bottom:15px; padding:10px; border: 1px solid #ccc; border-radius:4px; } input[type="submit"]{ padding:10px; background-color: #45a049; color: white; cursor: pointer; border-radius:4px; border: none; } input[type="submit"]:hover{ background-color: #FFBF00; } </style> </head> <body> <div class="container"> <h1>간단한 계산기</h1> <form action="calculate.jsp" method="POST"> <label for="num1">첫번째 숫자를 입력하세요</label> <input type="number" id="num1" name="num1" required="required"> <label for="num2">두번째 숫자를 입력하세요:</label> <input type="number" id="num2" name="num2" required="required"> <input type="submit" value="계산 요청하기"> </form> </div> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>계산 결과</title> </head> <body> <h1>계산 결과</h1> <%-- JSP 주석 입니다! --%> <% // -> 스크립트릿 / 코드 실행 영역 // 폼에서 데이터 추출 String num1=request.getParameter("num1"); String num2=request.getParameter("num2"); int int1=Integer.parseInt(num1); int int2=Integer.parseInt(num2); int plus=int1+int2; int minus=int1-int2; int times=int1*int2; int div=int1/int2; // 방어적 코드 작성 if(num1==null || num2==null || num1.trim().isEmpty() || num2.trim().isEmpty()){ out.println("<p>앗, 숫자를 입력하지 않았습니다! 다시 입력해주세요. </p>"); } else{ out.println("<p> 더하기 : "+int1+" + "+int2+" = "+plus+"</p>"); out.println("<p> 빼기 : "+int1+" - "+int2+" = "+minus+"</p>"); out.println("<p> 곱하기 : "+int1+" * "+int2+" = "+times+"</p>"); out.println("<p> 나누기 : "+int1+" % "+int2+" = "+div+"</p>"); } // 계산의 결과를 스트림을 통해 내려주기 %> <a href="caculator.html">Back</a> </body> </html>
*방어적 코드
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> body{ display: flex; height: 100vh; justify-content: center; align-items: center; margin: 0; background-color: #f0f0f0; } .container{ width: 300px; background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } form{ display: flex; flex-direction: column; } label{ margin-bottom: 10px; } input[type="number"] { height: 30px; border: 1px solid #bababa; border-radius: 5px; margin-bottom: 20px; } input[type="submit"] { height: 40px; border-radius: 5px; border: 0; background-color: #45a049; color: #fff; } input[type="submit"]:hover{ background-color: #1c5a1f; } </style> </head> <body> <div class="container"> <h1>간단한 계산기</h1> <form action="calculate.jsp" method="post"> <label for = "num1">첫번째 숫자를 입력하세요</label> <input type="number" id="num1" name="num1" required="required" value="10"> <label for = "num2">첫번째 숫자를 입력하세요</label> <input type="number" id="num2" name="num2" required="required" value="20"> <input type="submit" value="계산 요청하기"> </form> </div> </body> </html>
728x90반응형'JSP > 기본 이론' 카테고리의 다른 글
[JSP] 27. 센치미터-인치 변환 페이지 만들어보기 (0) 2024.07.04 [JSP] 26. JSP 주석과 지시자 (0) 2024.07.04 [JSP] 24. JSP와 HTML을 사용해 인사말 생성기 만들기 (0) 2024.07.04 [JSP] 23. JSP 기초 문법 (0) 2024.07.03 [JSP] 22. JSP 라이프 사이클 (0) 2024.07.03 다음글이전글이전 글이 없습니다.댓글
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)