티스토리 뷰
728x90
반응형
jsp-java 페이지 연결
내용) input으로 데이터를 받아 java에서 사칙연산 처리후 jsp페이지에서 보여주기
빼기(Cal3.java, action3.jsp, form3.jsp)
package kr.or.ksmart;
public class Cal3 {
public int calculateMinus(int a, int b) {
int c = a - b;
return c;
}
}
<%@page import="kr.or.ksmart.Cal3"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<%
String first = request.getParameter("first");
String second = request.getParameter("second");
int firstInt = Integer.parseInt(first);
int secondInt = Integer.parseInt(second);
Cal3 c3 = new Cal3();
int result = c3.calculateMinus(firstInt, secondInt);
%>
<%=firstInt%>
빼기
<%=secondInt%>
는
<%=result%>
입니다
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<form action="action3.jsp" method="post">
<input type="text" name="first">
<p>빼기</p>
<input type="text" name="second">
<input type="submit" class="계산">
</form>
곱하기(Cal4.java, action4.jsp, form4.jsp)
package kr.or.ksmart;
public class Cal4 {
public int calculateMultiply(int a, int b) {
int c = a * b;
return c;
}
}
<%@page import="kr.or.ksmart.Cal4"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<%
String first = request.getParameter("first");
String second = request.getParameter("second");
int firstInt = Integer.parseInt(first);
int secondInt = Integer.parseInt(second);
int result = new Cal4().calculateMultiply(firstInt, secondInt);
%>
<%=firstInt%>
곱하기
<%=secondInt%>
는
<%=result%>
입니다
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<form action="action4.jsp" method="post">
<input type="text" name="first">
<p>곱하기</p>
<input type="text" name="second">
<input type="submit" class="계산">
</form>
나누기(Cal5.java, action5.jsp, form5.jsp)
package kr.or.ksmart;
public class Cal5 {
public double calculateDivide(double a, double b) {
double c = a / b;
return c;
}
}
<%@page import="kr.or.ksmart.Cal5"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<%
String first = request.getParameter("first");
String second = request.getParameter("second");
double firstDouble = Double.parseDouble(first);
double secondtDouble = Double.parseDouble(second);
Double result = new Cal5().calculateDivide(firstDouble, secondtDouble);
%>
<%=firstDouble%>
나누기
<%=secondtDouble%>
는
<%=result%>
입니다
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<form action="action5.jsp" method="post">
<input type="text" name="first">
<p>나누기</p>
<input type="text" name="second">
<input type="submit" class="계산">
</form>
사칙연산+나머지(Cal6.java, Result.java, action6.jsp, form6.jsp)(수정)
package kr.or.ksmart;
public class Cal6 {
public String calculate(int a, int b, String type) {
int plus = a + b;
int minus = a - b;
int multiply = a * b;
double divide = ((double) a / (double) b);
int modulus = a % b;
Result total = new Result(plus, minus, multiply, divide, modulus);
if (type.equals("더하기")) {
String result = total.getPlus() + "";
return result;
} else if (type.equals("빼기")) {
String result = total.getMinus() + "";
return result;
} else if (type.equals("곱하기")) {
String result = total.getMultiply() + "";
return result;
} else if (type.equals("나누기")) {
String result = total.getDivide() + "";
return result;
} else if (type.equals("나머지")) {
String result = total.getModulus() + "";
return result;
} else {
String result = "올바른 계산타입을 입력해주세요";
return result;
}
}
}
package kr.or.ksmart;
public class Result {
private int plus;
private int minus;
private int multiply;
private double divide;
private int modulus;
public int getPlus() {
return plus;
}
public void setPlus(int plus) {
this.plus = plus;
}
public int getMinus() {
return minus;
}
public void setMinus(int minus) {
this.minus = minus;
}
public int getMultiply() {
return multiply;
}
public void setMultiply(int multiply) {
this.multiply = multiply;
}
public double getDivide() {
return divide;
}
public void setDivide(double divide) {
this.divide = divide;
}
public int getModulus() {
return modulus;
}
public void setModulus(int modulus) {
this.modulus = modulus;
}
public Result(int plus, int minus, int multiply, double divide, int modulus) {
super();
this.plus = plus;
this.minus = minus;
this.multiply = multiply;
this.divide = divide;
this.modulus = modulus;
}
}
<%@page import="kr.or.ksmart.Result"%>
<%@page import="kr.or.ksmart.Cal6"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<%
request.setCharacterEncoding("EUC-KR");
String first = request.getParameter("first");
String second = request.getParameter("second");
String calType = request.getParameter("caltype");
int firstInt = Integer.parseInt(first);
int secondtInt = Integer.parseInt(second);
Cal6 c6 = new Cal6();
String total = c6.calculate(firstInt, secondtInt, calType);
if (total.equals("올바른 계산타입을 입력해주세요")) {
}
%>
<%=firstInt%>과(와)
<%=secondtInt%>의 사칙연산(<%=calType%>) 값은 :
<p><%=total%>입니다</p>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<form action="action6.jsp" method="post">
<input type="text" name="first" placeholder="정수를 입력해주세요"
required="required">
<p>과(와)</p>
<input type="text" name="second" placeholder="정수를 입력해주세요"
required="required">
<p>의</p>
<input type="text" name="caltype" placeholder="더하기/빼기/곱하기/나누기/나머지 중 택1" style="width: 250px" required="required">
<p>(더하기/빼기/곱하기/나누기/나머지 중 하나를 입력해주세요)</p>
<input type="submit" class="계산">
</form>
728x90
반응형
'실습페이지' 카테고리의 다른 글
24_07_25 오늘의 실습내용 (0) | 2024.07.25 |
---|---|
24_07_12 오늘의 실습내용 (1) | 2024.07.12 |
24_07_03 오늘의 실습내용 (0) | 2024.07.03 |
24_07_02 오늘의 실습내용 (0) | 2024.07.02 |
24_06_26 오늘의 실습 (0) | 2024.06.26 |
반응형
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 객체 지향 프로그래맹의 특징(캡슐화|상속|다형성)
- 제어흐름함수
- JavaScript
- get한글인코딩
- 조건문
- Java
- let-const-var
- 문자열비교메서드
- 뷰(view)
- ㅎgroupbyvs윈도우함수
- stored program
- 오버로딩vs오버라이딩
- 필드 초기화와 사용
- resultsetimpl
- 테이터베이스 설계
- 변수표기법
- 데이터변환함수
- javascript자료형
- 클래스의 구성 멤버
- http 프로토콜:get vs post
- jsp의 주요 태그
- 가변 길이 매개변수와 리턴
- 호이스팅(hoisting)
- 참조타입vs기본타입
- 반복문
- cte ( common table expression )
- 배열(Array)
- 함수범위vs블록범위
- jdbc
- 데이터베이스(mysql)
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함