'회원관리 웹 어플리케이션 2 ' 포스팅 에서 이어지는 포스팅으로,
장바구니 기능을 하는 웹 어플리케이션 프로그램이다.
[ 구성이 달라지는 점 ]
1. layout.jsp를 한 가지로 합침
1) login 시 - [ ]님 로그인 / 로그아웃 링크
2) login 아닐 시 - 로그인 폼
2. home의 main 화면을 켜자마자 item 목록 리스트 보여주기
- home.jsp 수정 + HomeController에 item 목록 리스트 보여주는 기능 추가하기
[ 프로그램 진행 원리 및 개요 ]
웹 페이지에서 동적으로 이동되는 부분(메인 화면)만 새로 업데이트하여 보여주기 위하여,
전체적인 layout을 잡아두고, 메인 화면에 기능에 따른 새로운 view(jsp)를 할당하여 동작하도록 한다.
기능 1. layout 화면을 로그인 + 비로그인으로 합쳐서 제공 (session을 통해 다른 화면 제공)
기능 2 : 로그인 + 로그아웃
기능 3 : 로그인 상태에서, 상품 등록 기능
[ 구현 코드 ]
Model
> Item
/ItemDTO.java
package org.kosta.model;
public class ItemDTO {
private String itemNo;
private String name;
private String maker;
private int price;
private String detail;
public ItemDTO() {
super();
}
public ItemDTO(String itemNo, String name, String maker, int price, String detail) {
super();
this.itemNo = itemNo;
this.name = name;
this.maker = maker;
this.price = price;
this.detail = detail;
}
public String getItemNo() {
return itemNo;
}
public void setItemNo(String itemNo) {
this.itemNo = itemNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMaker() {
return maker;
}
public void setMaker(String maker) {
this.maker = maker;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
@Override
public String toString() {
return "ItemDTO [itemNo=" + itemNo + ", name=" + name + ", maker=" + maker + ", price=" + price + ", detail="
+ detail + "]";
}
}
/ItemDAO.java
package org.kosta.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.sql.DataSource;
public class ItemDAO {
private static ItemDAO instance=new ItemDAO();
private DataSource dataSource;
private ItemDAO() {
this.dataSource=DataSourceManager.getInstance().getDataSource();
}
public static ItemDAO getInstance() {
return instance;
}
public void closeAll(PreparedStatement pstmt,Connection con) throws SQLException {
if(pstmt!=null)
pstmt.close();
if(con!=null)
con.close();
}
public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection con) throws SQLException {
if(rs!=null)
rs.close();
closeAll(pstmt, con);
}
/* 1번 기능
* getAllItemList() : ArrayList<ItemDTO>
*/
public ArrayList<ItemDTO> getAllItemList() throws SQLException{
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
ArrayList<ItemDTO> list=new ArrayList<ItemDTO>();
try {
con=dataSource.getConnection();
String sql="select item_no,name,maker from item order by item_no desc";
pstmt=con.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()) {
ItemDTO dto=new ItemDTO();
dto.setItemNo(rs.getString(1));
dto.setName(rs.getString(2));
dto.setMaker(rs.getString(3));
list.add(dto);
}
}finally {
closeAll(rs, pstmt, con);
}
return list;
}
public ItemDTO findItemByNO(String itemNo) {
return null;
}
/* 2번 기능
* findItemByNo(String itemNo) : ItemDTO
*/
public ItemDTO findItemByNo(String itemNo) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ItemDTO dto = null;
try {
con = dataSource.getConnection();
String sql = "SELECT name, maker, price, detail "
+ "FROM item WHERE item_no = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, itemNo);
rs = pstmt.executeQuery();
if (rs.next())
dto = new ItemDTO(itemNo, rs.getString(1), rs.getString(2),
rs.getInt(3), rs.getString(4));
} finally {
closeAll(rs, pstmt, con);
}
return dto;
}
/* 3번 기능
* registerItem(ItemDTO) : void
*/
public void registerItem(ItemDTO dto) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = dataSource.getConnection();
String sql = "INSERT INTO item(item_no,name,maker,price,detail) "
+ "VALUES(item_seq.nextval, ?, ?, ?, ?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, dto.getName());
pstmt.setString(2, dto.getMaker());
pstmt.setInt(3,dto.getPrice());
pstmt.setString(4, dto.getDetail());
pstmt.executeUpdate();
} finally {
closeAll(pstmt, con);
}
}
}//class
> Member
/MemberDTO.java
package org.kosta.model;
public class MemberDTO {
private String id;
private String password;
private String name;
private String address;
public MemberDTO() {
super();
// TODO Auto-generated constructor stub
}
public MemberDTO(String id, String password, String name, String address) {
super();
this.id = id;
this.password = password;
this.name = name;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "MemberVO [id=" + id + ", password=" + password + ", name=" + name + ", address=" + address + "]";
}
}
/MemberDAO.java
package org.kosta.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.sql.DataSource;
public class MemberDAO {
private static MemberDAO instance=new MemberDAO();
private DataSource dataSource;//DBCP
private MemberDAO() {
this.dataSource=DataSourceManager.getInstance().getDataSource();
}
public static MemberDAO getInstance() {
return instance;
}
public void closeAll(PreparedStatement pstmt,Connection con) throws SQLException {
if(pstmt!=null)
pstmt.close();
if(con!=null)
con.close();
}
public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection con) throws SQLException {
if(rs!=null)
rs.close();
closeAll(pstmt, con);
}
public MemberDTO findMemberById(String id) throws SQLException {
MemberDTO vo=null;
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
con=dataSource.getConnection();
String sql="select name,address from web_member where id=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
rs=pstmt.executeQuery();
if(rs.next())
vo=new MemberDTO(id,null,rs.getString(1),rs.getString(2));
}finally {
closeAll(rs, pstmt, con);
}
return vo;
}
public ArrayList<String> getAddressKind() throws SQLException{
ArrayList<String> list=new ArrayList<String>();
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
con=dataSource.getConnection();
String sql="select distinct(address) from web_member";
pstmt=con.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next())
list.add(rs.getString(1));
}finally {
closeAll(rs, pstmt, con);
}
return list;
}
public int getMemberTotalCount() throws SQLException {
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
int count=0;
try {
con=dataSource.getConnection();
String sql="select count(*) from web_member";
pstmt=con.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next())
count=rs.getInt(1);
}finally {
closeAll(rs, pstmt, con);
}
return count;
}
public ArrayList<MemberDTO> findMemberListByAddres(String address) throws SQLException{
ArrayList<MemberDTO> list=new ArrayList<MemberDTO>();
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
con=dataSource.getConnection();
String sql="select id,name from web_member where address=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, address);
rs=pstmt.executeQuery();
while(rs.next())
list.add(new MemberDTO(rs.getString(1),null,rs.getString(2),null));
}finally {
closeAll(rs, pstmt, con);
}
return list;
}
public MemberDTO login(String id,String password) throws SQLException {
MemberDTO vo=null;
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
con=dataSource.getConnection();
String sql=
"select name,address from web_member where id=? and password=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, password);
rs=pstmt.executeQuery();
if(rs.next())
vo=new MemberDTO(id,password,rs.getString(1),rs.getString(2));
}finally {
closeAll(rs, pstmt, con);
}
return vo;
}
//회원가입
public void register(MemberDTO vo) throws SQLException {
Connection con=null;
PreparedStatement pstmt=null;
try {
con=dataSource.getConnection();
String sql="insert into web_member values(?,?,?,?)";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, vo.getId());
pstmt.setString(2, vo.getPassword());
pstmt.setString(3, vo.getName());
pstmt.setString(4, vo.getAddress());
pstmt.executeUpdate();
}finally {
closeAll(pstmt, con);
}
}
public void updateMember(MemberDTO vo) throws SQLException {
Connection con=null;
PreparedStatement pstmt=null;
try {
con=dataSource.getConnection();
String sql=
"update web_member set password=?,name=?,address=? where id=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, vo.getPassword());
pstmt.setString(2, vo.getName());
pstmt.setString(3, vo.getAddress());
pstmt.setString(4, vo.getId());
pstmt.executeUpdate();
}finally {
closeAll(pstmt, con);
}
}
}
View
[ template ]
/index.jsp
index를 실행하면 <jsp:forward> 방식으로 home.jsp로 간다
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:forward page="front">
<jsp:param value="home" name="command"/>
</jsp:forward>
<%--
index.jsp --command=home-- HomeController -- ItemDAO getAllItemList():ArrayList<ItemDTO>
|
| forward
item/item-list.jsp : 레이아웃 적용된 상태에서 메인화면에 아이템목록제공
--%>
/template/layout.jsp
로그인 로그아웃 기능이 여기서 실행 된다.
login 상태와 login 상태가 아닐 때를 다르게 제공한다
login 아닐 시 - 로그인 폼
login 시 - [ ]님 로그인 / 로그아웃 링크
<%@ 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>Home</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="${pageContext.request.contextPath}/css/myhome.css" />
</head>
<body>
<div class="container">
<%-- HEARDER --%>
<div class="row header">
<div class="col-sm-12">
<h3>
<a href="${pageContext.request.contextPath}/front?command=home">
Home logo</a>
</h3>
</div>
</div>
<%-- MAIN --%>
<div class="row main">
<%-- main 화면 --%>
<div class="col-sm-7">
<%--
메인 화면에 대한 view 정보(url or jsp파일명)를 컨트롤러에서
동적으로 할당받는다
--%>
<c:import url="${requestScope.url}" />
</div>
<%-- 오른쪽 화면 --%>
<div class="col-sm-5">
<%-- login 상태와 login 상태가 아닐 때를 다르게 제공한다
login 아닐 시 - 로그인 폼
login 시 - [ ]님 로그인 / 로그아웃 링크
--%>
<c:choose>
<%-- login X --%>
<c:when test="${sessionScope.memberDTO == null}">
<%--login Table --%>
<form action="${pageContext.request.contextPath}/front"
method="post">
<input type="hidden" name="command" value="login">
<table>
<tr>
<td>아이디</td>
<td><input type="text" name="id" required="required"></td>
</tr>
<tr>
<td>패스워드</td>
<td><input type="password" name="password"
required="required"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit"
value="로그인"></td>
</tr>
</table>
</form>
<br>
<br>
</c:when>
<%-- login O --%>
<c:otherwise>
${sessionScope.memberDTO.name} 님 로그인 <br>
<a href="${pageContext.request.contextPath}/front?command=logout">로그아웃</a><br><br>
<a href="${pageContext.request.contextPath}/front?command=registerItemForm">상품등록</a>
</c:otherwise>
</c:choose>
</div>
</div>
<%-- FOOTER --%>
<div class="row footer">
<div class="col-sm-12">
경기도 성남시 분당구 삼평동 대왕판교로 670길 유스페이스2 B동 8층 TEL. 031-606-9311~20 <br>
Copyright © 2020 KOSTA ALL RIGHTS RESERVED
</div>
</div>
</div>
</body>
</html>
[ CSS ]
/myhome.css
@charset "UTF-8";
.header{
text-align: center;
height: 70px;
padding: 15px;
}
.main{
text-align: center;
height: 400px;
}
.footer{
text-align: center;
height: 10px
}
th{
text-align: center;
}
.detailTable{
text-align: letf;
}
-- 기능 1: 상품 리스트 보기 + 상세 보기
/item/item-listjsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<h3> Item List </h3><br><br>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>번호</th><th>상품명</th><th>제조사</th>
</tr>
</thead>
<tbody>
<%-- ItemDetailController(command=ItemDetail)로 링크 --%>
<c:forEach items="${requestScope.list}" var="item">
<tr>
<td>${item.itemNo}</td>
<td><a href="${pageContext.request.contextPath}/front?command=ItemDetail&itemNo=${item.itemNo}">${item.name}</a></td>
<td>${item.maker}</td>
</tr>
</c:forEach>
</tbody>
</table>
/item/item-detail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="itemDTO" value="${requestScope.itemDTO}"></c:set>
<h3> ${itemDTO.name}의 Item Detail </h3><br><br>
<table class="table table-hover table-bordered detailTable">
<tr>
<td>NO</td>
<td>${itemDTO.itemNo }</td>
</tr>
<tr>
<td>Name</td>
<td>${itemDTO.name }</td>
</tr>
<tr>
<td>Maker</td>
<td>${itemDTO.maker }</td>
</tr>
<tr>
<td>Price</td>
<td>${itemDTO.price }</td>
</tr>
<tr>
<td>Detail</td>
<%-- pre tag --%>
<td><pre>${itemDTO.detail}</pre></td>
</tr>
</table>
-- 기능 2 : 로그인 기능
로그인 성공 시, 바로 layout으로 넘어가므로 jsp가 별달리 필요없음
/member/login-fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<script type="text/javascript">
alert("로그인 실패!");
location.href="front?command=home";
</script>
-- 기능 3 : 상품 등록 기능
/item/item-register-form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
RegisterItemController -- ItemDAO registerItem(dto)
ㅣ
ㅣ redirect
item/item-register-result.jsp
: alert() 상품등록 완료! 후
: home으로 이동
--%>
<h3>상품 등록</h3><br>
<form method="post" action="front">
<input type="hidden" name="command" value="RegisterItem">
<table class="table table-bordered detailTable">
<tr>
<td>name</td>
<td><input type="text" name="name" required="required"></td>
</tr>
<tr>
<td>maker</td>
<td><input type="text" name="maker" required="required"></td>
</tr>
<tr>
<td>price</td>
<td><input type="number" name="price" required="required"></td>
</tr>
<tr>
<td>detail</td>
<td><textarea rows="5" cols="30" name="detail" required="required"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="상품등록">
</td>
</tr>
</table>
</form>
/item/item-register-result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript">
alert("상품등록이 완료 되었습니다!");
href.location = "${pageContext.request.contextPath}/front?command=home";
</script>
Controller
/Controller.java
package org.kosta.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Controller {
public String execute(HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
/ HandlerMapping.java
package org.kosta.controller;
public class HandlerMapping {
private static HandlerMapping instance=new HandlerMapping();
private HandlerMapping() {}
public static HandlerMapping getInstance() {
return instance;
}
public Controller create(String command) {
Controller controller=null;
if(command.contentEquals("home")) {
controller=new HomeController();
}
else if(command.contentEquals("ItemDetail")) {
controller=new ItemDetailController();
}
else if(command.contentEquals("login")) {
controller=new LoginController();
}
else if(command.contentEquals("logout")) {
controller=new LogoutController();
}
else if(command.contentEquals("registerItemForm")) {
controller=new RegisterItemFormController();
}
else if(command.contentEquals("RegisterItem")) {
controller=new RegisterItemController();
}
else if(command.contentEquals("RegisterItemResult")) {
controller=new RegisterItemResultController();
}
return controller;
}
}
/DispatcherServlet.java
handleRequest method 작동 방식
1. 에러(예외) 발생 시, 콘솔에 메세지 발생 경로 출력 후, error.jsp로 redirect 방식으로 view로 이동한다.
2. 클라이언트가 전송한 command 정보를 받는다.
3. HandlerMapping 을 이용해 개별 컨트롤러 객체를 컨트롤러 인터페이스 타입으로 반환받는다.
4. 개별 컨트롤러를 실행한다.
5. 실행 후 반환된 String url 정보를 이용해,
forward 방식 or redirect 방식으로 view로 이동해 응답하게 한다.
package org.kosta.controller;
import java.io.IOException;
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("/front")
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.handleRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
this.handleRequest(request, response);
}
protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String command=request.getParameter("command");
Controller controller=HandlerMapping.getInstance().create(command);
String url=controller.execute(request, response);
if(url.startsWith("redirect:"))
response.sendRedirect(url.substring(9));
else
request.getRequestDispatcher(url).forward(request, response);
}catch (Exception e) {
e.printStackTrace(); // 예외 메세지와 발생 경로를 모두 콘솔에 출력한다
response.sendRedirect("error.jsp");
}
}
}
--- 기능 1 : index.jsp에서 실행하면, layout으로 이동하는 Controller
(request.setAttribute를 통해 url에 item-list.jsp를 할당하여, 상품 리스트 정보를 메인화면에 할당)
/HomeController.java
package org.kosta.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.kosta.model.ItemDAO;
public class HomeController implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
String url=null;
request.setAttribute("list", ItemDAO.getInstance().getAllItemList());
//layout 적용되면서 item/item-list.jsp가 메인 화면으로 제공되도록 한다
request.setAttribute("url", "/item/item-list.jsp");
url="/template/layout.jsp";
return url;
}
}
/ItemDetailController.java
package org.kosta.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.kosta.model.ItemDAO;
import org.kosta.model.ItemDTO;
public class ItemDetailController implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String itemNo = request.getParameter("itemNo");
ItemDTO dto = ItemDAO.getInstance().findItemByNo(itemNo);
request.setAttribute("itemDTO", dto);
request.setAttribute("url", "/item/item-detail.jsp");
return "/template/layout.jsp";
}
}
--- 기능 2 : 로그인 + 로그아웃 기능 Controller
/LoginController.java
package org.kosta.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.kosta.model.MemberDAO;
import org.kosta.model.MemberDTO;
public class LoginController implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String id = request.getParameter("id");
String password = request.getParameter("password");
MemberDTO dto = MemberDAO.getInstance().login(id, password);
if(dto == null) {
return "/member/login-fail.jsp";
} else {
HttpSession session=request.getSession();
session.setAttribute("memberDTO", dto);
//request.setAttribute("url", "/member/login-ok.jsp");
//return "/template/login-layout.jsp";
return "redirect:front?command=home";
}
}
}
/LogoutController.java
package org.kosta.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LogoutController implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session=request.getSession(false);
if(session!=null)
session.invalidate();
return "front?command=home";
}
}
-- 기능 3 : 상품 등록 Controller
/RegisterItemFormController.java
package org.kosta.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterItemFormController implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("url", "/item/item-register-form.jsp");
return "/template/layout.jsp";
}
}
/RegisterItemController.java
package org.kosta.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.kosta.model.ItemDAO;
import org.kosta.model.ItemDTO;
public class RegisterItemController implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String name = request.getParameter("name");
String maker = request.getParameter("maker");
int price= Integer.parseInt(request.getParameter("price"));
String detail= request.getParameter("detail");
ItemDTO regiItemDTO = new ItemDTO(null, name, maker, price, detail);
ItemDAO.getInstance().registerItem(regiItemDTO);
return "redirect:front?command=RegisterItemResult";
}
}
/RegisterItemResultController.java
package org.kosta.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterItemFormController implements Controller {
@Override
public String execute(HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("url", "/item/item-register-form.jsp");
return "/template/layout.jsp";
}
}
[ 브라우저 화면 ]
> Home (index.jsp -> layout.jsp)
> 2-1. 로그인 성공 시,
> 2-2. 로그인 실패 시 -> index.jsp
> 3-1. 상품 등록 폼
> 3-2. 상품 등록 결과
'Java Web Programming > 4. JSP' 카테고리의 다른 글
[JSP/MVC] 커뮤니티 게시판 웹 어플리케이션 - 게시글 관리 (Model2 Pattern) (0) | 2020.10.02 |
---|---|
[JSP] 장바구니 웹 어플리케이션 (세션 이용) (0) | 2020.09.23 |
[JSP] 회원관리 웹 어플리케이션2 - layout 두 가지 (로그인 + 회원가입 + 회원정보 수정 + 경로 + EL/JSTL + BootStrap + Model2 설계) (0) | 2020.09.21 |
[JSP] 회원 관리 웹 어플리케이션 (EL/JSTL 적용, 웹 프로그램 경로, BootStrap + Model2 설계) (0) | 2020.09.18 |
[JSP] EL / JSTL 표현식 한 방에 정리 ! (문법과 사용방법) (3) | 2020.09.16 |