JSP el (1)

* ${} 를 사용하여 출력 

\(역슬래시) : 연산하지 않고 그냥 표현

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP el (2)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP el_scope

${ 키명 } : 현재 범위에서 가장 작은 속성 출력

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP jstl (1)

설치 방법

https://tomcat.apache.org/taglibs/standard/

standard1.1 - download - binaries - jakarta-taglibs-standard-1.1.2.zip(933k) - lib파일 안에 두 jar파일 옮기기

 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> : jstl을 c로 사용하겠다는 뜻

<c:out value="내용" /> : 내용을 출력

<c:set var="변수명" value="변수값" /> : 변수 설정

<c:if test="조건" /> : 조건이 true면 실행

<c:choose> : choose태그 안에 if, else if, else문 작성 가능

첫번째 <c:when> : if문, 나머지 <c:when> : else if문, <c:otherwise> : else문

<c:forEach var="변수" begin="시작값" end="끝값" step="증가값"> : for문(step 생략 가능)

<c:set var="배열명" value="첫번째 배열값, 두번째, ...." /> : 배열 생성

<c:forEach var="변수명" items="${배열명}"> : 배열 출력

<c:forTokens var="저장될 변수명" items="${변수명}" delims="/"> : /를 기준으로 나눠서 출력

<c:catch var="result" /> : 태그 안에 내용에 오류 발생시 오류내용이 result로 저장됨

<c:import url="경로" /> : 해당 경로에 있는 내용을 가져옴

<c:remove var="변수명" /> : 해당 변수를 삭제

<c:redirect url="경로"> : 해당 경로로 이동

<c:url var="변수명" value="현재 최상위 폴더 뒤에 경로"> : value가 abcd이면, 7_el_jstl/abcd로 적용

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	ex01.jsp<br>
	<c:out value="안녕하세요" /><br>
	<c:set var="num" value="12345"/>
	c:out : <c:out value="${ num }" /><br>
	\%{ num } : ${ num }<br>
	<hr>
	<h3>c:if</h3>
	<c:set var="num1" value="12345" />
	<c:if test="${ num1 == num }">
		<b>두 값은 같다</b>
	</c:if>
	<c:if test="${ num1 ne num }">
		<b>두 값은 같지않다</b>
	</c:if>
	<hr>
	<c:set var="number" value="100" />
	<c:choose>
		<c:when test="${number > 90 }">
			90보다 크다
		</c:when>
		<c:when test="${number > 70 }">
			70보다 크다
		</c:when>
		<c:otherwise>
			그 외의 값 실행
		</c:otherwise>
	</c:choose>
	<hr>
	<h3>forEach</h3>
	<c:forEach var="i" begin="1" end="5" step="1">
		i : ${ i }<br>
	</c:forEach>
	<hr>
	<c:set value="0" var="sum" />
	<c:set value="10" var="n" />
	<c:forEach var="i" begin="1" end="${ n }">
		<c:set value="${ sum + i }" var="sum" />
	</c:forEach>
	sum : ${ sum }<hr>
	<c:set var="arr" value="홍길동, 김개똥, 고길똥" />
	<c:forEach var="item" items="${ arr }">
		${ item }<br>
	</c:forEach><hr>
	<h3>forTokens</h3>
	<c:set var="str" value="안녕/하세/요" />
	<c:forEach var="item" items="${ str }">
		${ item }<br>
	</c:forEach>
	<hr>
	<c:forTokens var="item" items="${ str }" delims="/">
		${ item }<br>
	</c:forTokens>>
	<hr>

	<c:catch var="result">
		<%= 10/0 %>
	</c:catch>
	<c:if test="${ result != null }">
		<b>문제 발생 : ${ result }</b>
	</c:if>
	
	<c:set var="name" value="홍길동" scope="session" />
	<hr>
	<a href="ex02_session.jsp">session확인</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3>test페이지 입니다</h3>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:import url="ex02_test.jsp" />
	<hr color="hotpink" size="10">
	<h3>session 확인</h3>
	session : ${ name }<br>
	${ sessionScope.name }
	<hr>
	<a href="ex02_delete.jsp">세션 삭제</a>
	<hr>
	getContextPath : <%= request.getContextPath() %><br>
	<a href="<%= request.getContextPath() %>/jstl/ex01.jsp">ex01</a>
	
	<c:set var="path" value="<%= request.getContextPath() %>"></c:set>
	<a href="${ path }/jstl/ex01.jsp">ex01</a>
	
	<a href="<c:url value="/jstl/ex01.jsp" /> ">ex01</a>
	
	<c:url var="url" value="/jstl/ex01.jsp" />
	<br>url : ${ url }
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:remove var="name"/>
	<c:redirect url="/jstl/ex02_session.jsp" />
</body>
</html>

 

실행 결과

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP jstl (2)

 

실행 결과

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP jstl (3)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
	<h1>모든 목록 확인</h1>
	<jsp:useBean id="dao" class="test.TestDAO"/>
	<c:set var="list" value="${dao.getList() }" />
	<c:forEach var="dto" items="${list }">
		name : ${dto.getName() }<br>
		addr : ${ dto.addr }<hr>
	</c:forEach>
	<a href="ex04_one_data.jsp?num=1">하나의 데이터 확인</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
	<jsp:useBean id="dao" class="test.TestDAO"></jsp:useBean>
	<c:set var="dto" value="${ dao.getData(param.num) }" />
	<h1>하나의 목록</h1>
	name : ${dto.name }<br>
	addr : ${dto.addr }<br>
	<a href="<c:url value='/jstl/ex04_all_data.jsp' />">목록 이동</a>
</body>
</html>
package test;

public class TestDTO {
	String name, addr;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}
	
}
package test;

import java.util.ArrayList;

public class TestDAO {
	ArrayList<TestDTO> list;
	public TestDAO() {
		list = new ArrayList<>();
		for(int i=0 ; i<5 ; i++) {
			TestDTO dto = new TestDTO();
			dto.setName("홍길동 "+i);
			dto.setAddr("산골짜기 "+i);
			list.add(dto);
		}
	}
	public ArrayList<TestDTO> getList(){
		return list;
	}
	public TestDTO getData(int num) {
		return list.get(num);
	}
}

 

실행 결과

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP header(include)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<c:url var="path" value="/" />
	<h1 align="center">Test Web</h1>
	<hr>
	<a href="${ path }index.jsp">HOME</a>&nbsp; &nbsp;
	<a href="${ path }member/m_list.jsp">MEMBERS</a>&nbsp; &nbsp;
	<c:if test="${loginId == null }">
		<a href="${ path }member/m_login.jsp">LOGIN</a>
	</c:if>
	<c:if test="${loginId != null }">
		<a href="${ path }member/m_logout.jsp">LOGOUT</a>
	</c:if>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ include file="/common/header.jsp" %>
	<h1>기본 페이지 입니다!!!</h1>
</body>
</html>

 

실행 결과

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP login / logout

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ include file="/common/header.jsp" %>
	<h1>login 페이지입니다!!!</h1>
	${param.result}
	<c:choose>
		<c:when test="${loginId == null}">
			<form action="${path}member/m_login_check.jsp" method="post">
				<input type="text" name="id" placeholder="input id"><br>
				<input type="text" name="pwd" placeholder="input pwd"><br>
				<input type="submit" value="login">
				<a href="${path}member/m_register_form.jsp">회원가입</a>
			</form>
		</c:when>
		<c:otherwise>
			${loginName}님 로그인 상태입니다!!<br>
			<a href="${path}member/m_logout.jsp">로그아웃</a>
		</c:otherwise>
	</c:choose>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<jsp:useBean id="dao" class="member.MemberDAO" />
	<c:set var="dto" value="${dao.getOneMember(param.id)}" />
	dto : ${dto}<br>
	<c:choose>
		<c:when test="${dto == null}">
			<c:set var="result" value="존재하지 않는id입니다!!" />
		</c:when>
		<c:otherwise>
			<c:choose>
				<c:when test="${dto.pwd == param.pwd}">
					<c:set var="result" value="인증 성공!!" />
					<c:set var="loginId" value="${dto.id}" scope="session" />
					<c:set var="loginName" value="${dto.name}" scope="session" />
				</c:when>
				<c:otherwise>
					<c:set var="result" value="존재하지 않는pwd입니다!!" />
				</c:otherwise>
			</c:choose>
		</c:otherwise>
	</c:choose>
	<c:redirect url="/member/m_login.jsp">
		<c:param name="result" value="${result}"></c:param>
	</c:redirect>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		session.invalidate();
		response.sendRedirect(request.getContextPath()+"/member/m_login.jsp");
	%>
</body>
</html>

 

실행 결과

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP register

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ include file="/common/header.jsp" %>
	<hr>
	${param.result}
	<form action="${path}member/m_register.jsp" method="post">
		<input type="text" name="id" placeholder="아이디"><br>
		<input type="text" name="pwd" placeholder="비밀번호"><br>
		<input type="text" name="name" placeholder="이름"><br>
		<input type="text" name="addr" placeholder="주소"><br>
		<input type="text" name="tel" placeholder="전화번호"><br>
		<input type="submit" value="회원가입"><br>
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<% request.setCharacterEncoding("utf-8"); %>
	<jsp:useBean id="dto" class="member.MemberDTO"></jsp:useBean>
	<jsp:setProperty property="*" name="dto" />
	
	<jsp:useBean id="dao" class="member.MemberDAO"></jsp:useBean>
	<c:choose>
		<c:when test="${dao.register(dto) == 1}">
			<c:set var="result" value="회원가입 되었습니다!!!"></c:set>
			<c:set var="url" value="m_login.jsp"></c:set>
		</c:when>
		<c:otherwise>
			<c:set var="result" value="문제 발생!!!"></c:set>
			<c:set var="url" value="m_register_form.jsp"></c:set>
		</c:otherwise>
	</c:choose>
	<c:redirect url="${url}">
		<c:param name="result" value="${result}"></c:param>
	</c:redirect>
</body>
</html>

 

실행 결과

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP list & info

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ include file="/common/header.jsp" %>
	<jsp:useBean id="dao" class="member.MemberDAO" />
	<h3>모든 목록 확인</h3>
	<table border="1">
		<tr>
			<th>이름</th>
			<th>주소</th>
			<th>전화번호</th>
		</tr>
		<c:set var="list" value="${dao.getList()}"></c:set>
		<c:forEach var="dto" items="${list}">
		<tr>
			<td>
				<c:choose>
					<c:when test="${loginId ne null }">
						<c:set var="result" value="/m_info.jsp?id=${dto.id}"></c:set>
					</c:when>
					<c:otherwise>
						<c:set var="result" value="/m_login.jsp"></c:set>
					</c:otherwise>
				</c:choose>
				<a href="${path}member${result}">${dto.name}</a>
			</td>
			<td>${dto.addr}</td>
			<td>${dto.tel}</td>
		</tr>
		</c:forEach>
	</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ include file="/common/header.jsp" %>
	<jsp:useBean id="dao" class="member.MemberDAO"></jsp:useBean>
	<c:set var="dto" value="${dao.getOneMember(param.id)}"></c:set>
	<h3>- 개 인 정 보 -</h3>
	id : ${dto.id}<br>
	pwd : ${dto.pwd}<br>
	name : ${dto.name}<br>
	addr : ${dto.addr}<br>
	tel : ${dto.tel}<br>
	<hr>
	<input type="button" value="수 정" 
		onclick="location.href='m_modify_form.jsp?id=${dto.id}'" >
	<input type="button" value="삭 제" 
		onclick="location.href='m_delete.jsp?id=${dto.id}'" > 
</body>
</html>

 

실행 결과

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP modify

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
	<style type="text/css">
		label {display: inline-block; width: 70px;}
	</style>
</head>
<body>
	<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
	<%@ include file="/common/header.jsp" %>
	<jsp:useBean id="dao" class="member.MemberDAO"></jsp:useBean>
	<c:set var="dto" value="${dao.getOneMember(param.id)}"></c:set>
	<form action="m_modify.jsp" method="post">
		<label>아이디</label><input type="text" readonly name="id" value="${dto.id }"><br>
		<label>비밀번호</label><input type="text" name="pwd" value="${dto.pwd }"><br>
		<label>이 름</label><input type="text" name="name" value="${dto.name }"><br>
		<label>주 소</label><input type="text" name="addr" value="${dto.addr }"><br>
		<label>전화번호</label><input type="text" name="tel" value="${dto.tel }"><br>
		<input type="submit" value="완료">
		<input type="reset" value="취소" onclick="javascript:history.back();">
	</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
	<% request.setCharacterEncoding("utf-8"); %>
	<jsp:useBean id="dto" class="member.MemberDTO"></jsp:useBean>
	<jsp:setProperty property="*" name="dto"/>
	
	<jsp:useBean id="dao" class="member.MemberDAO"></jsp:useBean>
	
	<c:choose>
		<c:when test="${dao.modify(dto) == 1 }">
			<c:redirect url="m_info.jsp?id=${dto.id}"></c:redirect>
		</c:when>
		<c:otherwise>
			<c:redirect url="m_modify_form.jsp?id=${dto.id}"></c:redirect>
		</c:otherwise>
	</c:choose>
</body>
</html>

 

실행 결과

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JSP delete

<%@page import="member.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		MemberDAO dao = new MemberDAO();
		int result = dao.delete(request.getParameter("id"));
		
		if(result == 1){
			response.sendRedirect("m_list.jsp");
		}else{
			response.sendRedirect("m_info.jsp?id=" + request.getParameter("id"));
		}
	%>
</body>
</html>

 

실행 결과

 

'jsp' 카테고리의 다른 글

JSP 활용(3)  (0) 2024.05.02
JSP 활용 (1)  (0) 2024.04.30
JSP 기초  (0) 2024.04.29

+ Recent posts