[Day9] JSP/Servlet 9 [12/14]
[ p 516 Chapter 18 MVC ํจํด ๊ตฌํ ]
- ๋ชจ๋ธ 1 ๊ตฌ์กฐ / ๋ชจ๋ธ 2๊ตฌ์กฐ
- MVC ํจํด
- ์ปค๋งจ๋ ํจํด์ ์ฌ์ฉํ [์ปจํธ๋กค๋ฌ(Controller)] ๊ตฌํ ( MVC ํจํด์ ํต์ฌ )
๋ชจ๋ธ 2๊ตฌ์กฐ ๊ธฐ๋ฐ์ MVC ํจํด์ผ๋ก ์น ์ดํ๋ฆฌ์ผ์ด์ ๊ตฌํํ๋ ๊ฒ์ด ๋ชฉ์ .
- JSP ๊ฐ๋ฐ์๋ผ๋ฉด "๋ฐ๋์ ์ต๋ํด์ผํ ๊ธฐ์ "์ด๋ค.
- Spring(MVC ํจํด ํ๋ ์์ํฌ)์ ๋ณด๋ค ์ฝ๊ฒ ์ ์ํ ์ ์๋ค.
[ JSP ์น ์ดํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์กฐ ]
1) ๋ชจ๋ธ 1๊ตฌ์กฐ [ํด] -> ์์ฒญ-> [JSPํ์ด์ง] ๋ก์ง๋ด๋น ์ถ๋ ฅ๋ด๋น <-์๋ต<- |
2) ๋ชจ๋ธ 2๊ตฌ์กฐ
[ํด] -> ์์ฒญ-> [JSPํ์ด์ง] + [๊ฐ์ฒด] ์ถ๋ ฅ๋ด๋น ๋ก์ง๋ด๋น <-์๋ต<- |
์)
list.htm -> List.java ์๋ธ๋ฆฟ -> list.jsp
doGet(){
// ๋ก์ง์ฒ๋ฆฌ // ์ถ๋ ฅ๋ด๋น
request.setAttribute();
// ํฌ์๋ฉ
}
[MVC ํจํด] ๋ก์ง + ์ถ๋ ฅ ๋ด๋น ๊ตฌ๋ถ( ๋ชจ๋ธ2๊ตฌ์กฐ )
M Model - ๋ก์ง ์ฒ๋ฆฌ ๋ด๋น, ๋น์ฆ๋์ค ์์ญ , ์๋ธ๋ฆฟ X, ์๋ฐ ํด๋์ค (๊ฐ์ฒด)
V View - ์ถ๋ ฅ ๋ด๋น, ํ๋ ์ ํ ์ด์ ์์ญ , list.jsp
C ์ปจํธ๋กค๋ฌ ***** - ์ฌ์ฉ์์ ์ ๋ ฅ์ฒ๋ฆฌ์ ํ๋ฆ ์ ์ด ๋ด๋น. ์๋ธ๋ฆฟ 1๊ฐ
Model
[list.htm] ListHandler.java - list = ์๋น์ค - DAO ( list = select() )
edit.htm EditHander.java
view.htm -> ์ปจํธ๋กค๋ฌ(์๋ธ๋ฆฟ) list
delete.htm 1) ์์ฒญURL ๋ถ์ request.setAttribute("")
write.htm list.htm(๊ฒ์๊ธ ๋ชฉ๋ก ) list.jsp
: :
:
์๋น
์๋ + ์ฃผ๋ฌธ(ํ์๋น)-> ์ปจํธ๋กค๋ฌ -> ์ฃผ๋ฐฉ(์๋ฆฌ์ฌ) + ์์ ์ค๋น
<- <- ์ปจํธ๋กค๋ฌ <-
- ์น ๊ฐ๋ฐ์๋ผ๋ฉด ๋ฐ๋์ ์์์ผ ํ ํจํด์ด๋ค.
[ MVC ํจํด์ ํต์ฌ ]
1. ๋ก์ง + ์ถ๋ ฅ ๋ด๋น ๋ถ๋ฆฌ
2. ์ปจํธ๋กค๋ฌ ์ญํ ์ค์(์ง์ค)
๊ฐ๋ฐ ๊ธฐ๊ฐ์ ํจ์ฌ ๋ง์ด ๊ฑธ๋ฆฌ์ง๋ง ์ ์ง,๋ณด์, ํ์ฅ์ฑ์ด ๋ฐ์ด๋๊ธฐ์~~
p 520 ๊ทธ๋ฆผ 18.4 MV[C] ์ปจํธ๋กค๋ฌ์ ์ญํ ( ์๊ธฐ ) ์ํ...
1.[ํด] -> ๋ชจ๋ ์์ฒญ -> ์ปจํธ๋กค๋ฌ(C)[์๋ธ๋ฆฟ]
list.htm 1) ์์ฒญURL ๋ถ์
http://~~~/list.htm?cp=2&sc=1&sw=aaa
๊ธ ๋ชฉ๋ก
2) ๋ง์ ๋ก์ง ๋ด๋น์ ํธ๋ค๋ฌ(Handler) - ๋ชจ๋ธ
๊ธ ๋ชฉ๋ก ํธ๋ค๋ฌ ๊ฐ์ฒด๋ฅผ ์ป์ด์จ๋ค(์ฐพ๋๋ค.)
3) ๊ธ๋ชฉ๋กํธ๋ค๋ฌ.๋ก์ง์ฒ๋ฆฌ();
list= ์๋น์ค - list = DAO - DB ์ฐ๋
request.setAttribute();
list.jsp
4) ํฌ์๋ฉ ๋๋ ๋ฆฌ๋ค์ด๋ ํธ
<-์๋ต- ๋ทฐ( jsp )
[ p 528 ์ปค๋งจ๋ ํจํด ๊ธฐ๋ฐ์ ์ฝ๋ ]
[์์ฒญ URL ๋ถ์ + ๋ช ๋ น์ด ์ ๋ฌ] 2๊ฐ์ง
http://localhost/jspPro/cstvsboard/delete.htm?seq=340
delete.htm : ๊ฒ์๊ธ ์ญ์ -> Delete.java ์๋ธ๋ฆฟ
?seq=340 : 340 ๊ธ๋ฒํธ ์ญ์
1) ํน์ ์ด๋ฆ์ ํ๋ผ๋ฏธํฐ์ ๋ช ๋ น์ด ์ ๋ณด๋ฅผ ์ ๋ฌ
http://localhost/jspPro/cstvsboard/board.htm?cmd=delete&seq=340
http://localhost/jspPro/cstvsboard/board.htm?cmd=list
http://localhost/jspPro/cstvsboard/board.htm?cmd=edit
2) ์์ฒญ URI ์์ฒด๋ฅผ ๋ช ๋ น์ด๋ก ์ฌ์ฉํ๋ค.
http://localhost/jspPro/cstvsboard/delete.htm?seq=340
http://localhost/jspPro/cstvsboard/delete/340
http://localhost/jspPro/cstvsboard/edit.htm?seq=340
p 531 ์ค๋ช
CommandHandler ์ธํฐํ์ด์ค + String process()
"???.jsp"
๋ก์ง ๋ด๋น ํด๋์ค
List[Handler] process(){}
WriteForm[Hanlder] process(){}
: process(){}
:
:
p 533 ์ปจํธ๋กค๋ฌ(์๋ธ๋ฆฟ) ๊ตฌํ ์์
p 536 web.xml ์ปจํธ๋กค๋ฌ ๋ฑ๋ก ์ฝ๋ฉ.
p 537 commandHandler.properties ํ์ผ
์์ฒญURL=๋ชจ๋ธ(ํธ๋ค๋ฌ)
hello=mv.hello. HelloHandler
hello=mv.hello.HelloHandler
hello=mv.hello.HelloHandler
[ ๊ฒ์ํ ๊ตฌํ ] days05 ใด board ํด๋ ใด delete.jsp ใด edit.jsp ใด list.jsp ใด view.jsp ใด write.jsp days05.board ใด BoardDTO.java ใด BoardDAO.java ใด BoardDAOImpl.java ใด Delete.java (์๋ธ๋ฆฟ) ใด Edit.java (์๋ธ๋ฆฟ) ใด List.java (์๋ธ๋ฆฟ) ใด View.java (์๋ธ๋ฆฟ) ใด Write.java (์๋ธ๋ฆฟ) ใด PageBlock.java - ํ์ด์ง ์ฒ๋ฆฌ ๋ง๋ ํด๋์ค |
[ ์์ ๊ฒ์ํ ๊ตฌํ์ MVC ํจํด์ผ๋ก ๊ตฌํ(์ฒ๋ฆฌ) ] 1. days09 ํด๋ ใด board ํด๋ ์์ฑ ์ถ๋ ฅ๋ด๋น View 2. days09>board ํด๋ ์์ ์ถ๋ ฅ๋ด๋น jsp ํ์ผ ๋ชจ๋ ๋ณต์ฌ ๋ถ์ด๊ธฐ. 3. MV[C] ์ปจํธ๋กค๋ฌ ์๋ธ๋ฆฟ ์ ์ธ web.xml ์ปจํธ๋กค๋ฌ ์๋ธ๋ฆฟ ๋ฑ๋ก 1) web.xml ๋ณต์ฌ ๋ถ์ด๊ธฐ + web_days08.xml DBCP ์ปค๋ฅ์ ํ ์ค์ ๋ถ๋ถ๋ง ๋จ๊ฒจ๋๋ค. ๊ทธ์ธ๋ ๋ชจ๋ ์ญ์ . 4. ์ปจํธ๋กค๋ฌ ์๋ธ๋ฆฟ ํด๋์ค ์ถ๊ฐ. Spring ํ๋ ์์ํฌ์ ์ปจํธ๋กค๋ฌ ์ด๋ฆ์ด DispatcherServlet ๊ณผ ๋์ผํ๊ฒ ๋ช ๋ช . days09.mvc.controller.DispatcherServlet.java ์๋ธ๋ฆฟ 5. ์ปจํธ๋กค๋ฌ๋ฅผ web.xml ์๋ธ๋ฆฟ ๋ฑ๋ก. 6. ์ธํฐํ์ด์ค, ํธ๋ค๋ฌ, ๊ฒ์ ๋ชฉ๋กํ์ด์ง( ์์ฒญURL=ํธ๋ค๋ฌ ๋งคํ) days09.mvc.command ํจํค์ง // 1) CommandHandler ์ธํฐํ์ด์ค + process() // 2) ListHandler, WriteHandler ๋ชจ๋ธ. // 3) /WEB-INF/commandHandler.properties 7. days09.mvc.controller.DispatcherServlet.java ์๋ธ๋ฆฟ ๊ฐ์ฒด ์ธ์ ์์ฑ์ด ๋๋? ใด ์ฒซ ๋ฒ์งธ ์์ฒญ์ด ๋ค์ด์ค๋ฉด ๊ทธ ๋ ์๋ธ๋ฆฟ ๊ฐ์ฒด๋ [ ์น ์ปจํ ์ด๋ ] ์์ ์์ฑ๋์ด์ง๋ค. => ํฐ์บฃ ์๋ฒ๊ฐ ์์๋ ๋ ์๋์ผ๋ก ์ปจํธ๋กค๋ฌ(์๋ธ๋ฆฟ) ๊ฐ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์์ฑํด ๋๋ฉด.. ์ฑ๋ฅ ํฅ์ web.xml ์ฝ๋ฉ ์ถ๊ฐ <load-on-startup>1</load-on-startup> 8. ์ปจํธ๋กค๋ฌ๊ฐ ์๋ฒ ์์ํ ๋ ์๋์ผ๋ก ์๋ธ๋ฆฟ ๊ฐ์ฒด ์์ฑ์ด ๋๋ค... ใด ์๋ธ๋ฆฟ ๊ฐ์ฒด ๊ฐ ์์ฑ๋ ๋ ์ด๊ธฐํ์์ ์ข ํ ๊ฒ์... (option command s -> override) init() ๋ฉ์๋ ๊ตฌํ 9. ์๋ฒ ์ฌ์์ - ์ค๋ฅ ๋ฐ์ java.lang.ClassNotFoundException: days09.mvc.command.WriteHandler 10. DispatcherServlet ใด doGet() ์ฝ๋ฉ ๋ง๋ฌด๋ฆฌ... 11. ๊ธ ๋ชฉ๋ก MVC ํจํด์ผ๋ก ๊ตฌํ ์๋ฃ!!! |
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/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>jspPro</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- DBCP ์ค์ -->
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!--
@WebFilter(dispatcherTypes = {DispatcherType.REQUEST }
, urlPatterns = { "/*" })
-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>com.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<!-- <url-pattern>/</url-pattern> ์์ ํจํด๊ณผ ์ฐจ์ด์ . -->
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</web-app>
days09 - mvc - controllerํด๋ ์์
DispatcherServlet.java
package days09.mvc.controller;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
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 days09.mvc.command.CommandHandler;
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DispatcherServlet() {
super();
// ์ด๊ธฐํ ์์
}
@Override
public void destroy() {
super.destroy();
}
// key = "/board/list.do" ์์ฒญURL
// value = "days09.mvc.command.ListHandler" X ์์ฑ๋ ํธ๋ค๋ฌ ๊ฐ์ฒด ์์ฑ
private Map<String, CommandHandler> commandHandlerMap = new HashMap<String, CommandHandler>();
@Override
public void init() throws ServletException {
// ์ด๊ธฐํ ์์
.
// 1. /WEB-INF/commandHandler.properties
String path = this.getInitParameter("path");
String realPath = this.getServletContext().getRealPath(path);
Properties prop = new Properties();
try(FileReader reader = new FileReader(realPath) ) {
prop.load(reader);
} catch (Exception e) {
throw new ServletException();
}
// commandHandlerMap<url, ์ค์ ํธ๋ค๋ฌ ๊ฐ์ฒด ์์ฑ>
Set<Entry<Object, Object>> set = prop.entrySet();
Iterator<Entry<Object, Object>> ir = set.iterator();
while( ir.hasNext()) {
Entry<Object, Object> entry = ir.next();
String url = (String) entry.getKey(); // "/board/list.do"
String className = (String) entry.getValue(); // "days09.mvc.command.ListHandler"
try {
Class<?> handlerClass = Class.forName(className);
CommandHandler commandHandler = (CommandHandler) handlerClass.newInstance();
commandHandlerMap.put(url, commandHandler);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
} // while
} // init
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println("> DispatcherServlet.doGet() ํธ์ถ๋จ...");
// [ 1 ์์ฒญ URL ๋ถ์ ]
// [/board/list.do], edit.do, [/board/write.do] ~~ ???.do
String requestURI = request.getRequestURI(); // "/jspPro/board/list.do"
// String requestURL = request.getRequestURL().toString(); // http://localhost/jspPro/board/list.do
//System.out.println(requestURI);
//System.out.println(requestURL);
// requestURI ๊ฒฝ๋ก ์์ ์ปจํ
์คํธํจ์ค๋ง ์ ๊ฑฐํ๋ฉด commandHandlerMap key = "/board/list.do"
String contextPath = request.getContextPath(); // "/jspPro"
if ( requestURI.indexOf(contextPath) == 0 ) {
int beginIndex = contextPath.length();
requestURI = requestURI.substring(beginIndex);
}
// [ 2 ๋ก์ง ์ฒ๋ฆฌ - ํธ๋ค๋ฌ ๊ฒ์ ]
// 1) CommandHandler ์ธํฐํ์ด์ค + process()
// 2) [WriteHandler], WriteHandler ๋ชจ๋ธ.
// 3) commandHandler.properties -> Properties ์ปฌ๋ ์
ํด๋์ค
// "/board/write.do" ์์ฒญURL ์๋ ํธ๋ค๋ฌ ๊ฐ์ฒด : p536 NullHandler ํด๋์ค ์ ์ธ
CommandHandler handler = commandHandlerMap.get(requestURI);
// 3 String viewpage =ํธ๋ค๋ฌ.process();
String viewPage = null;
try {
viewPage = handler.process(request, response);
} catch (Exception e) {
e.printStackTrace();
}
// 4 ํฌ์๋ฉ, ๋ฆฌ๋ค๋ ํธ ์ฒดํฌ
// request.ใ
etAttribute("ํฌ๋ฆฌ");
// if( 1 ) ํฌ์๋ฉ
// else if( 2) ๋ฆฌ๋ค์ด๋ ํธ
// (์ ์) ํฌ์๋ฉ.
if( viewPage != null ) {
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
dispatcher.forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- ์์ฒญ๋ณ๋ก ํธ๋ค๋ฌ
jsp
<a href="<%= contextPath %>/board/list.do">๊ธ๋ชฉ๋ก</a><br>
<a href="<%= contextPath %>/board/write.do">๊ธ์ฐ๊ธฐ</a><br>
- do ํ์ฅ์ ์๋ธ๋ฆฟ ์์ฒญํ๋๋ก
Handler
days09 - mvc - commandํด๋ ์์
CommandHandler.java (์ธํฐํ์ด์ค)
package days09.mvc.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface CommandHandler {
// ๋ฆฌํด์๋ฃํ : ํฌ์๋ฉ, ๋ฆฌ๋ค์ด๋ ํธ ๋ทฐํ์ด์ง( ???.jsp )
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
- ์ปจํธ๋กค๋ฌ๋ก๋ถํฐ request ๋งค๊ฐ๋ณ์๋ก ๋ฐ์์์ ์๋ธ๋ฆฟ(์ปจํธ๋กค๋ฌ) ์์ ์ฌ์ฉํ ์์๋ request์๋ค๊ฐ setAttributeํ ์ ์๋๋ก ๋งค๊ฐ๋ณ์๋ก ๋ฐ์.
- ์์ธ๋ฐ์ํ๋ฉด ํธ์ถํ ๊ณณ์ผ๋ก ๋ ๋๊ธฐ๋์์ผ๋ก
ListHandler.java
package days09.mvc.command;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.util.ConnectionProvider;
import com.util.DBConn;
import days05.board.BoardDAOImpl;
import days05.board.BoardDTO;
import days05.board.PageBlock;
// list.do ์์ฒญ์ ๋ก์ง ๋ด๋น ํธ๋ค๋ฌ
public class ListHandler implements CommandHandler{
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
// days05.board.List.java ์๋ธ๋ฆฟ
int currentPage = 1 ;
try { currentPage=Integer.parseInt(request.getParameter("currentpage")) ;}catch(Exception e) {}
int numberPerPage = 10;
int numberOfPageBlock = 10;
int searchCondition = 1;
try { searchCondition=Integer.parseInt(request.getParameter("searchCondition")) ;}catch(Exception e) {}
String searchWord = request.getParameter("searchWord") == null ? "" : request.getParameter("searchWord");
ArrayList<BoardDTO> list = null;
int totalRecords, totalPages = 0;
Connection conn = ConnectionProvider.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
PageBlock pageBlock = null;
try {
if( searchWord == "" ) {
list = dao.select(currentPage, numberPerPage);
}else {
list = dao.search(currentPage, numberPerPage, searchCondition, searchWord);
}
pageBlock = pagingService(currentPage, numberPerPage, numberOfPageBlock , dao
, searchCondition, searchWord );
} catch (SQLException e) {
System.out.println("> ListHandler.process() Exception...");
e.printStackTrace();
}
conn.close(); // ์ปค๋ฅ์
ํ ๋ฐํ
request.setAttribute("list", list);
request.setAttribute("pageBlock", pageBlock) ;
// ํฌ 1, ๋ฆฌ 2
// request.setAttribute("ํฌ๋ฆฌ", 1);
return "/days09/board/list.jsp";
// Modal.java ํด๋์ค ์ ์ธ viewPage , ํฌ+๋ฆฌ
// return new Modal( viewPage, 1);
// ๋ฆฌ๋ค์ด๋ ํธ ๋ฐ๋ก ์ํฌ๊ฒ์..
} // process
public PageBlock pagingService(int currentPage, int numberPerPage, int numberOfPageBlock, BoardDAOImpl dao
, int searchCondition, String searchWord ) {
PageBlock pageBlock = null;
int totalRecords =0;
int totalPages = 1;
try {
if( searchWord == "" ) {
totalRecords = dao.getTotalRecords() ;
totalPages = dao.getTotalPages( numberPerPage ) ;
}else {
totalRecords = dao.getTotalRecords(searchCondition, searchWord);
totalPages = dao.getTotalPages( numberPerPage,searchCondition, searchWord ) ;
}
int begin = ( currentPage - 1)/numberOfPageBlock * numberOfPageBlock + 1;
int end = begin + numberOfPageBlock - 1;
if( end > totalPages ) end = totalPages;
boolean prev = begin == 1 ? false : true;
boolean next = end == totalPages ? false: true;
pageBlock = new PageBlock(currentPage, numberPerPage, numberOfPageBlock ,begin, end, prev, next);
} catch( SQLException e) {
e.printStackTrace();
}
return pageBlock;
}
} // class
- ์ปจํธ๋กค๋ฌ๊ฐ ํด๋น ํธ๋ค๋ฌ ๋ถ๋ฅด๋ฉด ํธ๋ค๋ฌ๊ฐ db ์์ ํ๊ณ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ request๊ฐ์ฒด์ ๋ด์์ returnํด์ฃผ๋ ์ญํ
- ์ปจํธ๋กค๋ฌ์์ url ๋ถ์ํด์ listhandler ํธ์ถ (commandHandler.properties -> ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ฒ์, Properties ์ปฌ๋ ์ ํด๋์ค์)
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/include.jspf"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. ์คํ 4:45:18</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table, td, th {
border: solid 1px gray;
}
table {
border-spacing: 3px;
border-collapse: separate;
}
table, tr, td {
/* border-radius: 3px; */
/* padding:3px; */
}
tbody tr td:nth-of-type(2) {
text-align: left;
}
a {
text-decoration: none;
color: black;
}
a:hover {
color: red;
}
</style>
<!-- ํ์ด์ง ์ฒ๋ฆฌ style -->
<style>
.pagination {
margin: 0 auto;
display: flex;
justify-content: center;
}
.pagination a {
color: black;
float: left;
padding: 4px 8px;
text-decoration: none;
transition: background-color .3s;
}
.pagination a.active {
background-color: dodgerblue;
color: white;
}
.pagination a:hover:not (.active ) {
background-color: #ddd;
}
</style>
<style>
.searchWord {
background-color: yellow;
color: red;
}
</style>
<style>
.title {
display: inline-block;
white-space: nowrap;
width: 90%;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<h3>list.jsp</h3>
<div align="center">
<h2>๋ชฉ๋ก ๋ณด๊ธฐ</h2>
<a href="<%= contextPath %>/board/write.do">๊ธ์ฐ๊ธฐ</a>
<table style="width:600px;">
<thead>
<tr>
<th width="10%">๋ฒํธ</th>
<th width="45%">์ ๋ชฉ</th>
<th width="17%">์์ฑ์</th>
<th width="20%">๋ฑ๋ก์ผ</th>
<th width="10%">์กฐํ</th>
</tr>
</thead>
<tbody>
<c:choose>
<c:when test="${ not empty list }">
<c:forEach items="${ list }" var="dto">
<tr>
<td>${ dto.seq }</td>
<%-- <td><a href="<%= contextPath %>/cstvsboard/view.htm?seq=${ dto.seq }">${dto.title }</a></td> --%>
<td><a href="<%= contextPath %>/board/view.do?seq=${ dto.seq }¤tpage=${ param.currentpage}&searchCondition=${ param.searchCondition}&searchWord=${ param.searchWord}">${dto.title }</a></td>
<td><a href="mailto:${ dto.email }">${ dto.writer }</a></td>
<td>${ dto.writedate }</td>
<td>${ dto.readed }</td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr>
<td colspan="5">๋ฑ๋ก๋ ๊ฒ์๊ธ์ด ์์ต๋๋ค.</td>
</tr>
</c:otherwise>
</c:choose>
</tbody>
<tfoot>
<tr>
<td colspan="5" align="center">
<div class="pagination">
<%--
<a href="#" class="active"> 1 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=2"> 2 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=3"> 3 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=4"> 4 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=5"> 5 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=6"> 6 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=7"> 7 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=8"> 8 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=9"> 9 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=10"> 10 </a>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=11"> » </a>
--%>
<c:if test="${ pageBlock.prev }">
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=${ pageBlock.startOfPageBlock - 1 }"> « </a>
</c:if>
<c:forEach begin="${ pageBlock.startOfPageBlock }" end="${ pageBlock.endOfPageBlock }" var="i" step="1">
<c:choose>
<c:when test="${ pageBlock.currentPage eq i }">
<a href="#" class="active">${ i }</a>
</c:when>
<c:otherwise>
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=${ i }">${ i }</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${ pageBlock.next }">
<a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=${ pageBlock.endOfPageBlock + 1 }"> » </a>
</c:if>
</div>
</td>
</tr>
<tr>
<td colspan="5" align="center">
<form method="get">
<select name="searchCondition" id="searchCondition">
<option value="1">title</option>
<option value="2">content</option>
<option value="3">writer</option>
<option value="4">title+content</option>
</select>
<input type="text" name="searchWord" id="searchWord" />
<input type="submit" value="search" />
</form>
</td>
</tr>
</tfoot>
</table>
</div>
<script>
// list.htm?searchCondition=3&searchWord=๊ฒ์์ด
// list.htm
$(function (){
// list.htm?write=success
if( '<%= request.getParameter("write") %>' == 'success' ){
alert("๊ธ ์ฐ๊ธฐ ์๋ฃ!!!")
}
// list.htm?delete=success
if( '${ param.delete}' == 'success' ){
alert("๊ธ ์ญ์ ์๋ฃ!!!")
}
// ๊ฒ์ ์ํ ๊ด๋ฆฌ
$("#searchCondition").val( '${ empty param.searchCondition? 1 : param.searchCondition }' );
$("#searchWord").val( '${ param.searchWord}' );
// /jspPro/cstvsboard/list.htm?currentpage=2 + &searchCondition=1&searchWord=title+-+30
$(".pagination a:not(.active)").attr("href" , function (i,val){
return val + "&searchCondition=${ param.searchCondition}&searchWord=${ param.searchWord}";
});
});
</script>
</body>
</html>
WEB-INF์ (์ธ๋ถ์ ๊ทผ๋ชปํ๊ฒ) commandHandler.properties ์์ฑ
# /jspPro X request URL=Handler
/board/list.do=days09.mvc.command.ListHandler
/board/write.do=days09.mvc.command.WriteHandler
/board/view.do=days09.mvc.command.ViewHandler
/board/edit.do=days09.mvc.command.EditHandler
/board/delete.do=days09.mvc.command.DeleteHandler
์ปจํธ๋กค๋ฌ์์ ์๋ธ๋ฆฟ ์ด๊ธฐํ
(์์ฑ์, init ์ด๋์ํด๋ ์๊ด์์)
writeHandler.java
package days09.mvc.command;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.util.ConnectionProvider;
import com.util.DBConn;
import days05.board.BoardDAOImpl;
import days05.board.BoardDTO;
public class WriteHandler implements CommandHandler{
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
String requestMethod = request.getMethod(); // "GET" "POST"
if(requestMethod.equals("GET")) { // "GET" Write.java doGet() ๋ณต์ฌ ๋ถ์ด๊ธฐ
// ํฌ์๋ฉ
return "/days09/board/write.jsp";
}else if(requestMethod.equals("POST")) { // "POST" Write.java doPost() ๋ณต์ฌ ๋ถ์ด๊ธฐ
// ๋ฆฌ๋ค์ด๋ ํธ
request.setCharacterEncoding("UTF-8");
String writer = request.getParameter("writer") ;
String pwd = request.getParameter("pwd") ;
String email = request.getParameter("email") ;
String title = request.getParameter("title") ;
String content = request.getParameter("content") ;
int tag = Integer.parseInt( request.getParameter("tag") );
BoardDTO dto = new BoardDTO(0, writer, pwd, email, title, null, 0, tag, content);
Connection conn = ConnectionProvider.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
int rowCount = 0;
try {
rowCount = dao.insert(dto);
} catch (SQLException e) {
System.out.println("> WriteHandler.process() Exception...");
e.printStackTrace();
}
conn.close(); // ์ปค๋ฅ์
ํ ๋ฐํ.
String location = "/jspPro/board/list.do";
if( rowCount == 1) {
location +="?write=success";
}else {
location +="?write=fail";
}
response.sendRedirect(location);
} //
return null;
}
}
write.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/include.jspf"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. ์คํ 3:28:25</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table, td, th {
border: solid 1px gray;
}
table {
border-spacing: 3px;
border-collapse: separate;
}
table, tr, td {
/* border-radius: 3px; */
/* padding:3px; */
}
</style>
</head>
<body>
<h3>write.jsp</h3>
<!-- 4:05 ์์
์์ -->
<div align="center">
<h2>๊ธ ์ฐ๊ธฐ</h2>
<!--
action ์์ฑ ์๋ต : http://localhost/jspPro/cstvsboard/write.htm
-> Write.java ์๋ธ๋ฆฟ
-> doPost()
-->
<form method="post">
<table style="padding: 2px; width: 600px">
<tr>
<td colspan="2" align="center"><b>๊ธ์ ์ ์ด์ฃผ์ธ์</b></td>
</tr>
<tr>
<td align="center">์ด๋ฆ</td>
<td><input type="text" name="writer" size="15"
autofocus="autofocus" required="required"></td>
</tr>
<tr>
<td align="center">๋น๋ฐ๋ฒํธ</td>
<td><input type="password" name="pwd" size="15" required="required"></td>
</tr>
<tr>
<td align="center">Email</td>
<td><input type="email" name="email" size="50" ></td>
</tr>
<tr>
<td align="center">์ ๋ชฉ</td>
<td><input type="text" name="title" size="50" required="required"></td>
</tr>
<tr>
<td align="center">๋ด์ฉ</td>
<td><textarea name="content" cols="50" rows="10"></textarea></td>
</tr>
<tr>
<td align="center">HTML</td>
<td><input type="radio" name="tag" value="1" checked>์ ์ฉ
<input type="radio" name="tag" value="0">๋น์ ์ฉ</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="์์ฑ ์๋ฃ">
<input type="reset" value="๋ค์ ์์ฑ">
<a href="<%= contextPath %>/cstvsboard/list.htm">Home</a>
</td>
</tr>
</table>
</form>
</div>
<script>
$(function (){
$("form").submit(function (){
alert("์ ํจ์ฑ ๊ฒ์ฌ ์ฒดํฌ ํ๋ค. ");
// event.preventDefault();
// return false;
});
});
</script>
</body>
</html>
editHandler.java
package days09.mvc.command;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.util.ConnectionProvider;
import com.util.DBConn;
import days05.board.BoardDAOImpl;
import days05.board.BoardDTO;
public class EditHandler implements CommandHandler{
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
String requestMethod = request.getMethod();
if(requestMethod.equals("GET")) {
// ํฌ์๋ฉ
int seq = Integer.parseInt( request.getParameter("seq") );
Connection conn = ConnectionProvider.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
BoardDTO dto = null;
try {
dto = dao.view(seq);
} catch (SQLException e) {
System.out.println("> EditHandler.process() Exception...");
e.printStackTrace();
}
conn.close(); // ๋ฐํ
request.setAttribute("dto", dto);
// 3. ํฌ์๋ฉ
return "/days09/board/edit.jsp";
}else if(requestMethod.equals("POST")) { // "POST"
// ๋ฆฌ๋ค์ด๋ ํธ
request.setCharacterEncoding("UTF-8");
int seq = Integer.parseInt(request.getParameter("seq"));
String pwd = request.getParameter("pwd") ;
String email = request.getParameter("email") ;
String title = request.getParameter("title") ;
String content = request.getParameter("content") ;
int tag = Integer.parseInt( request.getParameter("tag") );
BoardDTO dto = new BoardDTO(seq, null, pwd, email, title, null, 0, tag, content);
Connection conn = ConnectionProvider.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
int rowCount = 0;
try {
rowCount = dao.update(dto);
} catch (SQLException e) {
System.out.println("> EditHandler.process() Exception...");
e.printStackTrace();
}
conn.close();
String location = "/jspPro/board/view.do?seq="+ seq;
if( rowCount == 1) {
location +="&edit=success";
}else {
location +="&edit=fail";
}
response.sendRedirect(location);
} //
return null;
}
}
View Handler
package days09.mvc.command;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.util.ConnectionProvider;
import com.util.DBConn;
import days05.board.BoardDAOImpl;
import days05.board.BoardDTO;
public class ViewHandler implements CommandHandler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
int seq = Integer.parseInt(request.getParameter("seq"));
Connection conn = ConnectionProvider.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
BoardDTO dto = null;
try {
dao.increaseReaded(seq);
dto = dao.view(seq);
} catch (SQLException e) {
System.out.println("> ViewHandler.process() Exception...");
e.printStackTrace();
}
request.setAttribute("dto", dto);
conn.close(); // ์ปค๋ฅ์
ํ์ ๋ฐํ
// 3. ํฌ์๋ฉ
return "/days09/board/view.jsp";
}
}
editHandler.java
package days09.mvc.command;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.util.ConnectionProvider;
import com.util.DBConn;
import days05.board.BoardDAOImpl;
import days05.board.BoardDTO;
public class EditHandler implements CommandHandler{
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
String requestMethod = request.getMethod();
if(requestMethod.equals("GET")) {
// ํฌ์๋ฉ
int seq = Integer.parseInt( request.getParameter("seq") );
Connection conn = ConnectionProvider.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
BoardDTO dto = null;
try {
dto = dao.view(seq);
} catch (SQLException e) {
System.out.println("> EditHandler.process() Exception...");
e.printStackTrace();
}
conn.close(); // ๋ฐํ
request.setAttribute("dto", dto);
// 3. ํฌ์๋ฉ
return "/days09/board/edit.jsp";
}else if(requestMethod.equals("POST")) { // "POST"
// ๋ฆฌ๋ค์ด๋ ํธ
request.setCharacterEncoding("UTF-8");
int seq = Integer.parseInt(request.getParameter("seq"));
String pwd = request.getParameter("pwd") ;
String email = request.getParameter("email") ;
String title = request.getParameter("title") ;
String content = request.getParameter("content") ;
int tag = Integer.parseInt( request.getParameter("tag") );
BoardDTO dto = new BoardDTO(seq, null, pwd, email, title, null, 0, tag, content);
Connection conn = ConnectionProvider.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
int rowCount = 0;
try {
rowCount = dao.update(dto);
} catch (SQLException e) {
System.out.println("> EditHandler.process() Exception...");
e.printStackTrace();
}
conn.close();
String location = "/jspPro/board/view.do?seq="+ seq;
if( rowCount == 1) {
location +="&edit=success";
}else {
location +="&edit=fail";
}
response.sendRedirect(location);
} //
return null;
}
}
edit.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/include.jspf"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 8. ์คํ 3:28:25</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table, td, th {
border: solid 1px gray;
}
table {
border-spacing: 3px;
border-collapse: separate;
}
table, tr, td {
/* border-radius: 3px; */
/* padding:3px; */
}
</style>
</head>
<body>
<h3>edit.jsp</h3>
<div align="center">
<h2>๊ธ ์์ </h2>
<form method="post">
<table style="padding: 2px; width: 600px">
<tr>
<td colspan="2" align="center"><b>๊ธ์ ์์ ํฉ๋๋ค.</b></td>
</tr>
<tr>
<td align="center">์ด๋ฆ</td>
<td><input type="text" name="writer" size="15" value="${ dto.writer }" disabled="disabled"></td>
</tr>
<tr>
<td align="center">๋น๋ฐ๋ฒํธ</td>
<td><input type="password" name="pwd" size="15" required="required"></td>
</tr>
<tr>
<td align="center">Email</td>
<td><input type="email" name="email" size="50" value="${ dto.email }"></td>
</tr>
<tr>
<td align="center">์ ๋ชฉ</td>
<td><input type="text" name="title" size="50" required="required" value="${ dto.title }" autofocus="autofocus"></td>
</tr>
<tr>
<td align="center">๋ด์ฉ</td>
<td><textarea name="content" cols="50" rows="10">${ dto.content }</textarea></td>
</tr>
<tr>
<td align="center">HTML</td>
<td>
<input type="radio" name="tag" value="1" >์ ์ฉ
<input type="radio" name="tag" value="0">๋น์ ์ฉ
<script>
$(":radio[name=tag][value=${ dto.tag }]").attr("checked", "checked");
</script>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="์์ฑ ์๋ฃ">
<input type="button" onclick="history.back()" value="์ด์ ์ผ๋ก">
</td>
</tr>
</table>
</form>
</div>
<script>
$(function (){
$("form").submit(function (){
alert("์ ํจ์ฑ ๊ฒ์ฌ ์ฒดํฌ ํ๋ค. ");
// event.preventDefault();
// return false;
});
});
</script>
</body>
</html>
deleteHandler.java
package days09.mvc.command;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.util.ConnectionProvider;
import com.util.DBConn;
import days05.board.BoardDAOImpl;
public class DeleteHandler implements CommandHandler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
String requestMethod = request.getMethod();
if(requestMethod.equals("GET")) {
return "/days09/board/delete.jsp";
}else if(requestMethod.equals("POST")) {
request.setCharacterEncoding("UTF-8");
int seq = Integer.parseInt( request.getParameter("seq") );
String pwd = request.getParameter("pwd");
Connection conn = ConnectionProvider.getConnection();
BoardDAOImpl dao = new BoardDAOImpl(conn);
int rowCount = 0;
try {
String originalPwd = dao.getOriginalPwd(seq);
if( originalPwd.equals(pwd) ) {
rowCount = dao.delete(seq);
}else {
request.setAttribute("error", "๋น๋ฐ๋ฒํธ๊ฐ ํ๋ฆฝ๋๋ค.");
return "/days09/board/delete.jsp";
}
} catch (SQLException e) {
System.out.println("> DeleteHandler.process() Exception...");
e.printStackTrace();
}
conn.close();
String location = "/jspPro/board/list.do";
if( rowCount == 1) {
location +="?delete=success";
}else {
location +="?delete=fail";
}
response.sendRedirect(location);
} // if
return null;
}
}
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/include.jspf" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 9. ์คํ 3:38:00</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
table , td, th {
border:solid 1px gray;
}
table{
border-spacing: 3px;
border-collapse: separate;
}
table, tr, td {
/* border-radius: 3px; */
/* padding:3px; */
}
table{
width: 600px;
}
</style>
</head>
<body>
<h3>delete.jsp</h3>
<div align="center">
<h2>์ญ์ ํ๊ธฐ</h2>
<form method="post">
<table>
<tr>
<td colspan="2" align="center"><b>๊ธ์ ์ญ์ ํฉ๋๋ค</b></td>
</tr>
<tr>
<td align="center">๋น๋ฐ๋ฒํธ</td>
<td>
<input type="password" name="pwd" size="15" autofocus="autofocus">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="์ญ์ ">
<input type="button" id="cancel" value="์ทจ์">
</td>
</tr>
</table>
</form>
</div>
<script>
<%
String pwdError = (String)request.getAttribute("error");
if( pwdError != null){
%>
alert( '<%= pwdError %>');
<%
}
%>
</script>
</body>
</html>
1. days09
ใด board
ใด list.jsp
๊ธ์ฐ๊ธฐ ๋งํฌ ํ๊ทธ href ์์ฑ ํ์ธ/์์
<a href="<%= contextPath %>/cstvsboard/write.htm">๊ธ์ฐ๊ธฐ</a>
==> <a href="<%= contextPath %>/board/write.do">๊ธ์ฐ๊ธฐ</a>
2. ํ์ธ/์ฝ๋ฉ~
[MVC ์ฒ๋ฆฌ ๊ณผ์ ์ดํด ]
/board/write.do => MV[C] doGet(){}
(1) WriteHandler ํด๋์ค ์์ฑ
(2) commandHandler.properties ํ์ธ/์์ /์ถ๊ฐ
/board/write.do=days09.mvc.command.WriteHandler
(3) WriteHandler.process() ๊ตฌํ
- ๋ก์ง ์ฒ๋ฆฌ ๋ด๋น
- ( days05.board.Write.java ์๋ธ๋ฆฟ )
doGet(){
write.jsp ํ์ด์ง๋ก ํฌ์๋ฉ.
}
doPost(){
BoardDTO dto;
DB insert()
list.htm ๋ฆฌ๋ค์ด๋ ํธ
}
( ๋ฌธ์ ์ ) ํ๋์ process() ๋ฉ์๋์์ doget(), dopost() ๊ธฐ๋ฅ์ ๋ชจ๋
ํด์ผ ๋๋ ๋ฌธ์ ์ ..
ํด๊ฒฐ์ ์๋ ์ฝ๋ฉ์ผ๋ก ํด๊ฒฐ~~
if(requestMethod.equals("GET")) {
}else if(requestMethod.equals("POST")) {
}
[๊ฒ์๊ธ ์์ธ๋ณด๊ธฐ(View.jsp)]
1. ํ์ธ/[์์ ]/์ถ๊ฐ
๋ณด๊ณ ์ ํ๋ ๊ฒ์๊ธ์ ์ ๋ชฉ์ ํด๋ฆญํ๋ฉด ์์ฒญ
http://localhost/jspPro/cstvsboard/view.htm
?
seq=381¤tpage=1&searchCondition=1&searchWord=
list.jsp
/board/view.do ์์
2. ํ์ธ/์์ /์ถ๊ฐ
(1) /board/view.do= ViewHandler ํด๋์ค ํ์ธ
process(){} View.java ๋ณต์ฌ ๋ถ์ฌ์ ์์ .
(2) commandHandler.properties ํ์ผ ํ์ธ/์์ .
[๋ด์ฉ๋ณด๊ธฐ] view.jsp
[์์ ํ๊ธฐ] ***
1. <a class="btn btn-secondary" href="<%= contextPath %>/board/edit.do?seq=${ dto.seq }" id="editLink">์์ ํ๊ธฐ</a>
2. /board/edit.do => EditHandler.java ํ์ธ/์ถ๊ฐ
WriteHandler.java ๋ณต์ฌ ๋ถ์ด๊ธฐ..
commandHandler.properties ํ์ผ ํ์ธ/์์ .
[๋ด์ฉ๋ณด๊ธฐ] view.jsp
[๋ชจ๋ฌ์ฐฝ์ผ๋ก ๊ธ ์ญ์ ] ํด๋ฆญํ๋ฉด
[์ญ์ ๋ชจ๋ฌ์ฐฝ]
๋น๋ฐ๋ฒํธ ์ ๋ ฅ ํ [์ญ์ ]๋ฒํผ ํด๋ฆญ
1) /board/delete.do ์์ฒญ
view.jsp ์ด์ด์ ๋ชจ๋ฌ div ํ์ธ..
<form method="post" action="<%= contextPath%>/board/delete.do?seq=${ param.seq}"> ์์ .
2) DeleteHandler.java ์ถ๊ฐ.
commandHandler.properties ํ์ผ ํ์ธ/์์ .
p 544 Chapter 19 ํํฐ( Filter ) -- [ ํ๋ก์ ํธ ํ ๋ ์ฌ์ฉ ๊ธฐ์ ]
- ํํฐ
- Filter ๊ตฌํ
- ๋ก๊ทธ์ธ ๊ฒ์ฌ ๋ฑ๋ฑ ํํฐ ์์ฉ.
์)
list.do -> ์์ฒญ -> jsp/servlet
์ธ์ฆ
๊ถํ
ํํฐ๋ฅผ ์ฌ์ฉํด์ ์ฒดํฌ
<-์๋ต<- [์๋ต๋ฐ์ดํฐ]
xml ๋ณํ ์์ผ์ ์๋ต
์ํธํ ์์ผ์ ์๋ต
- ํํฐ ์ฒด์ธ : ์ฌ๋ฌ ๊ฐ์ ํํฐ๊ฐ ๋ชจ์ฌ์ ํ์ฑ๋ ๊ฒ.
[- ํํฐ ๊ตฌํ : ํต์ฌ 3๊ฐ ํ์ ]
1) javax,servlet.Filter ์ธํฐํ์ด์ค
init() ํํฐ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ํธ์ถ๋๋ ๋ฉ์๋ , ์ด๊ธฐํ.
*** doFilter() : ์ธ์ฆ/๊ถํ
ํํฐ ๊ธฐ๋ฅ์ ํ๋ ๋ฉ์๋ + ๋ค์ ํํฐ / ์ต์ข ์์(jsp/servlet ) ์ฒด์ธ
destory() ํํฐ ๊ฐ์ฒด๊ฐ ์๋ฉธ๋ ๋ ํธ์ถ๋๋ ๋ฉ์๋, ๋ฆฌ์์ค ๋ฐํ
2) javax.servlet.ServletRequestWrapper ํด๋์ค
- request(์์ฒญ)-> ์์ฒญ ํ๋ผ๋ฏธํฐ ๋ณ๊ฒฝ
3) javax.servlet.ServletResponseWrapper ํด๋์ค
- ์๋ต ๋ณ๊ฒฝ..
- ํํฐ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด
ํฐ์บฃ\lib\servlet-api.jar ํ์ผ์ ํด๋์ค ํจ์ค์ ์ถ๊ฐํด์ผ ๋์ง๋ง
์ดํด๋ฆฝ์ค๋ฅผ ์ฌ์ฉํด์ ๊ฐ๋ฐํ ๊ฒฝ์ฐ์๋ ์ถ๊ฐํ ํ์๊ฐ ์๋ค.
[ ํํฐ ์ค์ ํ๋ 2๊ฐ์ง ๋ฐฉ๋ฒ]
1) web.xml ํํฐ ์ค์
2) @WebFilter ์ ๋ ธํ ์ด์ ์ ์ฌ์ฉ.
p 552 ๊น์ง ์ ๋ฆฌ~~
[ ์ค์ต ์์ ] ์ถ๊ฐ ์ค๋ช
ex04.jsp
ex04_ok.jsp
// ๋ด์ผ ์ค์ ์ค์ต ์์ ( ์ธ์ฆ, ๊ถํ )
// Chapter 20 ServletContextListener
// Chapter 15 ๋ฐฉ๋ช ๋ก MVC ํจํด ๊ตฌํ
์์)
<!--
web_days09_mvc.xml ๋ณต์ฌ ๋ถ์ด๊ธฐ
ํํฐ ์์ฑ - post ์์ฒญ/ ์ธ์ฝ๋ฉ ์ค์ .
1. com.filter.CharacterEncondingFilter ํํฐ ํด๋์ค ์ถ๊ฐ.
-->
<form action="ex04_ok.jsp" method="post">
ํ๊ธ ์ด๋ฆ : <input type="text" name="name" value="ํ๊ธธ๋"><br>
๋์ด : <input type="text" name="age" value="20"><br>
<input type="submit">
</form>
<%
String name = request.getParameter("name");
String age = request.getParameter("age");
%>
์ ๋ฌ๋ ์ด๋ฆ : <%= name %><br> <!-- íยย길ëยย post ISO 8859-1 ์ธ์ฝ๋ฉ -->
์ ๋ฌ๋ ๋์ด : <%= age %><br>
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharacterEncodingFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response
, FilterChain chain)
throws IOException, ServletException {
System.out.println("> CharacterEncodingFilter.doFilter() ํธ์ถ๋จ.");
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
'๐จโ๐ป Web Development > JDBC | JSP | Servlet' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day10] JSP/Servlet 10 - (0) | 2022.12.19 |
---|---|
[Day8] JSP/Servlet 8 - ์ธ์ +, EL, EL์ฟ ํค, JSTL (0) | 2022.12.17 |
[Day7] JSP/Servlet 7 - ์ฟ ํค, ์ธ์ , ์ปค๋ฅ์ ํ (0) | 2022.12.16 |
[Day6] JSP/Servlet 6 - ๊ฒ์ํ (0) | 2022.12.15 |
[Day5] JSP/Servlet 5 - JSP 4scope, ์๋ฌํ์ด์ง, ๋ชจ๋ํ(include), ์๋ฐ๋น(javabean) (0) | 2022.12.15 |
์ต๊ทผ๋๊ธ