19.04.27 JSP 기본 제어문

Back-End/JSP 2019. 4. 27. 08:55
728x90
반응형

- if else -

 

if - else 문은 특정한 조건에 의해서 코드 실행 블록을 조정 할 수 있는 조건 제어문 이다.

 

 

  JSP에서 선언된 변수는 HTML의 태그 속성 및  다양한 곳에서 사용 할 수가 있다.

  예를 들어

 

  <% String id = "rorod";%>

  <FORM>

   ...............

  <INPUT TYPE="text" NAME="id" VALUE="<%=id%>">

   ...............

  </FORM>

 

  String 타입의 id를 FORM에 이름이 id인 텍스트 상자의 VALUE의 속성 값으로 사용을 한다.

  속성 뿐만 아니라 정적 코드인 HTML 및 자바스크립트에서 다양하게 사용을 하게 된다.

 

 

 

 

-예제 및 출력 결과-

 

1
2
3
4
5
6
7
8
9
10
11
<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> <input type="submit" value="보내기">
</form>
cs

 

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
<%@ page contentType="text/html;charset=euc-kr"%>
<%
    request.setCharacterEncoding("euc-kr");
%>
<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>
cs

 

 

 

 

- for (반복문) -

 

for문은 while문과 같이 대표적인 반복문이다.

반복문은 모두 스크립트 요소에서 사용하여 jsp 페이지에서 반복적인 내용을 출력할 수가 있다.

특히 Database의 질의 결과를 순서대로 출력할때 매우 유용하게 사용이 된다.

절대적인 것은 아니지만 일반적인 for문은 크기가 고정이 되어 있을 때 사용이 많이 되고,

while 문은 크기가 유동적일 때 사용이 많이 된다.

 

 

 

-예제 및 출력 결과-

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=EUC-KR"%>
<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%>
cs

 

 

-while (반복문)-

 

while문은 조건을 검사해서 조건이 참(true) 이면 실행문을 반복적으로 실행하고 그렇지 않으면 while문을 빠져 나오는 동작을 하는 반복문이다.

 

 

 

-예제 및 출력 결과-

 

1
2
3
4
5
6
7
8
<h1>While Example</h1>
<form method="post" action="while.jsp">
    반복하고 싶은 문구 : <input name="msg" size="20">
    <p />
    반복하고 싶은 횟수 : <input name="number">
    <p />
    <input type="submit" value="보내기">
</form>
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page contentType="text/html; charset=EUC-KR"%>
<h1>While Example</h1>
<%
    request.setCharacterEncoding("EUC-KR");
    String msg = request.getParameter("msg");
    int number = Integer.parseInt(request.getParameter("number")); //요청한 값이 정수이지만 html에서 jsp파일로 이동할 때 문자로 변환이 되서 다시 정수로 변환해야한다.
    int count = 0;                                                   //FORM에서 넘기는 값은 String값으로 넘어오기때문에 값의 데이터형과 일치하는 Wrapper 클래스로 변환시켜야됨.
    while(number>count)
    {
%>
<b><%=msg%> <%=count%> 번째 출력값</b>
<br />
<%
        count++;
    }
cs

 

 

 

728x90
반응형
: