39: Servlet RequestDispatcher.forward(request, response)

jk·2024년 2월 27일
0

kdt 풀스택

목록 보기
75/127



1. 아래를 구현하시오.

- /emp/list.do 를 e-shopper 템플릿에서 나오도록 하시오. 
- EmpController.java 서블릿 파일을 따로 만들것.
//code1
//./src/dto/EnameAndDnameDto.java
package dto;
public class EnameAndDnameDto {
	private String ename;
	private String dname;
	public EnameAndDnameDto() {
	}
	public EnameAndDnameDto(String ename, String dname) {
		super();
		this.ename = ename;
		this.dname = dname;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
}
//code2
//./src/dao/EnameAndDnameDao.java
package dao;
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;
import dto.EnameAndDnameDto;
public class EnameAndDnameDao {
	private DataSource dataSource = null;
	public EnameAndDnameDao() {
		try {
			Context context = new InitialContext();
			dataSource = (DataSource)context.lookup("java:comp/env/jdbc/oracle");
		} catch(Exception e) {
			e.printStackTrace();
		};
	}
	public List<EnameAndDnameDto> edList() {
		List<EnameAndDnameDto> list = new ArrayList<EnameAndDnameDto>();
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String query = "select emp.ename, dept.dname from emp, dept where emp.deptno = dept.deptno";
		System.out.println(query);
		try {
			connection = dataSource.getConnection();
			pstmt = connection.prepareStatement(query);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				list.add(new EnameAndDnameDto(
					rs.getString("ename"),
					rs.getString("dname")
				));
			};
			System.out.println("SUCESS");
		} catch(Exception e) {
			System.out.println("FAIL");
			e.printStackTrace();
		};
		return list;
	}
}
//code3
//./src/emp/EmpCommand.java
package emp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface EmpCommand {
	void execute(HttpServletRequest request, HttpServletResponse response);
}
//code4
//./src/emp/EmpListCommand.java
package emp;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*; 
import java.io.*;
import dao.*;
import dto.*;
public class EmpListCommand implements EmpCommand {
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		EnameAndDnameDao edDao = new EnameAndDnameDao();
		List<EnameAndDnameDto> dtos = edDao.edList();
		request.setAttribute("dtos", dtos);
		List<String> results = new ArrayList<String>();
		File[] files = new File("C:/Users/601-26/Documents/workspace-sts-3.9.11.RELEASE/jsp_jk_el/WebContent/images/home").listFiles();
		for (File file : files) {
		    if (file.isFile()) {
		        results.add(file.getName());
		    };
		};
		Collections.shuffle(results);
		Iterator<String> it = results.iterator();
		request.setAttribute("it2", it);
	}
}
//code5
//./src/emp/EmpController.java
package emp;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/emp/*")
public class EmpController extends HttpServlet {
    public EmpController() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doGet() ..");
		actionDo(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doPost() ..");
		actionDo(request, response);
	}
	protected void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("actionDo() ..");
		request.setCharacterEncoding("UTF-8");
		String viewPage = null;
		EmpCommand command = null;
		String uri = request.getRequestURI();
		String conPath = request.getContextPath();
		String commandDo = uri.substring(conPath.length());
		System.out.println("uri: " + uri);
		System.out.println("conPath: " + conPath);
		System.out.println("commandDo: " + commandDo);
		System.out.println(commandDo);		
		switch (commandDo) {
			case "/emp/list.do": { 
				System.out.println("Here is /emp/list.do");
				System.out.println("--------------");
				command = new EmpListCommand();
				command.execute(request, response);
				viewPage = "/list.jsp";
				break;
			}
			default: {
				System.out.println("I dont know there. Pardon?");
			}
		};
		RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
		dispatcher.forward(request, response);		
	}
}
<!-- code6 -->
<!-- ./WebContent/list.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="java.util.*" import="java.io.*"
	import="dao.EnameAndDnameDao" import="dto.EnameAndDnameDto"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>list.jsp</title>
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/font-awesome.min.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/prettyPhoto.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/price-range.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/animate.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/main.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/responsive.css" rel="stylesheet">
<link rel="shortcut icon" href="images/ico/favicon.ico">
<link rel="apple-touch-icon-precomposed" sizes="144x144"
	href="${pageContext.request.contextPath}/images/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114"
	href="${pageContext.request.contextPath}/images/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72"
	href="${pageContext.request.contextPath}/images/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed"
	href="${pageContext.request.contextPath}/images/ico/apple-touch-icon-57-precomposed.png">
</head>
<body>
	<header></header>
	<section>
		<div class="container">
			<div class="row">
				<div class="col-sm-9 padding-right">
					<div class="features_items">
						<!--features_items-->
						<h2 class="title text-center">EMP LIST</h2>
						<c:forEach var="dto" items="${dtos}">
							<div class="col-sm-4">
								<div class="product-image-wrapper">
									<div class="single-products">
										<div class="productinfo text-center">
											<img src="${pageContext.request.contextPath}/images/home/${it2.next()}" alt="" />
											<h2>${dto.getDname()}</h2>
											<p>${dto.getEname()}</p>
										</div>
										<div class="product-overlay">
											<div class="overlay-content">
												<h2>${dto.getDname()}</h2>
												<p>${dto.getEname()}</p>
											</div>
										</div>
									</div>
								</div>
							</div>
						</c:forEach>
					</div>
					<!--features_items-->
				</div>
			</div>
		</div>
	</section>
	<footer></footer>
	<script src="${pageContext.request.contextPath}/js/jquery.js"></script>
	<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
	<script src="${pageContext.request.contextPath}/js/jquery.scrollUp.min.js"></script>
	<script src="${pageContext.request.contextPath}/js/price-range.js"></script>
	<script src="${pageContext.request.contextPath}/js/jquery.prettyPhoto.js"></script>
	<script src="${pageContext.request.contextPath}/js/main.js"></script>
</body>
</html>
profile
Brave but clumsy

0개의 댓글