1. 상품 상세페이지 (Detail.java) 에서 수정
(1) (list.jsp)상품목록에서 상품 이름을 누르면 (링크) 상품 번호를 가지고 상세페이지에 가도록 함. (Detail.java)
<a href = "/webApp2/product/detail?num=${vo.num}">${vo.name}</a>
(2) doGet() -- url로 보낸 제품 번호로 Service에서 검색해서 결과값을 담아 뷰페이지로 보냄
(3) detail.jsp -- 결과값을 표에 담아 보여줌. 값을 변경하고 submit
(4) doPost() -- 변경한 값으로 Service 수정 메소드를 실행하고 그 결과값을 반영한 상품목록(List.java)으로 리프레시.
Detail.java
@WebServlet("/product/detail")
public class Detail extends HttpServlet {
private static final long serialVersionUID = 1L;
public Detail() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int num = Integer.parseInt(request.getParameter("num")); // url 주소로 보낸 num값 읽어오기
ProductService service = new ProductService();
ProductVo vo = service.getByNum(num); //번호로 검색
request.setAttribute("vo", vo); // 검색한 결과값 담기 (jsp에서 쓸 수 있게)
RequestDispatcher dis = request.getRequestDispatcher("/product/detail.jsp");
dis.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int num = Integer.parseInt(request.getParameter("num"));
int price = Integer.parseInt(request.getParameter("price"));
int amount = Integer.parseInt(request.getParameter("amount"));
ProductService service = new ProductService();
service.editProduct(new ProductVo (num, "", price, amount));
response.sendRedirect("/webApp2/product/list");
}
}
detail.jsp
<%@ 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>
<h3>상세 페이지</h3>
<form action="/webApp2/product/detail" method="post">
<table border="1">
<tr><th>제품번호</th>
<td><input type="number" value="${vo.num}" readonly name="num"></td>
</tr>
<tr><th>제품명</th>
<td><input type="text" value="${vo.name}" readonly></td>
</tr>
<tr><th>가격</th>
<td><input type="number" value="${vo.price}" name="price"></td>
</tr>
<tr><th>수량</th>
<td><input type="number" value="${vo.amount}" name="amount"></td>
</tr>
<tr><th>수정</th>
<td><input type="submit" value="수정"></td>
</tr>
</table>
</form>
</body>
</html>
2. 상품 삭제 (Delete.java)
(1) (list.jsp)상품 목록에 삭제 열을 만들고 삭제 버튼을 누르면 Delete.java로 넘어감
(2) doGet() --url 과 함께 전달된 num 값으로 Service에서 삭제 메소드 실행. 상품목록(List.java)으로 리프레시.
list.jsp
<%@ 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>
<h3>상품 목록</h3>
<a href="/webApp2/product/add">상품추가</a>
<br />
<a href="/webApp2/index.jsp">홈화면</a>
<br />
<table border="1">
<tr>
<th>제품번호</th>
<th>제품명</th>
<th>가격</th>
<th>수량</th>
<th>삭제</th>
</tr>
<c:forEach var="vo" items="${list }">
<tr>
<td>${vo.num}</td>
<td><a href="/webApp2/product/detail?num=${vo.num}">${vo.name}</a></td>
<td>${vo.price}</td>
<td>${vo.amount}</td>
<td><a href="/webApp2/product/delete?num=${vo.num}">
<input type="button" value="삭제" onclick="alert('삭제되었습니다.')">
</a></td>
</c:forEach>
</table>
</form>
</body>
</html>
Delete.java
@WebServlet("/product/delete")
public class Delete extends HttpServlet {
private static final long serialVersionUID = 1L;
public Delete() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int num = Integer.parseInt(request.getParameter("num"));
ProductService service = new ProductService();
service.delProduct(num);
response.sendRedirect("/webApp2/product/list");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3. 상품 검색
(1) list.jsp (상품목록)에 javascript로 상품명/검색명에 따라 입력 다르게 받음.
<head>
<script>
function a() {
let type = f.searchType.value;
let span = document.getElementById("searchSpan");
let html = "";
if (type == "1") {
html = "<input type = 'text' name = 'name'>";
f.action = "/webApp2/product/getbyname"; //form의 액션 속성에 값 넣어줌
} else {
html = "<input type = 'number' name = 'p1'> ~ ";
html += "<input type = 'number' name = 'p2'>"
f.action = "/webApp2/product/getbyprice";
}
span.innerHTML = html;
}
</script>
</head>
<body>
<form action="/webApp2/product/getbyname" method="post" name = "f">
<span> <select name="searchType" onchange = "a()">
<option value="1">상품명</option>
<option value="2">가격대</option>
</select>
</span> <span id = "searchSpan"> <input type = 'text' name = 'name'></span> <input type="submit" value="검색">
</form>
</body>
(*) ProductDao 에서 name 중복으로 받게 수정 ㅎㅎ
String sql = "select * from product where name like ? order by num";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%"+name+"%");
(2) 상품명 검색 -> GetByName.java 의 doPost() -> doGet() 실행
- 한글 깨지지 않게 인코딩
- 이름 읽어와서 Service 메소드로 검색 후 ArrayList에 담음
- 상품목록 list.jsp 뷰페이지로 넘김
@WebServlet("/product/getbyname")
public class GetByName extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
ProductService service = new ProductService();
ArrayList<ProductVo> list = service.getByName(name);
request.setAttribute("list", list);
RequestDispatcher dis = request.getRequestDispatcher("/product/list.jsp");
dis.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(3) 가격대 검색 -> GetByPrice.java 의 doPost() -> doGet() 실행
- 입력한 가격대 읽어와서 Service 메소드로 검색 후 ArrayList에 담음
- 상품목록 list.jsp 뷰페이지로 넘김
@WebServlet("/product/getbyprice")
public class GetByPrice extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int p1 = Integer.parseInt(request.getParameter("p1"));
int p2 = Integer.parseInt(request.getParameter("p2"));
ProductService service = new ProductService();
ArrayList<ProductVo> list = service.getByPrice(p1, p2);
request.setAttribute("list", list);
RequestDispatcher dis = request.getRequestDispatcher("/product/list.jsp");
dis.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
0. 총코드
ProductVo
package product;
public class ProductVo {
private int num;
private String name;
private int price;
private int amount;
public ProductVo() {}
public ProductVo(int num, String name, int price, int amount) {
super();
this.num = num;
this.name = name;
this.price = price;
this.amount = amount;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
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 int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Override
public String toString() {
return "ProductVo [num=" + num + ", name=" + name + ", price=" + price + ", amount=" + amount + "]";
}
}
ProductDao
package product;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import conn.DBConnect;
public class ProductDao {
private DBConnect dbconn;
public ProductDao() {
dbconn = DBConnect.getInstance();
}
// 상품 등록 -- void insert(ProductVo vo)
public void insert(ProductVo vo) {
Connection conn = dbconn.conn();
String sql = "insert into product values(seq_product.nextval, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, vo.getName());
pstmt.setInt(2, vo.getPrice());
pstmt.setInt(3, vo.getAmount());
int num = pstmt.executeUpdate();
System.out.println(num + "줄이 추가되었습니다.");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 번호로 검색 -- ProductVo selectByNum(int num)
public ProductVo selectByNum(int num) {
ArrayList<ProductVo> list = new ArrayList<>();
ProductVo vo = null;
Connection conn = dbconn.conn();
String sql = "select * from product where num = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, num);
ResultSet rs = pstmt.executeQuery(); // 검색결과 넣기
if (rs.next()) { // 검색결과가 있으면 실행
return new ProductVo(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null; // 검색된거 없으면 널값 리턴
}
// 제품명으로 검색 -- ArrayList<ProductVo> selectByName(String name)
public ArrayList<ProductVo> selectByName(String name) {
ArrayList<ProductVo> list = new ArrayList<>();
Connection conn = dbconn.conn();
String sql = "select * from product where name like ? order by num";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%"+name+"%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add(new ProductVo(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
// 가격으로 검색 -- ArrayList<ProductVo> selectByPrice(int price1, int price2)
public ArrayList<ProductVo> selectByPrice(int price1, int price2) {
ArrayList<ProductVo> list = new ArrayList<>();
Connection conn = dbconn.conn();
String sql = "select * from product where price between ? and ? order by num";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, price1);
pstmt.setInt(2, price2);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add(new ProductVo(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
// 전체목록 -- ArrayList<ProductVo> selectAll()
public ArrayList<ProductVo> selectAll() {
ArrayList<ProductVo> list = new ArrayList<>();
Connection conn = dbconn.conn();
String sql = "select * from product order by num";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add(new ProductVo(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
// 가격, 수량 수정 -- void update(ProductVo vo)
public void update(ProductVo vo) {
Connection conn = dbconn.conn();
String sql = "update product set price = ?, amount = ? where num = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, vo.getPrice());
pstmt.setInt(2, vo.getAmount());
pstmt.setInt(3, vo.getNum());
int num = pstmt.executeUpdate();
System.out.println(num + "줄이 수정되었습니다.");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 삭제 -- void delete(int num)
public void delete(int num) {
Connection conn = dbconn.conn();
String sql = "delete product where num = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, num);
int number = pstmt.executeUpdate();
System.out.println(number + "줄이 삭제되었습니다.");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
ProductService
package product;
import java.util.ArrayList;
public class ProductService {
private ProductDao dao;
public ProductService() {
dao = new ProductDao();
}
// 제품 추가
public void addProduct(ProductVo vo) {
dao.insert(vo);
}
// 번호로 검색
public ProductVo getByNum(int num) {
return dao.selectByNum(num);
}
// 제품명으로 검색
public ArrayList<ProductVo> getByName(String name) {
return dao.selectByName(name);
}
// 가격으로 검색
public ArrayList<ProductVo> getByPrice(int p1, int p2) {
return dao.selectByPrice(p1, p2);
}
// 전체 검색
public ArrayList<ProductVo> getAll() {
return dao.selectAll();
}
// 가격, 수량 수정
public void editProduct(ProductVo vo) {
dao.update(vo);
}
// 삭제
public void delProduct(int num) {
dao.delete(num);
}
}
Add.java
package product.controller;
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;
import product.ProductService;
import product.ProductVo;
/**
* Servlet implementation class Add
*/
@WebServlet("/product/add")
public class Add extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Add() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 상품 추가 폼
RequestDispatcher dis = request.getRequestDispatcher("/product/add.jsp");
dis.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 한글 안깨지게!
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 폼파라메터에 입력된 상품정보 읽어와서 dq에 저장
// request.getParameter 는 무조건 값을 String 으로 반환.
// Integer.parseInt() : int로 형변환
String name = request.getParameter("name");
int price = Integer.parseInt(request.getParameter("price"));
int amount = Integer.parseInt(request.getParameter("amount"));
ProductService service = new ProductService();
service.addProduct(new ProductVo(0, name, price, amount));
// 완료 후 List.java로 가기 -- 전체 목록 리프레시
// redirect를 써서 링크 타고 들어가면 get 방식으로 doGet() 호출
response.sendRedirect("/webApp2/product/list");
}
}
List.java
package product.controller;
import java.io.IOException;
import java.util.ArrayList;
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;
import product.ProductService;
import product.ProductVo;
/**
* Servlet implementation class All
*/
@WebServlet("/product/list")
public class List extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public List() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
// 전체 목록 보여주기는 1 단계만 필요함.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ProductService service = new ProductService();
//service로 기능 실행
ArrayList<ProductVo> list = service.getAll(); // 전체검색
// 결과값을 request에 담음
request.setAttribute("list", list);
// 뷰페이지로 이동
RequestDispatcher dis = request.getRequestDispatcher("/product/list.jsp");
dis.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Detail.java
package product.controller;
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;
import product.ProductService;
import product.ProductVo;
/**
* Servlet implementation class Detail
*/
@WebServlet("/product/detail")
public class Detail extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Detail() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int num = Integer.parseInt(request.getParameter("num")); // url 주소로 보낸 num값 읽어오기
ProductService service = new ProductService();
ProductVo vo = service.getByNum(num); //번호로 검색
request.setAttribute("vo", vo); // 검색한 결과값 담기 (jsp에서 쓸 수 있게)
RequestDispatcher dis = request.getRequestDispatcher("/product/detail.jsp");
dis.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int num = Integer.parseInt(request.getParameter("num"));
int price = Integer.parseInt(request.getParameter("price"));
int amount = Integer.parseInt(request.getParameter("amount"));
ProductService service = new ProductService();
service.editProduct(new ProductVo (num, "", price, amount));
response.sendRedirect("/webApp2/product/list");
}
}
Delete.java
package product.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;
import product.ProductService;
/**
* Servlet implementation class Delete
*/
@WebServlet("/product/delete")
public class Delete extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Delete() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int num = Integer.parseInt(request.getParameter("num"));
ProductService service = new ProductService();
service.delProduct(num);
response.sendRedirect("/webApp2/product/list");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
GetByName.java
package product.controller;
import java.io.IOException;
import java.util.ArrayList;
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;
import product.ProductService;
import product.ProductVo;
/**
* Servlet implementation class GetByName
*/
@WebServlet("/product/getbyname")
public class GetByName extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetByName() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
ProductService service = new ProductService();
ArrayList<ProductVo> list = service.getByName(name);
request.setAttribute("list", list);
RequestDispatcher dis = request.getRequestDispatcher("/product/list.jsp");
dis.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
GetByPrice
package product.controller;
import java.io.IOException;
import java.util.ArrayList;
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;
import product.ProductService;
import product.ProductVo;
/**
* Servlet implementation class GetByPrice
*/
@WebServlet("/product/getbyprice")
public class GetByPrice extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetByPrice() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int p1 = Integer.parseInt(request.getParameter("p1"));
int p2 = Integer.parseInt(request.getParameter("p2"));
ProductService service = new ProductService();
ArrayList<ProductVo> list = service.getByPrice(p1, p2);
request.setAttribute("list", list);
RequestDispatcher dis = request.getRequestDispatcher("/product/list.jsp");
dis.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
add.jsp
<%@ 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>
<h3>상품 입력</h3>
<form action = "/webApp2/product/add" method = "post">
<table border = "1">
<tr><th>제품명</th><td><input type = "text" name = "name"></td></tr>
<tr><th>가격</th><td><input type = "number" name = "price"></td></tr>
<tr><th>수량</th><td><input type = "number" name = "amount"></td></tr>
<tr><th>추가</th><td><input type = "submit" value = "add"></td></tr>
</table>
</form>
</body>
</html>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- page: jsp 설정 지시자 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function a() {
let type = f.searchType.value;
let span = document.getElementById("searchSpan");
let html = "";
if (type == "1") {
html = "<input type = 'text' name = 'name'>";
f.action = "/webApp2/product/getbyname"; //form의 액션 속성에 값 넣어줌
} else {
html = "<input type = 'number' name = 'p1'> ~ ";
html += "<input type = 'number' name = 'p2'>"
f.action = "/webApp2/product/getbyprice";
}
span.innerHTML = html;
}
</script>
</head>
<body>
<h3>상품 목록</h3>
<a href="/webApp2/product/add">상품추가</a>
<br />
<form action="/webApp2/product/getbyname" method="post" name = "f">
<span> <select name="searchType" onchange = "a()">
<option value="1">상품명</option>
<option value="2">가격대</option>
</select>
</span> <span id = "searchSpan"> <input type = 'text' name = 'name'></span> <input type="submit" value="검색">
</form>
<table border="1">
<tr>
<th>제품번호</th>
<th>제품명</th>
<th>가격</th>
<th>수량</th>
<th>삭제</th>
</tr>
<!-- for문 - 자동으로 list.size 만큼 루프돎. 자동으로 "vo"에 넣어줌. -->
<!-- items = "리스트 or 배열" -->
<!-- ProductVo vo = list.get(i) 와 같음. -->
<c:forEach var="vo" items="${list }">
<tr>
<td>${vo.num}</td>
<td><a href="/webApp2/product/detail?num=${vo.num}">${vo.name}</a></td>
<td>${vo.price}</td>
<td>${vo.amount}</td>
<td><a href="/webApp2/product/delete?num=${vo.num}"> <input
type="button" value="삭제" onclick="alert('삭제되었습니다.')">
</a></td>
</c:forEach>
</table>
</body>
</html>
detail.jsp
<%@ 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>
<h3>상세 페이지</h3>
<form action="/webApp2/product/detail" method="post">
<table border="1">
<tr>
<th>제품번호</th>
<td><input type="number" value="${vo.num}" readonly name="num"></td>
</tr>
<tr>
<th>제품명</th>
<td><input type="text" value="${vo.name}" readonly></td>
</tr>
<tr>
<th>가격</th>
<td><input type="number" value="${vo.price}" name="price"></td>
</tr>
<tr>
<th>수량</th>
<td><input type="number" value="${vo.amount}" name="amount"></td>
</tr>
<tr>
<th>수정</th>
<td><input type="submit" value="수정"></td>
</tr>
</table>
</form>
</body>
</html>