2016년 8월 12일 금요일

10day JSP

1.JSTL 세팅
라이브러리 프로젝트 추가


2. c태그
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%--접두어 c로 core태그 사용 --%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- core태그 : 서버에서 처리되는 자바 소스이기 때문에 브라우저 소스보기에서 태그 안 보임 -->
출력 : <c:out value="browser1" /> <br>
<%
    pageContext.setAttribute("data", "1");
%>
출력 : <c:out value="${ data }" /> <br> <%-- EL식을 안에 써줘도 됨 --%>
</body>
</html>


3
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%--접두어 c로 core태그 사용 --%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- core태그 : 서버에서 처리되는 소스이기 때문에 브라우저 소스보기에서 태그 안 보임 -->
    출력 : <c:out value="browser1" /><br>
    <!-- JSP 선언 -->
    <%
        pageContext.setAttribute("data1", "1");
    %>
    출력 : <c:out value="${ data1 }" /> <br>
    <!-- c태그 선언 -->
    <c:set var="data2" value="2" scope="page" /><br>
    출력 : <c:out value="${ data2 }" /> <br>
    <!-- c태그 선언 2번째 방법-->
    <c:set var="data3" scope="page">3</c:set><br>
    출력 : <c:out value="${ data3 }" /> <br>
    <!-- 변수 제거 -->
    <c:remove var="data1"/><br>
    출력 : <c:out value="${ data1 }" /> <br>
    출력 : <c:out value="${ data1 }" default="값 없음" /> <br>
</body>
</html>


4
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        //java 코드로 map에 씀
        java.util.HashMap<String, String> map1 = new java.util.HashMap<>();
        map1.put("name", "홍길동2");
        map1.put("age", "30");
        pageContext.setAttribute("map", map1);  //map1은 실제 데이터저장됨  "map"은 문자열 KEY
    %>
    <%-- c태그로 같은 map에 씀 --%>
    <c:set var="map" value="<%= new java.util.HashMap<String, String>() %>" /><%-- scope='page' 디폴트 --%>
    <c:set target="${map }" property="name" value="홍길동1" />
    <c:set target="${map }" property="age" value="20" />
    <%
        //java 코드 출력
        java.util.HashMap map2 = (java.util.HashMap)pageContext.getAttribute("map");
        out.println(map2.get("name")+"<br>");
        out.println(map2.get("age")+"<br>");
    %>
    <%-- EL출력 --%>
    출력 : ${ map.name }<br>
    출력 : ${ map.age }<br>
    
    <%-- c태그를 쓸지 java 코드를 쓸지는 선택의 문제 --%>
</body>
</html>


5
package model1;

public class BoardTO {
 private String subject;
 private String writer;
 
 public String getSubject() {
  System.out.println("getSubject 호출");
  return subject;
 }
 public String getWriter() {
  System.out.println("getWriter 호출");
  return writer;
 }
 public void setSubject(String subject) {
  this.subject = subject;
 }
 public void setWriter(String writer) {
  this.writer = writer;
 }
 
}

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- c태그로 사용자 정의 Bean 객체 생성 -->
    <c:set var="to1" value="<%= new model1.BoardTO() %>" />
    <c:set target="${to1 }" property="subject" value="제목1" />
    <c:set target="${to1 }" property="writer" value="작성자1" />
    제목 : ${ to1.subject }<br>
    작성자 : ${ to1.writer }<br>
    
    <c:set var="to2" value="<%= new model1.BoardTO() %>" />
    <c:set target="${to2 }" property="subject" value="제목2" />
    <c:set target="${to2 }" property="writer" value="작성자2" />
    
    <!-- c태그로 HashMap 객체 생성 -->
    <c:set var="map" value="<%= new java.util.HashMap<>() %>" />
    <c:set target="${map }" property="to1" value="${to1 }" />
    <c:set target="${map }" property="to2" value="${to2 }" />
    제목 : ${ map.to1.subject }<br>
    작성자 : ${ map.to2.writer }<br>
</body>
</html>


6
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- c 조건문if -->
    <c:if test="true">
        무조건 실행 조건문<br>
    </c:if>
    
    <c:set var="country" value="korea" />
    <c:if test="${country =='korea' }">
        한국입니다.<br>
    </c:if>
    
    <!-- c 조건문else if -->
    <c:choose>
        <c:when test="${ country == 'korea' }">
            <c:out value="${country }" />
        </c:when>
        <c:when test="${country =='canada' }">
            <c:out value="${country }" />
        </c:when>
        <c:otherwise>
            안춥다.
        </c:otherwise>
    </c:choose>
</body>
</html>


7
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="core05.jsp" method="post">
        학점 : <input type="text" name="hakjum" />
        <input type="submit" value = "학점보기" />
    </form>
    <hr />
    
    <!-- c태그 학점계산 -->
    <c:if test="${ !empty(param.hakjum) }">
        <c:choose>
            <c:when test="${param.hakjum >=90 }">
                ${param.hakjum }의 학점은 A입니다.
            </c:when>
            <c:when test="${param.hakjum >=80 }">
                ${param.hakjum }의 학점은 B입니다.
            </c:when>
            <c:when test="${param.hakjum >=70 }">
                ${param.hakjum }의 학점은 C입니다.
            </c:when>
            <c:when test="${param.hakjum >=60 }">
                ${param.hakjum }의 학점은 D입니다.
            </c:when>
            <c:otherwise>
                ${param.hakjum }의 학점은 F입니다.
            </c:otherwise>
        </c:choose>
    </c:if>
</body>
</html>


8
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- c태그 반복문 -->
    <c:forEach var="i" begin="1" end="5"> <%-- step='1' 디폴트 --%>
        <c:out value="${i }" /><br>
    </c:forEach>
    <br>
    <c:forEach var="i" begin="1" end="5" step="2">
        <c:out value="${i }" /><br>
    </c:forEach>
    <br>
    <!-- c태그 이중 반복문 -->
    <c:forEach var="i" begin="1" end="3">
        <c:forEach var="j" begin="1" end="3">
            <c:out value="${i }" />-<c:out value="${j }" /><br>
        </c:forEach>
    </c:forEach>
    
    
</body>
</html>


9
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- c태그 구구단 -->
    <table border="1">
    <c:forEach var="i" begin="0" end="9"><%-- 선언한 i는 pageContext에 올라가서 EL태그로 받을 수 있음 --%>
        <tr>
        <c:forEach var="j" begin="0" end="9">
            <td>
            <c:choose>
                <c:when test="${i==0 && j==0}"></c:when>
                <c:when test="${i==0}">
                    *${j}
                </c:when>
                <c:when test="${j==0}">
                    ${i}단
                </c:when>
                <c:otherwise>
                    ${i}*${j}=${i*j}
                </c:otherwise>
            </c:choose>
            </td>
        </c:forEach>
        </tr>
    </c:forEach>
    </table>    
</body>
</html>


10
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <c:set var="intArray" value="<%= new int[]{1,2,3,4,5} %>"/>
    <c:out value="${intArray[0] }" /><br>
    <c:out value="${intArray[1] }" /><br>
    <hr/>
    <!-- c태그 향상된 forEach 배열-->
    <c:forEach var="i" items="${intArray }">
        <c:out value="${i }" /><br>
    </c:forEach>
    <hr/>
    <!-- c태그 향상된 forEach 배열 시작끝 지정-->
    <c:forEach var="i" items="${intArray }" begin="2" end="4">
        <c:out value="${i }" /><br>
    </c:forEach>
    <hr>
    <!-- c태그 향상된 forEach 해쉬맵-->
    <c:set var="mapData" value="<%=new java.util.HashMap() %>" />
    <c:set target="${ mapData }" property="name" value="홍길동" />
    <c:set target="${ mapData }" property="today" value="<%= new java.util.Date() %>" />
    <c:forEach var="i" items="${mapData }">
        ${i.key } = ${ i.value } <br>
    </c:forEach>
    <hr>
    <!-- header 값 추출 -->
    <c:forEach var="h" items="${headerValues }">
        <c:forEach var="val" items="${h.value }">
            param: ${h.key } / values: ${val }<br>
        </c:forEach>
    </c:forEach>
</body>
</html>


11
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="model1.BoardTO"%>
<%@page import="java.util.ArrayList"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    BoardTO to1 = new BoardTO();
    to1.setSubject("제목 1");
    to1.setWriter("이름 1");
    BoardTO to2 = new BoardTO();
    to2.setSubject("제목 2");
    to2.setWriter("이름 2");
    
    ArrayList<BoardTO> lists = new ArrayList<>();
    lists.add(to1);
    lists.add(to2);
    
    pageContext.setAttribute("lists", lists);
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- c태그 forEach ArrayList-->
<c:forEach var="i" begin="0" end="<%=lists.size()-1 %>">
    ${lists[i].subject }<br>
</c:forEach>
    
</body>
</html>


12
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- c태그 문자열분리 -->
<c:set var="delim_str" value="김승현,이승현,박승현,최승현" scope="request" />
<c:forTokens var="obj" items="${delim_str }" delims="," varStatus="pos"> <%-- varStatus 현재상태 --%>
    <c:out value="${pos.count }" /> : <c:out value="${obj }" /><br>
</c:forTokens>

</body>
</html>


13
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!-- c태그 redirect-->
<c:redirect url="http://search.naver.com/search.naver">
    <c:param name="where" value="nexearch" />
    <c:param name="sm" value="top_hty" />
    <c:param name="fbm" value="1" />
    <c:param name="ie" value="utf-8" />
    <c:param name="query" value="hobbit" />
</c:redirect>


14
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!-- c태그 url : 경로값을 저장-->
<c:url var="url1" value="./shopping.do"/>
<c:url var="url1" value="http://Localhost:8080/shopping.do">
    <c:param name="add" value="t001"/>
</c:url>

${url1 }<br>

<c:url var="url" value="http://search.naver.com/search.naver">
    <c:param name="where" value="nexearch" />
    <c:param name="sm" value="top_hty" />
    <c:param name="fbm" value="1" />
    <c:param name="ie" value="utf-8" />
    <c:param name="query" value="hobbit" />
</c:url>

${url }<br>

<%-- <c:redirect url="${url }" /> --%>


15
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<br><hr>
<!-- import 경로를 바꾸지 않고 url연결 -->
<!-- 소스보기를 보면 HTML태그를 가져와서 넣는 식으로 되어있음 -->
<c:import url="http://m.naver.com" />


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<br><hr>
<!-- import -->
<!-- var에 넣고 HTML을 출력할 수있음 -->
<c:import var="data" url="http://m.naver.com" />

<textarea rows="50" cols="600">
${ data }
</textarea>


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<br><hr>
<!-- import 구글지도 HTML 출력 -->
<c:import var="data" url="https://maps.googleapis.com/maps/api/geocode/xml">
    <c:param name="Language" value="ko" />
    <c:param name="address" value="역삼역" />
    <c:param name="sensor" value="false" />
</c:import>

<textarea rows="50" cols="600">
<c:out value="${data }"></c:out>
<%
    System.out.println(pageContext.getAttribute("data"));  //console창에 출력
%>
</textarea>


16. s태그 sql

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="http://java.sun.com/jsp/jstl/sql" %>
<!-- s태그 : 자바코드를 안쓰고 DB연결할 수있음-->
<s:setDataSource 
    url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
    driver="oracle.jdbc.driver.OracleDriver"
    user="scott"
    password="tiger" 
    var="ds" />

<s:update dataSource="${ds }">
    insert into dept values(99, '개발', '강릉')
</s:update>

완료되었습니다.


17
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="http://java.sun.com/jsp/jstl/sql" %>
<!-- s태그 : 자바코드를 안쓰고 DB연결할 수있음-->
<s:setDataSource 
    url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
    driver="oracle.jdbc.driver.OracleDriver"
    user="scott"
    password="tiger" 
    var="ds" />
<!-- 태그안에 sql문 쓸수 있음 -->
<s:update dataSource="${ds }" sql="insert into dept values(98, '개발', '서울')" />

완료되었습니다.


18
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="http://java.sun.com/jsp/jstl/sql" %>
<!-- s태그 : 자바코드를 안쓰고 DB연결할 수있음-->
<s:setDataSource 
    url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
    driver="oracle.jdbc.driver.OracleDriver"
    user="scott"
    password="tiger" 
    var="ds" />
<!-- preparedStatement 쓸수 있음 -->
<s:update dataSource="${ds }" sql="insert into dept values(?, ?, ?)">
    <s:param value="97" />
    <s:param value="홍보" />
    <s:param value="대구" />
</s:update>

완료되었습니다.


19
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<s:setDataSource 
    url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
    driver="oracle.jdbc.driver.OracleDriver"
    user="scott"
    password="tiger" 
    var="ds" />
    
<!-- s태그 select -->
<s:query var="rs" dataSource="${ds }" sql="select * from dept" />

<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- c태그 forEach 출력 -->
<table border="1" width="600">
    <c:forEach var="cName" items="${rs.columnNames }"> <%--열이름 --%>
        <td>${cName }</td>
    </c:forEach>
    
    <c:forEach var="data" items="${rs.rows }">
        <tr>
        <td>${data.deptno }</td>  <%--객체식으로 출력 --%>
        <td>${data['dname'] }</td> <%--배열형식으로 출력해도 됨 --%>
        <td>${data.loc }</td>
        </tr>
    </c:forEach>
</table>

</body>
</html>


20


  

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- pooing -->
    <%-- <s:setDataSource dataSource="jdbc/oracle" var="ds" />
         <s:query var="rs" dataSource="${ds }" sql="select * from dept" /> --%>
<s:query dataSource="jdbc/oracle" var="rs" sql="select * from dept" /> <%--직접 쿼리문에 소스를 연결하는 방법 --%>

<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 출력 -->
<table border="1" width="600">
    <c:forEach var="cName" items="${rs.columnNames }">
        <td>${cName }</td>
    </c:forEach>
    
    <c:forEach var="data" items="${rs.rows }">
        <tr>
        <td>${data.deptno }</td>
        <td>${data['dname'] }</td>
        <td>${data.loc }</td>
        </tr>
    </c:forEach>
</table>

</body>
</html>


21
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>format 설정</title>
</head>
<body>
    <!-- fmt태그 택스트형식 조정 -->
    숫자 형식: <fmt:formatNumber value="1234.56" type="number" /><br>
    통화 형식: <fmt:formatNumber value="1234.56" type="currency" currencySymbol="$" /><br>
    % 형식: <fmt:formatNumber value="1234.56" type="percent" /><br>
    지정패턴: <fmt:formatNumber value="1234.567" pattern="#,###.0" /><br>
    최대 자리수: <fmt:formatNumber value="1234.56" maxIntegerDigits="3" /><br>
    최대 소수점 자리수: <fmt:formatNumber value="1234.56" maxFractionDigits="1" /><br>
    숫자 분석(정수지정): <fmt:parseNumber value="1234.56" integerOnly="true" /><br><br>
    <hr><br>
    <c:set var="datetime" value="<%= new java.util.Date() %>" scope="request" />
    기본 시간형식: <c:out value="${datetime }" /><br>
    일 형식: <fmt:formatDate value="${datetime }" type="date" timeStyle="full" /><br>
    시간 형식: <fmt:formatDate value="${datetime }" type="time" dateStyle="full" /><br>
    일,시간 둘다: <fmt:formatDate value="${datetime }" type="both" dateStyle="full" timeStyle="full" /><br>
    지정패턴:
        <fmt:parseDate value="20100216201524" pattern="yyyyMMddHHmmss" var="mydate" />
        <fmt:formatDate value="${mydate }" pattern="yyyy-MM-dd HH:mm:ss" />

</body>
</html>


댓글 없음:

댓글 쓰기