32: Servlet jsp3

jk·2024년 2월 16일
0

kdt 풀스택

목록 보기
62/127



1. 아래를 프로그래밍 하시오.

/context명/input.jsp ->이름 국어 영어 수학 (input box) 센드 버튼, 리셋버튼
 
/context명/result.jsp ->이름 총점 평균
 
-최대한 액션태그를 활용하시오.
<!-- code1 -->
<!-- .\WebContent\score.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>score.html</title>
</head>
<body>
	<h1>Sum and Average of you Scores</h1>
	<form action="scoreResult.jsp" method="post">
		Korean: <input type="number" name="korean" min="0" max="100"><br/>
		English: <input type="number" name="english" min="0" max="100"><br/>
		Math: <input type="number" name="math" min="0" max="100"><br/>
		<input type="submit" value="SUBMIT">
		<input type="reset" value="RESET">
	</form>
</body>
</html>
//code2
//.\src\school\Score.java
package school;
public class Score {
	public Score() {
	}
	private int sum; 
	private double average;
	private StringBuilder print;
	public int getSum() {
		return sum;
	}
	public void setSum(int sum) {
		this.sum = sum;
	}
	public double getAverage() {
		return average;
	}
	public void setAverage(double average) {
		this.average = average;
	}
	public StringBuilder getPrint() {
		return print;
	}
	public void setPrint(StringBuilder print) {
		this.print = print;
	} 
}
<!-- code3 -->
<!-- .\WebContent\score.html -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>scoreResult.jsp</title>
</head>
<body>
	<%
		String scoreKorStr = request.getParameter("korean");
		String scoreEngStr = request.getParameter("english");
		String scoreMathStr = request.getParameter("math");
//		
		int scoreKor = Integer.parseInt(scoreKorStr);
		int scoreEng = Integer.parseInt(scoreEngStr);
		int scoreMath = Integer.parseInt(scoreMathStr);
//		
		int sum = scoreKor + scoreEng + scoreMath;
		double average = (double)sum / 3.;
//		
		StringBuilder print = new StringBuilder();
		print.append("You are ");
		if (average >= 90.) {
			print.append("A");
		} else if (average >= 80) {
			print.append("B");
		} else if (average >= 70) {
			print.append("C");
		} else if (average >= 60) {
			print.append("D");
		} else if (average >= 50) {
			print.append("E");
		} else if (average >= 40) {
			print.append("F");
		} else {
			print.setLength(0);
			print.append("Go home");
		};
		print.append(".");
	%>
	<jsp:useBean id="score" class="school.Score"/>
	<jsp:setProperty name="score" property="sum" value="<%=sum%>"/>
	<jsp:setProperty name="score" property="average" value="<%=average%>"/>
	<jsp:setProperty name="score" property="print" value="<%=print%>"/>
	Sum of three: <jsp:getProperty name="score" property="sum"/><br/>
	Average : <jsp:getProperty name="score" property="average"/><br/>
	<jsp:getProperty name="score" property="print"/><br/>
	<a href="score.html">BACK</a>
</body>
</html>



2.아래의 액션태그에 대하여 설명하시오.

  • forward : forward replaces the page contents without changing the page address.
  • include : include calls the page in the original page.
  • param : param sends data and the data can be requested.



3. forward , redirect 의 차이는?

  • The address gets changed in redirect but it is not changed in forward.



4.어제 배운 가위바위보 게임을 액션태그로 활용하여, 짜시오.

<!-- code1 -->
<!-- ./WebContent/rockPaperScissors.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>rockPaperScissors.html</title>
</head>
<body>
	<h1>Show me your hand.</h1>
	<form action="rockPaperScissors.jsp" method="post">
		<select name="rps">
			<option value="Rock">Rock</option>
			<option value="Paper">Paper</option>
			<option value="Scissors">Scissors</option>
		</select>
		<input type="submit" value="FIGHT">
	</form>
</body>
</html>
//code2
//./src/game/Rps.java
package game;
public class Rps {
	private StringBuilder print;
	public Rps() {
	}
	public StringBuilder getPrint() {
		return print;
	}
	public void setPrint(StringBuilder print) {
		this.print = print;
	}
}
<!-- code3 -->
<!-- ./WebContent/rockPaperScissors.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.concurrent.ThreadLocalRandom"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>rockPaperScissors.jsp</title>
</head>
<body>
	<%
		final String ROCK = "Rock";
		final String PAPER = "Paper";
		final String SCISSORS = "Scissors";
		final String[] HANDS = {ROCK, PAPER, SCISSORS};
//		
		String handStr = request.getParameter("rps");		
		String handRandom = HANDS[ThreadLocalRandom.current().nextInt(0, HANDS.length)];		
//		
		StringBuilder print = new StringBuilder();
		print.append("Yours is ");
		print.append(handStr);
		print.append(". Mine is ");
		print.append(handRandom);
		print.append(". ");
		if (handStr.equals(handRandom)) {
			print.append("Nobody won.");
		} else if ((handStr.equals(ROCK) && handRandom.equals(SCISSORS))
			|| (handStr.equals(PAPER) && handRandom.equals(ROCK))
			|| (handStr.equals(SCISSORS) && handRandom.equals(PAPER)))
		{
			print.append("You Won.");
		} else {
			print.append("You Lost.");
		};
	%>
	<jsp:useBean id="rps" class="game.Rps"/>
	<jsp:setProperty property="print" name="rps" value="<%=print%>"/>
	<jsp:getProperty property="print" name="rps"/>
	<br>
	<a href="rockPaperScissors.html">Try again?</a>
</body>
</html>
profile
Brave but clumsy

0개의 댓글