Web Programming/JSP

[JSP] 기본 제어문

IT수정 2024. 10. 2. 11:09

if-else(조건 분기문)

if.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>IF-else Example</h1>
	<form method="POST" action="if.jsp">
		이름 : <input name="name"><p/>
		좋아하는 색깔 : <select name="color">
			<option value="blue" selected>파란색</option>
			<option value="red">붉은색</option>
			<option value="orange">오렌지색</option>
			<option value="etc">기타</option>
		</select><p/>
		<input type="submit" value="보내기">
	</form>
</body>
</html>

 

if.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<h1>If-else Example</h1>
<%!
	String msg;
%>
<%
	String name = request.getParameter("name");
	String color = request.getParameter("color");
	
	if (color.equals("blue")) {
	msg = "파란색";
	} else if (color.equals("red")) {
	msg = "붉은색";
	} else if (color.equals("orange")) {
	msg = "오렌지색";
	} else {
	color = "white";
	msg = "기타색";
	}
%>
<body bgcolor=<%=color%>>
<b><%=name%></b>님이 좋아하는 색깔은 <b><%=msg%></b>입니다.
</body>

 

출력 결과

 

for(반복문)

<%@ page contentType="text/html; charset=UTF-8"%>
<h1>For Example</h1>
1에서 10까지 합은?<p>
<%
	int i,sum = 0;
	for(i = 1; i <= 10; i++) {
	if(i < 10) {
%>
<%=(i + " +")%>
<%
	}else{
	out.println(i + " =");
	}//if-else
	sum += i;
	}//for
%>
<%=sum%>

 

while(반복문)

while.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>White Example</h1>
	<form method="post" action="while.jsp">
	반복하고 싶은 문구 : <input name="msg" size="20"><p/>
	반복하고 싶은 횟수 : <input name="number"><p/>
	<input type="submit" value="보내기">
	</form>
</body>
</html>

 

while.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<h1>While Example</h1>
<%
	request.setCharacterEncoding("UTF-8");
	String msg = request.getParameter("msg");
	int number = Integer.parseInt(request.getParameter("number"));
	int count = 0;
	while(number>count) {
%>
<b><%=msg%></b><br/>
<%
	count++;
	}
%>

 

출력 결과

 

연습 문제 1

while 문을 이용해 1에서 10까지의 합을 구하는 프로그램을 작성하시오.

 

경우의 수 1

<%@ page contentType="text/html; charset=UTF-8"%>
<h1>While Example2</h1>
<%
	int i = 1, sum = 0;

	while(i <= 10) {
		sum += i;
		if(i < 10) {
%>
<%=(i + " +")%>
<%
		} else {
%>
<%=(i + " =")%>
<%
		}
		i++;
	}
%>
<%=sum%>

 

경우의 수 2

<%@ page contentType="text/html; charset=UTF-8"%>
<h1>While Example2</h1>
<%
	int i = 1, sum = 0;

	while(i <= 10) {
		sum += i;
		if(i < 10) {
			out.println(i + " +");
		} else {
			out.println(i + " =");
		}
		i++;
	}
%>
<%=sum%>

 

연습 문제 2

for 문을 이용하여 구구단을 출력하는 프로그램을 작성하시오.

 

경우의 수 1

<%@ page contentType="text/html; charset=UTF-8"%>
<h1>GuguDan Example</h1>
<%
	for (int i = 1; i<=9; i++) {
		for (int j = 1; j<=9; j++) {
		out.println(i + " * " + j + " = " + i*j);
%>
		<br/>
<%
		}
		}
%>

 

경우의 수 2

<%@ page contentType="text/html; charset=UTF-8"%>
<h1>GuguDan Example</h1>
<%
	for (int i = 1; i<=9; i++) {
		for (int j = 1; j<=9; j++) {
%>
<%= (i + " * " + j + " = " + i*j)%><br/>
<%
		}
		}
%>

 

톰캣 기반에서의 한글 처리

서버에서 웹 브라우저에 응답되는 페이지의 화면 출력 시 한글처리

<%@ page contentType="text/html;charset=UTF-8"%>

 

웹 브라우저에서 서버로 넘어오는 파라미터 값에 한글이 있는 경우(post 방식) 한글처리

<% request.setCharacterEncoding("UTF-8");%>

 

웹 브라우저에서 서버로 넘어오는 파라미터 값에 한글이 있는 경우(get 방식) 한글처리

 

추가로 실제 서비스하는 환경인 톰캣홈\WEB-INF 폴더에 있는 server.xml 파일과 이클립스의 [Project Explorer] 뷰의 [Servers]-[Tomcat 버전 Server~] 항목에 있는 server.xml 파일에 한글 인코딩을 추가한다.

'Web Programming > JSP' 카테고리의 다른 글

[JSP] 내부 객체  (0) 2024.10.04
[JSP] 액션 태그  (1) 2024.10.02
[JSP] taglib Directive  (1) 2024.09.30
[JSP] include Directive  (1) 2024.09.30
[JSP] Page Directive  (0) 2024.09.30