37: Servlet JSTL

jk·2024년 2월 23일
0

kdt 풀스택

목록 보기
71/127



1. emp_list.jsp 에서

el 과 jstl 을 활용하여, 12명을 테이블로 나오게 하시오.

//code1
//./src/emp/EmpDTO.java
package emp;
public class EmpDTO {
	private String empno;
	private String ename;
	private String job;
	private String mgr;
	private String hiredate;
	private String sal;
	private String comm;
	private String deptno;
	public EmpDTO() {
	}
	public EmpDTO(String empno, String ename, String job, String mgr, String hiredate, String sal, String comm,
			String deptno) {
		super();
		this.empno = empno;
		this.ename = ename;
		this.job = job;
		this.mgr = mgr;
		this.hiredate = hiredate;
		this.sal = sal;
		this.comm = comm;
		this.deptno = deptno;
	}
	public String getEmpno() {
		return empno;
	}
	public void setEmpno(String empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public String getMgr() {
		return mgr;
	}
	public void setMgr(String mgr) {
		this.mgr = mgr;
	}
	public String getHiredate() {
		return hiredate;
	}
	public void setHiredate(String hiredate) {
		this.hiredate = hiredate;
	}
	public String getSal() {
		return sal;
	}
	public void setSal(String sal) {
		this.sal = sal;
	}
	public String getComm() {
		return comm;
	}
	public void setComm(String comm) {
		this.comm = comm;
	}
	public String getDeptno() {
		return deptno;
	}
	public void setDeptno(String deptno) {
		this.deptno = deptno;
	}
}
//code2
//./src/emp/EmpDAO.java
package emp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class EmpDAO {
	private DataSource dataSource = null;
	public EmpDAO() {
		try {
			Context context = new InitialContext();
			dataSource = (DataSource)context.lookup("java:comp/env/jdbc/oracle");
		} catch(Exception e) {
			e.printStackTrace();
		};
	}			
	public List<EmpDTO> empList() {
		List<EmpDTO> list = new ArrayList<EmpDTO>();
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String query = "select * from emp";
		System.out.println(query);
		try {
			connection = dataSource.getConnection();
			pstmt = connection.prepareStatement(query);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				list.add(new EmpDTO(
					rs.getString("ename"),
					rs.getString("empno"),
					rs.getString("job"),
					rs.getString("mgr"),
					rs.getString("hiredate"),
					rs.getString("sal"),
					rs.getString("comm"),
					rs.getString("deptno")
				));
			};
			System.out.println("SUCESS");
		} catch(Exception e) {
			System.out.println("FAIL");
			e.printStackTrace();
		};
		return list;
	}
}
<!-- code3 -->
<!-- ./WebContent/emp_list.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="emp.*" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>emp_list.jsp</title>
<style>
body {
	font-family:verdana, monospace
}
h1 {
	color: red
}
table, th, td {
  border:1px solid green;
  color: skyblue
}
</style>
</head>
<body>
<%
	EmpDAO deptDao = new EmpDAO();
	List<EmpDTO> dtos = deptDao.empList();
	pageContext.setAttribute("dtos", dtos);
%>
<h1>EMP LIST</h1>
<table>
	<c:forEach var="dto" items="${dtos}">
		<tr>
			<td>${dto.getEmpno()}</td>
			<td>${dto.getEname()}</td>
			<td>${dto.getJob()}</td>
			<td>${dto.getMgr()}</td>
			<td>${dto.getHiredate()}</td>
			<td>${dto.getSal()}</td>
			<td>${dto.getComm()}</td>
			<td>${dto.getDeptno()}</td>
		</tr>
	</c:forEach>
</table>
</body>
</html>



2. JSTL 문법에서 아래의 예를 드시오.

<c:set 
<c:out
<c:remove
<c:if
<c:choose>
<c:forEach
<c:forTokens
<c:set var="set" value="SET"/>
<c:out value="${set}"/><br/>
<c:remove var="set"/>
<c:out value="${set}"/><br/>
<c:if test="${1 lt 2}">
	1 is less than 2.<br/>
</c:if>
<c:choose>
	<c:when test="${1 lt 2}">1 is less than 2.</c:when>
	<c:when test="${1 eq 2}">1 is same with 2.</c:when>
	<c:when test="${1 gt 2}">1 is greater than 2.</c:when>
</c:choose><br/>
<c:forEach var="i" begin="0" end="100" step="10">
	${i}&nbsp
</c:forEach><br/>
<c:forTokens var="alphabet" items="a b c" delims=" ">
	${alphabet}<br/>
</c:forTokens>

profile
Brave but clumsy

0개의 댓글