프로그래밍 언어/자바 웹

학점 변환 예제

· 코딩마이데이

1. 다음과 같이 scoreTest.html, scoreTest.jsp 파일을 준비합니다.

실습 파일 위치

 

2. scoreTest.html을 다음과 같이 작성합니다. 사용자로부터 시험 점수를 입력 받아 scoreTest.jsp로 전송합니다.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>시험 점수를 입력해 주세요</h1>
	<form method=get action="scoreTest.jsp">
		시험점수 : <input type=text name="score" /> <br>
		<input type="submit" value="변환하기">
	</form>
</body>
</html>

 

3. scoreTest.jsp를 다음과 같이 작성합니다. scoreTest.html로부터 받은 점수를 다중 if~else if문을 이용해 학점을 변환합니다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf-8");
	int score = Integer.parseInt(request.getParameter("score"));
%>
<!DOCTYPE html>
<html>
<head>
	<title>점수 출력창</title>
	<meta charset="UTF-8">
</head>
<body>
	<h1> 시험점수 <%=score %>점</h1>
	<%
		if (score >= 90) {
	%>
	<h1> A학점입니다.</h1>
	<%
		} else if (score >= 80 && score < 90) {
	%>
	<h1> B학점입니다.</h1>
	<%
		} else if (score >= 70 && score < 80) {
	%>
	<h1> C학점입니다.</h1>
	<%
		} else if (score >= 60 && score < 70) {
	%>
	<h1> D학점입니다.</h1>
	<%
		} else {
	%>
	<h1> F학점입니다.</h1>
	<% 
		}
	%>
	<br>
	<a href="scoreTest.html">시험점수입력</a>
</body>
</html>

 

4. http://localhost:8090/pro12/scoreTest.html로 요청하여 시험점수 입력창에 시험 점수를 입력한 후 변환하기를 클릭합니다.

시험 점수 입력 후 변환하기 클릭

 

5. 시험 점수를 학점으로 변환하여 출력합니다.

시험 점수를 학점으로 변환 후 출력