[커맨드패턴]


- 서블릿에서 사용자의 요청을 명령어 전달

- 사용자가 어떤 요청을 했는지 판단하기 위한 가장 일반적인 방법이 명령어로 사용자의 요청을 전달하는 것이다.
  예를 들어, 글목록 명령, 글삭제 명령

- 요청 파라미터로 명령어를 전달하는 방법
  http://localhost:9090/mvc/servlet/Controller?command=Message

- 요청 URI자체을 명령어로 사용하는 방법
  http://localhost:9090/mvc/control/test.do


예제)

/ch1/loginForm.jsp--------------
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<TITLE> ch1/loginForm.jsp</TITLE>
</HEAD>

<BODY>

<form name="frm" method="post" action="../control.do">
   아이디 : <input type="text" name="id" /><br/>
   비번 : <input type="password" name="pw" /><br/>
   <input type="submit" value="확인" />
</form>

</BODY>
</HTML>


ControllerServlet.java----------
package ch1;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ControllerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
       
        String id=HangulConversion.toKor(req.getParameter("id"));       
        String pw=HangulConversion.toKor(req.getParameter("pw"));       
        resp.setContentType("text/html;charset=euc-kr");
        PrintWriter out=resp.getWriter();
        out.write("<html>");
        out.write("<body>");
        out.write("아이디 :"+id);
        out.write("<br/>");
        out.write("비번 : "+pw);
        out.write("</body>");       
        out.write("</html>");
        out.close();
               
    }
}


/WEB-INF/web.xml------------
  <servlet>
    <servlet-name>control</servlet-name>
    <servlet-class>ch1.ControllerServlet</servlet-class>
  </servlet>
 
  <servlet-mapping>
     <servlet-name>control</servlet-name>
     <url-pattern>/control.do</url-pattern>
  </servlet-mapping>


한글처리))))
HangulConversion.java---------
package ch1;

public class HangulConversion {
    public static String toEng (String ko)  {
        if (ko == null) {
            return null;
        }
       
        try {
            return new String(ko.getBytes("euc-kr"),"8859_1");
        } catch(Exception e) {
            return ko;
        }
    }

    public static String toKor (String en) {
        if (en == null) {
            return null;
        }
        try {
                return new String (en.getBytes("8859_1"), "euc-kr");
        } catch(Exception e) {
            return en;
        }
    }
}

============================

예제)))
http://localhost:9090/mvc/form.do 명령어에 의해  loginForm.jsp 호출

FormController.java-------------
package ch2;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FormController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
       
        RequestDispatcher dispatcher=req.getRequestDispatcher("ch2/loginForm.jsp");
        dispatcher.forward(req, resp);
    }
}


/ch2/loginForm.jsp--------------
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<TITLE> ch2/loginForm.jsp</TITLE>
</HEAD>

<BODY>

<form name="frm" method="post" action="read.do">
   아이디 : <input type="text" name="id" /><br/>
   비번 : <input type="password" name="pw" /><br/>
   <input type="submit" value="확인" />
</form>

</BODY>
</HTML>

web.xml-------------------------
  <servlet>
    <servlet-name>form</servlet-name>
    <servlet-class>ch2.FormController</servlet-class>
 </servlet>
 
 <servlet-mapping>
   <servlet-name>form</servlet-name>
   <url-pattern>/form.do</url-pattern>
 </servlet-mapping>

 

ReadController.java----------------
package ch2;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch1.HangulConversion;

public class ReadController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
       
        String id=HangulConversion.toKor(req.getParameter("id"));       
        String pw=HangulConversion.toKor(req.getParameter("pw"));
       
         RequestDispatcher dispather=
                 req.getRequestDispatcher("ch2/read.jsp");
         req.setAttribute("id", id);
         req.setAttribute("pw", pw);
         dispather.forward(req, resp);
    }
}

/ch2/read.jsp------------------------
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<TITLE> ch2/read.jsp</TITLE>
</HEAD>

<BODY>

<p>당신의 아이디 : ${requestScope.id} 입니다</p>
<p>당신의 비번 : ${requestScope.pw} 입니다</p>

</BODY>
</HTML>


예제))))로그아웃
/ch2/loginForm.jsp-------------
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<TITLE> ch2/loginForm.jsp</TITLE>
</HEAD>

<BODY>
<!--
<form name="frm" method="post" action="read.do">
 -->
 
 <form name="frm" method="post" action="login.do">
   아이디 : <input type="text" name="id" /><br/>
   비번 : <input type="password" name="pw" /><br/>
 <input type="submit" value="확인" />
</form>

</BODY>
</HTML>


LoginProController.java----------------
package ch2;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginProController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
       
        RequestDispatcher dispather=req.getRequestDispatcher("ch2/logout.jsp");
        String id=req.getParameter("id");
        String pw=req.getParameter("pw");
        if(id.equals("user") && pw.equals("1234")){
            req.setAttribute("id", id);
            req.setAttribute("pw", pw);
            dispather.forward(req,resp);
        }else{
            resp.sendRedirect("ch2/loginForm.jsp");
        }
       
    }
}

/ch2/logout.jsp----------------------
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<TITLE> /ch2/logout.jsp</TITLE>
</HEAD>

<BODY>

<p>${requestScope.id} 님 로그아웃</p>

</BODY>
</HTML>


web.xml-----------------------
<servlet>
  <servlet-name>login</servlet-name>
  <servlet-class>ch2.LoginProController</servlet-class>
</servlet>

<servlet-mapping>
 <servlet-name>login</servlet-name>
 <url-pattern>/login.do</url-pattern>
</servlet-mapping>

 

 

 

 

 

 

+ Recent posts