프레젠테이션, 추가, 삭제 기능 표시

85025 단어 jsp

뉴스 쇼


welcome.jsp 페이지에서 뉴스 목록 보이기 (부분 코드)
<!--        -->
<table>
<tr>
	<td>    </td><td>    </td><td>    </td><td>    </td><td>    </td><td>    </td><td>    </td><td>  |  </td>
</tr>
<%
//	      (    )
List<Commodity> cList = (List<Commodity>)session.getAttribute("cList");
if(cList != null && cList.size() > 0){
	for(Commodity c:cList){
%>
	<tr>
		<td><%=c.getC_id() %></td><td><%=c.getC_name() %></td><td><%=c.getC_madein() %></td><td><%=c.getCt().getCt_name() %></td><td><%=c.getC_inprice() %></td><td><%=c.getC_outprice() %></td><td><%=c.getC_num() %></td><td>  |  </td>
	</tr>
<%
	}
}
%>
</table>

commoditytypeDao 인터페이스의 추상적인 방법은 상품 종류 번호에 따라 상품 종류 유형을 되돌려 상품표에 사용하도록 한다
public interface CommoditytypeDao {
	Commoditytype getCommoditytypeById(String ct_id);
}

commoditytypeDaoImpl 구현 클래스, 구현 인터페이스 방법
@Override
	public Commoditytype getCommoditytypeById(String ct_id) {
		Commoditytype ct = null;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype where ct_id=?";
			List param = new ArrayList();
			param.add(ct_id);
			ResultSet rs = DBHelper.executeQuery(conn, sql, param);
			rs.next();
			ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return ct;
	}

CommoditytypeController 제어층 정보 전달
public class CommoditytypeController {
	private CommoditytypeDao commoditytypeDao = null;
	public CommoditytypeController() {
		commoditytypeDao = new CommoditytypeDaoImpl();
	}
}

Commodity Dao 인터페이스에서 메소드를 추가하여 모든 상품 목록의 정보를 집합합니다.
public interface CommodityDao {
	List<Commodity> getCommodityList();
}

Commodity DaoImpl 클래스 구현 인터페이스 접근 방법
@Override
	public List<Commodity> getCommodityList() {
		List<Commodity> commoditys = new ArrayList<Commodity>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commodity";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commodity c = new Commodity();
				c.setC_id(rs.getString("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setC_madein(rs.getString("c_madein"));
				c.setC_type(rs.getString("c_type"));
				c.setC_inprice(rs.getInt("c_inprice"));
				c.setC_outprice(rs.getInt("c_outprice"));
				c.setC_num(rs.getInt("c_num"));
				c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getString("c_type")));
				commoditys.add(c);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return commoditys;
	}

Commodity Controller 제어층
public class CommodityController {
	private CommodityDao commodityDao = null;
	public CommodityController() {
		commodityDao = new CommodityDaoImpl();
	}
	public List<Commodity> getCommodityList() {
		return commodityDao.getCommodityList();
	}
}

LoginServlet에서session으로 저장하고welcome에서 읽고 표시
List<Commodity> cList = new CommodityController().getCommodityList();
session.setAttribute("cList", cList);

추가, 제거 기능


CommoditytypeDao 인터페이스
public interface CommoditytypeDao {
	Commoditytype getCommoditytypeById(String ct_id);
	//               
	List<Commoditytype> getCommoditytypeList();
}

CommoditytypeDaoImpl 구현 클래스
public class CommoditytypeDaoImpl implements CommoditytypeDao{

	@Override
	public Commoditytype getCommoditytypeById(String ct_id) {
		Commoditytype ct = null;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype where ct_id=?";
			List param = new ArrayList();
			param.add(ct_id);
			ResultSet rs = DBHelper.executeQuery(conn, sql, param);
			rs.next();
			ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return ct;
	}

	@Override
	public List<Commoditytype> getCommoditytypeList() {
		List<Commoditytype> cts = new ArrayList<>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commoditytype";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commoditytype ct = new Commoditytype(rs.getString("ct_id"),rs.getString("ct_name"));
				cts.add(ct);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return cts;
	}
}

CommoditytypeController 제어층
public class CommoditytypeController {
	private CommoditytypeDao commoditytypeDao = null;
	public CommoditytypeController() {
		commoditytypeDao = new CommoditytypeDaoImpl();
	}
	
	public List<Commoditytype> getCommoditytypeList(){
		return commoditytypeDao.getCommoditytypeList();
	}
}

Commodity Dao 인터페이스
public interface CommodityDao {
	List<Commodity> getCommodityList();
	int istCommodity(Commodity c);
	int delCommodityById(String c_id);
}

Commodity DaoImpl 구현 클래스
public class CommodityDaoImpl implements CommodityDao {

	@Override
	public List<Commodity> getCommodityList() {
		List<Commodity> commoditys = new ArrayList<Commodity>();
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "select * from commodity";
			ResultSet rs = DBHelper.executeQuery(conn, sql, null);
			while(rs.next()) {
				Commodity c = new Commodity();
				c.setC_id(rs.getString("c_id"));
				c.setC_name(rs.getString("c_name"));
				c.setC_madein(rs.getString("c_madein"));
				c.setC_type(rs.getString("c_type"));
				c.setC_inprice(rs.getInt("c_inprice"));
				c.setC_outprice(rs.getInt("c_outprice"));
				c.setC_num(rs.getInt("c_num"));
				c.setCt(new CommoditytypeDaoImpl().getCommoditytypeById(rs.getString("c_type")));
				commoditys.add(c);
			}
			DBHelper.closeConnection(conn);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return commoditys;
	}

	@Override
	public int istCommodity(Commodity c) {
		int line = 0;
		try {
			Connection conn = DBHelper.getConnection();
			String sql = "insert into commodity(c_id,c_name,c_madein,c_type,c_inprice,c_outprice,c_num) values (?,?,?,?,?,?,?)";
			List param = new ArrayList();
			param.add(c.getC_id());
			param.add(c.getC_name());
			param.add(c.getC_madein());
			param.add(c.getC_type());
			param.add(c.getC_inprice());
			param.add(c.getC_outprice());
			param.add(c.getC_num());
			
			line = DBHelper.executeUpdate(conn, sql, param);
			
			DBHelper.closeConnection(conn);
		}catch(Exception  e) {
			e.printStackTrace();
		}
		return line;
	}

	@Override
	public int delCommodityById(String c_id) {
		int line = 0;
		Connection conn = DBHelper.getConnection();
		String sql = "delete from commodity where c_id=?";
		List param = new ArrayList();
		param.add(c_id);
		line = DBHelper.executeUpdate(conn, sql, param);
		DBHelper.closeConnection(conn);
		return line;
	}
	
}

Commodity Controller 제어층
public class CommodityController {
	private CommodityDao commodityDao = null;
	public CommodityController() {
		commodityDao = new CommodityDaoImpl();
	}
	
	public List<Commodity> getCommodityList() {
		return commodityDao.getCommodityList();
	}
	
	public int istCommodity(String c_name,String c_madein,String c_type,Integer c_inprice,Integer c_outprice,Integer c_num) {
		Commodity c = new Commodity(System.currentTimeMillis()+"",c_name,c_madein,c_type,c_inprice,c_outprice,c_num);
		return commodityDao.istCommodity(c);
	}
	
	public int delCommodityById(String c_id) {
		return commodityDao.delCommodityById(c_id);
	}
}

welcome.jsp 페이지 (부분 코드)
<form method="post" action="/web06/istCommodityServlet">
<table style="border: 1px solid black; margin:0 auto;">
<td colspan="1" style="width:130px;" >    :<input type="text" name="c_name" /></td>
<td style="width:150px;" >    :<input type="text" name="c_madein" /></td>
<td style="width:130px;" >    :
	<select name="c_type">
		<%
		for(Commoditytype ct:ctList){
		%>
		<option value=""><%=ct.getCt_name() %></option>
		<%
		}
		%>
	</select>
</td>
<td style="width:130px;" >    :<input type="text" name="c_inprice" /></td>
<td style="width:130px;" >    :<input type="text" name="c_outprice" /></td>
<td style="width:130px;" >    :<input type="text" name="c_num" /></td>
<td style="width:130px;" ><input type="submit" name="  " /></td>

<!--        -->
<tr>
	<td>    </td><td>    </td><td>    </td><td>    </td><td>    </td><td>    </td><td>    </td><td>  |  </td>
</tr>
<%
if(cList != null && cList.size() > 0){
	for(Commodity c:cList){
%>
	<tr>
		<td><%=c.getC_id() %></td><td><%=c.getC_name() %></td><td><%=c.getC_madein() %></td><td><%=c.getCt().getCt_name() %></td><td><%=c.getC_inprice() %></td><td><%=c.getC_outprice() %></td><td><%=c.getC_num() %></td><td><a href="/web06/toupdateCommodityByid?c_id=">  </a>|<a href="/web06/deleteCommodityById?c_id=">  </a></td>
	</tr>
<%
	}
}
%>
</table>
</form>

Servlet(ist\del)
public class IstCommodityServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String c_name = req.getParameter("c_name");
		String c_madein = req.getParameter("c_madein");
		String c_type = req.getParameter("c_type");
		Integer c_inprice = new Integer(req.getParameter("c_inprice"));
		Integer c_outprice = new Integer(req.getParameter("c_outprice"));
		Integer c_num = new Integer(req.getParameter("c_num"));
		
		int line = new CommodityController().istCommodity(c_name,c_madein,c_type,c_inprice,c_outprice,c_num);
		if(line>0) {
			HttpSession session = req.getSession();
			
			List<Commodity> cList = new CommodityController().getCommodityList();
			session.setAttribute("cList", cList);
			
			List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
			session.setAttribute("ctList", ctList);
			
			resp.sendRedirect("/web06/welcome.jsp");
		}else {
			
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
public class DeleteCommodityById extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		String c_id = req.getParameter("c_id");
		
		int line = new CommodityController().delCommodityById(c_id);
		if(line>0) {
			HttpSession session = req.getSession();
			
			List<Commodity> cList = new CommodityController().getCommodityList();
			session.setAttribute("cList", cList);
			
			List<Commoditytype> ctList = new CommoditytypeController().getCommoditytypeList();
			session.setAttribute("ctList", ctList);
			
			resp.sendRedirect("/web06/welcome.jsp");
		}else {
			
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

좋은 웹페이지 즐겨찾기