[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 }&currentpage=${ 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"> &raquo; </a>
             --%>  
     
             <c:if test="${ pageBlock.prev }">
                  <a href="<%= contextPath %>/cstvsboard/list.htm?currentpage=${  pageBlock.startOfPageBlock - 1  }"> &laquo; </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  }"> &raquo; </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="์ž‘์„ฑ ์™„๋ฃŒ">
				  &nbsp;&nbsp;&nbsp; 
				  <input type="reset" value="๋‹ค์‹œ ์ž‘์„ฑ">
				  &nbsp;&nbsp;&nbsp; 
				  <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="์ž‘์„ฑ ์™„๋ฃŒ">
				  &nbsp;&nbsp;&nbsp;				  
				  <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="์‚ญ์ œ">&nbsp;&nbsp;
	      <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&currentpage=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() { 
		
	}
}

 

 

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ