ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 21111~211112_mybatis_2
    카테고리 없음 2021. 11. 12. 10:49

    1. servlet 선택

     

    2. writeformservlet 생성

     

    3. writeformservlet

     

    4.writeform

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

    <!DOCTYPE html>

    <html>

    <head>

    <meta charset="UTF-8">

    <link href="https://fonts.googleapis.com/css2?family=Do+Hyeon&display=swap" rel="stylesheet">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

    <script src="https://code.jquery.com/jquery-3.5.0.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

    <title>Insert title here</title>

    </head>

    <body>

    <!-- wirte라는 매핑주소를 가지고 있는 서블릿 호출 -->

    <form action="write" method="post">

      <table class="table table-bordered" style="width: 350px; margin-left: 50px;">

        <caption><b>차정보 입력</b></caption>

          <tr>

            <th bgcolor="#e6e6fa" width="100">차종</th>

            <td>

    <!-- name은 dto랑 일치해야 함 -->

              <input type="text" name="carname" size="10" required="required" class="form-control">

            </td>

          </tr>

          

          <tr>

            <th bgcolor="#e6e6fa" width="100">가격</th>

            <td>

              <input type="text" name="carprice" size="10" required="required" class="form-control">

            </td>

          </tr>

          

          <tr>

            <th bgcolor="#e6e6fa" width="100">색상</th>

            <td>

              <input type="color" name="carcolor" required="required" value="#ffcccc">

            </td>

          </tr>

          

          <tr>

            <th bgcolor="#e6e6fa" width="100">구입일</th>

            <td>

              <input type="date" name="carguip" size="10" required="required">

            </td>

          </tr>

          

          <tr>

            <td colspan="2" align="center">

              <button type="submit" class="btn btn-default btn-sm">DB저장</button>

              <button type="button" class="btn btn-default btn-sm" onclick="location.href='list'">목록</button>

            </td>

          </tr>

      </table>

    </form>

    </body>

    </html>

    5. writeservlet 생성

     

     

    6. carsql 추가

     

    7. cardao에 insert 추가

     

    8. writeservlet

    package car.servlet;

     

    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 data.car.CarDao;

    import data.car.CarDto;

     

     

    @WebServlet("/samsung/write")

    public class WriteServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

        

    //전역변수로 두기..쓸 데가 많기 때문에

    CarDao dao=new CarDao();

     

        public WriteServlet() {

            super();

        }

     

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //한글 엔코딩

    request.setCharacterEncoding("utf-8");

     

    //데이터 읽어서 dto에 넣기

    CarDto dto=new CarDto();

    String carname=request.getParameter("carname");

    int carprice=Integer.parseInt(request.getParameter("carprice"));

    String carcolor=request.getParameter("carcolor");

    String carguip=request.getParameter("carguip");

     

    dto.setCarname(carname);

    dto.setCarprice(carprice);

    dto.setCarcolor(carcolor);

    dto.setCarguip(carguip);

     

    //db에 추가

    dao.insertCar(dto);

     

    //목록으로 이동..리스트를 보여주는 서블릿 기사 데이터 가져온 후 jsp 가서 출력

    response.sendRedirect("list");

    }

     

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    doGet(request, response);

    }

    }

     

    9. carsql에 전체출력 메서드 추가

     

    10. cardao에 전체출력 메서드 추가

     

    11. listservlet에 목록 가져오기,저장,전달 추가

     

    12. carlist에 출력할 테이블 메서드 추가

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

        

        <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

        <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

    <!DOCTYPE html>

    <html>

    <head>

    <meta charset="UTF-8">

    <link href="https://fonts.googleapis.com/css2?family=Do+Hyeon&display=swap" rel="stylesheet">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

    <script src="https://code.jquery.com/jquery-3.5.0.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

    <title>Insert title here</title>

    </head>

    <body>

     

    <button type="button" class="btn btn-info"

     onclick="location.href='writeform'">차정보 입력</button>

    <!-- wirteform이라는 매핑주소인 서블릿을 호출 -->

     

    <c:if test="${totalCount==0}">

      <h2>저장된 차의 정보가 없습니다</h2>

    </c:if>

     

    <c:if test="${totalCount>0}">

      <div class="alert alert-info" style="width: 600px;">

        <b>총 ${requestScope.totalCount} 개의 데이터가 있습니다</b>

      </div>

      

    <hr>  

      <table class="table table-bordered" style="width: 800px;">

        <tr bgcolor="#f0f8ff">

          <th width="60">번호</th>

          <th width="120">차종</th>

          <th width="100">가격</th>

          <th width="100">색상</th>

          <th width="150">구입일</th>

          <th width="120">편집</th>

        </tr>

        

        <c:forEach var="s" items="${list}" varStatus="i">

          <tr align="center">

            <td>${i.count }</td>

            <td>${s.carname }</td>

            <td align="center">

              <fmt:formatNumber value="${s.carprice }" type="currency"/>

            </td>

            <td>

              <div style="width: 20px; height: 20px; background-color: ${s.carcolor}; border: 1px solid gray;"></div>

            </td>

            <td>${s.carguip }</td>

            <td>

              <button class="btn btn-default btn-sm" 

              onclick="location.href='updateform?num=${s.num}'">수정</button>

              <button class="btn btn-default btn-sm"

              onclick="location.href='delete?num=${s.num}'">삭제</button>

            </td>

          </tr>

        </c:forEach>

      </table>

    </c:if>

    </body>

    </html>

    댓글

Designed by Tistory.