๊ตฌํ ์์ ( ๋ฐ์ดํฐ๋ฒ ์ด์ค ๐๐ป ์๋ฒ ๐๐ป ํ๋ก ํธ ๐๐ป ๋ฐฑ์๋ )
1) [์ ๋ณด์ฒ๋ฆฌ์ฐ์ ๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ค๊ธฐ์ํ ๋ฌธ์ - ์ง์ญ๊ตฌ์์ํฌํ
2) [์ ๋ณด์ฒ๋ฆฌ์ฐ์ ๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ค๊ธฐ MVC ํจํด ๊ตฌ์กฐ / ํ๋ฆ
3) [์ ๋ณด์ฒ๋ฆฌ์ฐ์
๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ง์ญ๊ตฌ์์ํฌํ - DB ์์ฑํ๊ธฐ
4) [์ ๋ณด์ฒ๋ฆฌ์ฐ์
๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ง์ญ๊ตฌ์์ํฌํ - Java / Servlet ์์ฑ
5) [์ ๋ณด์ฒ๋ฆฌ์ฐ์
๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ง์ญ๊ตฌ์์ํฌํ - View ํ๋ฉด ๋ง๋ค๊ธฐ
6) [์ ๋ณด์ฒ๋ฆฌ์ฐ์
๊ธฐ์ฌ ๊ณผ์ ํ๊ฐํ] ์ง์ญ๊ตฌ์์ํฌํ - ๋ฐ์ดํฐ ์
/์ถ๋ ฅ
โจ Step2. Java / Servlet ์์ฑ
ํ๋ฉด ์ ํ ๋ฐ View ์ DB์ ์ฐ๊ฒฐ์ ๋์์ค Java ํ์ผ์ ๋ง๋ค์ด ์ค๋๋ค.
๊ทธ์ ์ Tomcat 9.0\lib ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํด๋๋ก ์ด๋ํด์ servlet-api.jar ๋ฅผ WEB-INFํด๋์ lib ํด๋์์ ๋ฃ์ด์ค๋๋ค.
โพ๏ธweb.xml
WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>HRD1</display-name>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>VoteDB.VoteController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<web-app> ์์ <servlet></servlet> , <servlet-mapping></servlet-mapping> ์ ์ ๋ ฅํด์ฃผ์ธ์.
๊ตฌ๋ถ | ์ค๋ช |
<servlet-name> | <servlet>๊ณผ <servlet-mapping>์ ์ฐ๊ฒฐ๊ณ ๋ฆฌ |
<servlet-class> | ํจํค์ง๋ช .ํด๋์ค๋ช ์ผ๋ก ๊ตฌ์ฑ๋๋ฉฐ, ํด๋น ์์น์ ์๋ฐ ํ์ผ์ ์๋ ค์ค๋ค. |
<url-pattern> | jsp์์ action์ ํธ์ถํ ๋ ์ฌ์ฉ (*.do ๋ฅผ ํธ์ถํ๋ฉด VoteDB ํจํค์ง์ VoteController์ ๋ถ๋ฅด๊ฒ๋๋ค.) |
โพ๏ธํจํค์ง ๋ฐ class ์์ฑ
VoteDB ํจํค์ง ์์ Servlet ํ์ผ์ ์์ฑํฉ๋๋ค.
jsp์ ๊ธฐ์ด request ๋ฐฉ์
Servlet์ ์์ฑํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก doGet(get ๋ฐฉ์ ํธ์ถ)๊ณผ doPost(post ๋ฐฉ์ ํธ์ถ) ๊ฐ ์ฃผ์ด์ง๋๋ค.
๋งจ ์๋์ ์ถ๊ฐ์ ์ผ๋ก requestPro๋ฅผ ์์ฑํด์ get, post์ ๊ตฌ๋ถ์์ด ์คํ๋๋๋ก ์์ฑํด์ค๋๋ค.
package VoteDB;
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;
public class VoteController extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
requestPro(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
requestPro(request, response);
}
protected void requestPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* URL check */
String uri = request.getRequestURI();
String context = request.getContextPath();
String command = uri.substring(context.length());
String site = null;
System.out.println("command : "+command);
VoteDAO vote = new VoteDAO();
switch(command) {
case "/main.do" :
site = "index.jsp";
break;
case "/memberList.do" :
site = "memberList.jsp";
break;
case "/voteMember.do" :
site = "voteMember.jsp";
break;
case "/voteList.do" :
site = "voteList.jsp";
break;
case "/voteResult.do" :
site = "voteResult.jsp";
break;
default : break;
}
/* ๊ฒฐ๊ณผ */
RequestDispatcher dispatcher = request.getRequestDispatcher(site);
dispatcher.forward(request, response);
}
}
โพ๏ธDTO ์์ฑ
package VoteBean;
public class Vote {
String v_name;
String v_jumin;
String v_age;
String v_gender;
String m_no;
String v_time;
String v_confirm;
public String getV_name() {
return v_name;
}
public void setV_name(String v_name) {
this.v_name = v_name;
}
public String getV_jumin() {
return v_jumin;
}
public void setV_jumin(String v_jumin) {
this.v_jumin = v_jumin;
}
public String getV_age() {
return v_age;
}
public void setV_age(String v_age) {
this.v_age = v_age;
}
public String getV_gender() {
return v_gender;
}
public void setV_gender(String v_gender) {
this.v_gender = v_gender;
}
public String getM_no() {
return m_no;
}
public void setM_no(String m_no) {
this.m_no = m_no;
}
public String getV_time() {
return v_time;
}
public void setV_time(String v_time) {
this.v_time = v_time;
}
public String getV_confirm() {
return v_confirm;
}
public void setV_confirm(String v_confirm) {
this.v_confirm = v_confirm;
}
}
package VoteBean;
public class Result {
String m_no;
String m_name;
String v_total;
public String getM_no() {
return m_no;
}
public void setM_no(String m_no) {
this.m_no = m_no;
}
public String getM_name() {
return m_name;
}
public void setM_name(String m_name) {
this.m_name = m_name;
}
public String getV_total() {
return v_total;
}
public void setV_total(String v_total) {
this.v_total = v_total;
}
}
package VoteBean;
public class Member {
String m_no;
String m_name;
String p_name;
String p_school;
String m_jumin;
String m_city;
String p_tel;
public String getM_no() {
return m_no;
}
public void setM_no(String m_no) {
this.m_no = m_no;
}
public String getM_name() {
return m_name;
}
public void setM_name(String m_name) {
this.m_name = m_name;
}
public String getP_name() {
return p_name;
}
public void setP_name(String p_name) {
this.p_name = p_name;
}
public String getP_school() {
return p_school;
}
public void setP_school(String p_school) {
this.p_school = p_school;
}
public String getM_jumin() {
return m_jumin;
}
public void setM_jumin(String m_jumin) {
this.m_jumin = m_jumin;
}
public String getM_city() {
return m_city;
}
public void setM_city(String m_city) {
this.m_city = m_city;
}
public String getP_tel() {
return p_tel;
}
public void setP_tel(String p_tel) {
this.p_tel = p_tel;
}
}
โพ๏ธDAO ์์ฑ
์ดํด๋ฆฝ์ค์์ java์ oracle์ ์ฐ๊ฒฐํ๊ธฐ ์ํด
C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib
์์ ๊ฒฝ๋ก์์ jar ํ์ผ์ lib ํด๋ ์์ ๋ฃ์ด์ค๋๋ค.
๊ทธ ํ์ ๋ฌธ์ ์์ ์ ๊ณต๋๋ Connection์ VoteDAO ์ ๊ทธ๋๋ก ์์ฑํด์ค๋๋ค.
Connection con = "oracle ์ฐ๊ฒฐ ๊ฒฝ๋ก","์ ์ id","๋น๋ฐ๋ฒํธ" ์ ๋๋ค.
์์ฑ์๋ฅผ ํตํด DAO ํธ์ถ์ ์๋์ผ๋ก Connection์ด ์ฒ๋ฆฌ๋๋๋ก ์ธํ ์ ํฉ๋๋ค.
package VoteDB;
import java.sql.*;
public class VoteDAO {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
public static Connection getConnection() throws Exception {
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection
("jdbc:oracle:thin:@//localhost:1521/xe","system","1234");
return con;
}
}
๋ค์ ํฌ์คํ ์์๋ View ํ๋ฉด์ ๊ตฌํํ๋๋ก ํ๊ฒ ์ต๋๋ค ๐