1. 단순 if문
ifTestForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>값을 입력 받는 폼</title>
</head>
<body>
<h2>숫자값을 입력</h2>
<form method="post" action="ifTestPro.jsp">
<input type="text" name="number">
<input type="submit" value="입력완료">
</form>
</body>
</html>
ifTestPro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>입력 받은 숫자 비교</title>
</head>
<body>
<h2>입력 받은 숫자가 10보다 작거나 같은지 비교</h2>
<%
String strNumber = request.getParameter("number");
int number = Integer.parseInt(strNumber);
/* Line13: 일반적으로 폼으로부터 넘어온 값들은 모두 문자열 취급을 받게된다. */
if(number <= 10){
%>
입력 받은 숫자는 <%=number %>이다
<%} %>
</body>
</html>
2. if-else
ifElseTestForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>이름과 나이를 입력하는 폼</title>
</head>
<body>
<h2>이름과 나이를 입력</h2>
<form method="post" action="ifElseTestPro.jsp">
이름 : <input type="text" name="name"><br>
나이 : <input type="text" name="age"><br>
<input type="submit" value="입력완료">
</form>
</body>
</html>
ifElseTestPro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("utf-8"); %>
<%
String name= request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
if(age>=20){
out.print("<b>"+name+"</b> 님의 나이는 20세 이상입니다.");
}else{
out.println("<b>"+name+"</b> 님은 미성년 입니다.");
}
%>
<!-- <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html> -->
3. switch
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>권역을 선택하는 폼</title>
</head>
<body>
<h2>권역을 선택하세요</h2>
<form method="post" action="switchTestPro.jsp">
<input type="radio" name="localNum" value="0 checked">0권역<br>
<input type="radio" name="localNum" value="1">1권역<br>
<input type="radio" name="localNum" value="2">2권역<br>
<input type="radio" name="localNum" value="3">3권역<br>
<input type="radio" name="localNum" value="4">4권역<br>
<input type="submit" value="입력완료">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%request.setCharacterEncoding("utf-8");%>
<%
int localNum = Integer.parseInt(request.getParameter("localNum"));
String localName = "";
switch(localNum){
case 0:
localName="종로, 중구, 용산";
break;
case 1:
localName="도봉, 강북, 노원";
break;
case 2:
localName="동대문, 성동, 광진";
break;
case 3:
localName="강동, 송파";
break;
case 4:
localName="서초, 강남";
break;
default:
localName="없는 권역";
break;
}
out.println("선택하신 지역ㅇ느 <b>" +localName +"</b> 입니다.");
%>
4. for 문
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String strArray[]={"java", "jsp", "html"};
for(int i = 0; i<strArray.length; i++){
out.println("strArray["+i+"]의 값은 :"+strArray[i]+"<br>");
}
%>
5. while 문
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
int i = 0;
while(i<10){
out.println("출력된값:"+i+"<br>");
i++;
}
%>
'개발 > JSP' 카테고리의 다른 글
[JSP] 액션 태그 (0) | 2021.04.05 |
---|---|
[JSP] 내장 객체 (0) | 2021.04.04 |
[JSP] 페이지의 스크랩트 요소 (0) | 2021.04.03 |
[JSP] 디렉토리 (0) | 2021.04.03 |
[JSP] 프로그래밍의 개요 (0) | 2021.04.02 |