web 정리
1. DB(모델링)
2. VO
package model;
public class item {
private int itemNumber;//item_id
private String name;//item_name
private int price;
private String description;
private String url;//picture_url
private int count;
public item() { }
public item(int itemNumber) {this.itemNumber = itemNumber;}
public item(int itemNumber, String name, int price, String description, String url, int count) {
this.itemNumber = itemNumber;
this.name = name;
this.price = price;
this.description = description;
this.url = url;
this.count = count;
}
public item(int itemNumber, String name, int price, String description, String url) {
this.itemNumber = itemNumber;
this.name = name;
this.price = price;
this.description = description;
this.url = url;
}
public int getItemNumber() {
return itemNumber;
}
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
return "item [itemNumber=" + itemNumber + ", name=" + name + ", price=" + price + ", description=" + description
+ ", url=" + url + ", count=" + count + "]";
}
}
3
1) DAO-datasourceManager
DAO에서
DataSourceManager: naming service(JNDI API)를 이용해서 datasource를 얻어 오는 것만 합니다.
그래서 분리해서 이 역할만 하는 class를 만듭니다.
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
username = "root"
password= "1234"
driverClassName= "com.mysql.cj.jdbc.Driver"
factory = "org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory"
url="jdbc:mysql://localhost:3306/shopdb?serverTimezone=Asia/Seoul"
maxActive="500"
maxIdle="30"
/>
</Context>
package util;
/*
* JNDI API를 이용해서 dataSource를 리턴받아 온다.
* 싱글톤으로
*/
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DataSourceManager {
private DataSource ds;
private static DataSourceManager instance = new DataSourceManager();
private DataSourceManager() {
//initialContext 객체의 lookup()을 이용해서 ds하나 받아 오는 로직...
try {
// Naming Service(JNDI API)...javax.naming.Context
InitialContext ic = new InitialContext();
ds = (DataSource) ic.lookup("java:comp/env/jdbc/mysql");
System.out.println("DataSource Lookup 성공...");
} catch (NamingException e) {
System.out.println("DataSource Lookup 실패...");
}
}
public static DataSourceManager getInstance() {
return instance;
}
public DataSource getConnection() {
return ds;
}
}
ui만들고, 서버 쪽을 만듭니다.
CBD(component-based development)
package controller;
import java.io.IOException;
import java.sql.SQLException;
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("*.do")
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doProcesss(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doProcesss(request, response);
}
protected void doProcesss(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestURI=request.getRequestURI();
System.out.println("requestURI:: "+requestURI);
String contextPath= request.getContextPath();
System.out.println("contextPath::"+contextPath);
String command=requestURI.substring(contextPath.length()+1);
System.out.println("command::"+command);
Controller controller = HandlerMapping.getInstance().createContoller(command);
String path="index.jsp";
ModelAndView mv=null;
try {
mv=controller.handle(request, response);
path = mv.getPath();
}catch(Exception e) {
System.out.println(e);
}
if(mv!=null) {
if(mv.isRedirect()) {
response.sendRedirect(path);
}else {
request.getRequestDispatcher(path).forward(request, response);
}
}
}
}
package controller;
import controller.ItemListController;
public class HandlerMapping {
private static HandlerMapping handler = new HandlerMapping();
private HandlerMapping() {
}
public static HandlerMapping getInstance() {
return handler;
}
public Controller createContoller(String command) {
Controller controller = null;
if (command.equals("itemList.do")) {
controller = new ItemListController();
System.out.println("ItemListController 생성");
}
return controller;
}
}
package controller;
public class ModelAndView {
private String path;
private boolean isRedirect;
public ModelAndView() {
super();
}
public ModelAndView(String path) {
super();
this.path = path;
}
public ModelAndView(String path, boolean isRedirect) {
super();
this.path = path;
this.isRedirect = isRedirect;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isRedirect() {
return isRedirect;
}
public void setRedirect(boolean isRedirect) {
this.isRedirect = isRedirect;
}
@Override
public String toString() {
return "ModelAndView [path=" + path + ", isRedirect=" + isRedirect + "]";
}
}
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Controller {
ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
package controller;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Item;
import model.ItemDAO;
public class ItemListController implements Controller {
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
String path = "index.jsp";
try {
ArrayList<Item> list = ItemDAO.getInstance().getAllItem();
System.out.println("실행");
request.setAttribute("list", list);
path = "itemList.jsp";
} catch (SQLException e) {
System.out.println(e);
}
return new ModelAndView(path);
}
}
<%@ 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>
<h2 align="center">==========Fruits All item==========</h2>
<a href="itemList.do"><h3 align="center">Show All Fruit Item Using Factory Pattern</h3></a>
</body>
</html>
<%@ 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>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 align="center">Fruit Total List 1</h1>
<table align = "center">
<tr>
<c:forEach items="${list}" var = "item">
<td>
<img src="${item.url}" width=150 height=150 border="2"><br>
상품명 : ${item.name}<br>
가격 : ${item.price}원
</td>
</c:forEach>
</tr>
</table>
</body>
</html>