[JSP 개념] mvc 패턴 분석

2021. 4. 6. 00:23 Java 관련/JSP 개념

<test.jsp>

[Test.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>
   저장완료
</body>
</html>

<db 연결 파일>

package A1101.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;

import A1101.conn.DBConn;
import A1101.model.T1101Vo;


public class InsertDao implements InsertDaoI {
      Connection conn = null;
      PreparedStatement pstmt = null;

      @Override
      public void insert(T1101Vo m) {
         try {
            conn = DBConn.getConnection();
            
            String sql = "insert into T20191101 values(sT20191101.nextval, ?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, m.getId());
            pstmt.setString(2, m.getName());
            pstmt.setString(3, m.getPwd());
            pstmt.setInt(4, m.getAge());
            pstmt.setString(5, m.getEmail());
            
            pstmt.executeUpdate();
         } catch (Exception e) {
            e.printStackTrace();
         } finally {
            DBConn.close(pstmt, conn);
         }
      }

   }

<vo 파일>

package A1101;

public class T1101Vo {
    private int idx;    
    private String id;  
    private String name;
    private String pwd;
    private int age;
    private String email;
    
    
        
    
    
    T1101Vo(String id,String name,String pwd,int age, String email){
       this.id=id;
       this.name=name;
       this.pwd=pwd;
       this.age=age;
       this.email=email;
    }
    
    
    
   public int getIdx() {
      return idx;
   }
   public void setIdx(int idx) {
      this.idx = idx;
   }
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getPwd() {
      return pwd;
   }
   public void setPwd(String pwd) {
      this.pwd = pwd;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String getEmail() {
      return email;
   }
   public void setEmail(String email) {
      this.email = email;
   }
   
   @Override
   public String toString() {
       return "T1101Vo [idx=" + idx + ", id=" + id + ", name=" + name + ", pwd=" + pwd + ", age=" + age + ", email="
         + email + "]";
   } 
   
   
   

}

<서블릿>

package A1101.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 A1101.model.T1101Vo;
import A1101.service.InsertService;
import A1101.service.InsertServiceI;

/**
 * Servlet implementation class InsertController
 */
@WebServlet("/InsertController")
public class InsertController extends HttpServlet {
   private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertController() {
        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
      String id =request.getParameter("id");
      String name =request.getParameter("name");
      String pwd = request.getParameter("pwd");
      int age =Integer.parseInt(request.getParameter("age"));
      String email =request.getParameter("email");
      
      InsertServiceI s = new InsertService();
      T1101Vo m = new T1101Vo(id,name,pwd,age,email);
      s.insert(m);
      
      
      RequestDispatcher dispatcher=request.getRequestDispatcher("/A1101/Test.jsp");
      dispatcher.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);
   }

}

<가입 폼>

<%@ 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>회원가입 폼만들기</h2>

<form action="<%=request.getContextPath()%>/InsertController">
아 디 :   <input type="text" name = id>  <br>
이 름 : <input type="text" name =name><br>
암 호 :   <input type="text" name =pwd><br>
나 이 :   <input type="text" name =age><br>
메 일 :   <input type="text" name =email><br>
<input type="submit" value="가입">
</form>



</body>
</html>

 

출처 : namubada.net/65?category=769900