[쉽게 배우는 JSP 웹 프로그래밍] 18장 정리

웹 MVC에서는 클라이언트로부터 컨트롤러인 서블릿을 통해 요청을 받으며, 이를 위해 web.xml에 서블릿 클래스와 웹 브라우저의 요청 url 패턴을 등록해야 한다. 컨트롤러는 뷰와 모델 간의 인터페이스 역할을 하며, 모델은 웹 애플리케이션의 비즈니스 로직을 포함하는 데이터다. 이러한 MVC 패턴 구현 방법을 코드를 통해 이해할 수 있다.
DriedPollack's avatar
Mar 06, 2024
[쉽게 배우는 JSP 웹 프로그래밍] 18장 정리

MVC 패턴 구현 방법

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app> <security-role> <role-name>admin</role-name> </security-role> <security-constraint> <web-resource-collection> <web-resource-name>JSPBook</web-resource-name> <url-pattern>/ch10/practice/practice04.jsp</url-pattern> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <description></description> <role-name>admin</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <form-login-config> <form-login-page>/ch10/practice/practice04_login.jsp </form-login-page> <form-error-page>/ch10/practice/practice04_login_failed.jsp </form-error-page> </form-login-config> </login-config> <!-- <error-page> <error-code>500</error-code> <location>/ch11/errorCode_error.jsp</location> </error-page> <error-page> <exception-type>javax.servlet.ServletException</exception-type> <location>/ch11/practice/practice05_error.jsp</location> </error-page> --> <filter> <filter-name>Filter01</filter-name> <filter-class>ch12.AuthenFilter</filter-class> </filter> <filter-mapping> <filter-name>Filter01</filter-name> <url-pattern>/ch12/filter01.process.jsp</url-pattern> </filter-mapping> <filter> <filter-name>Filter02</filter-name> <filter-class>ch12.InitParamFilter</filter-class> <init-param> <param-name>param1</param-name> <param-value>admin</param-value> </init-param> <init-param> <param-name>param2</param-name> <param-value>1234</param-value> </init-param> </filter> <filter-mapping> <filter-name>Filter02</filter-name> <url-pattern>/ch12/filter02_process.jsp</url-pattern> </filter-mapping> <filter> <filter-name>Filter02_2</filter-name> <filter-class>ch12.LogFileFilter</filter-class> <init-param> <param-name>filename</param-name> <param-value>c:\\logs\\monitor.log</param-value> </init-param> </filter> <filter-mapping> <filter-name>Filter02_2</filter-name> <url-pattern>/ch12/filter02_process.jsp</url-pattern> </filter-mapping> <filter> <filter-name>loginFilter</filter-name> <filter-class>ch12.practice.LoginCheckFilter</filter-class> </filter> <filter-mapping> <filter-name>loginFilter</filter-name> <url-pattern>/ch12/practice/practice04_success.jsp</url-pattern> </filter-mapping> <servlet> <servlet-name>myController</servlet-name> <servlet-class>ch18.ControllerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myController</servlet-name> <url-pattern>/ch18/ControllerServlet</url-pattern> </servlet-mapping> </web-app>

LoginBean.java

package ch18; public class LoginBean { private String id; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean validate() { if(password.equals("admin")) return true; else return false; } }

ControllerServlet.java

package ch18; 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 ControllerServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html; charset=utf-8"); String id = req.getParameter("id"); String password = req.getParameter("passwd"); LoginBean bean = new LoginBean(); bean.setId(id); bean.setPassword(password); req.setAttribute("bean", bean); boolean status = bean.validate(); if (status) { RequestDispatcher rd = req.getRequestDispatcher("mvc_success.jsp"); rd.forward(req, resp); } else { RequestDispatcher rd = req.getRequestDispatcher("mvc_error.jsp"); rd.forward(req, resp); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } }

mvc.jsp

<%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>MVC</title> </head> <body> <form method="post" action="ControllerServlet"> <p> 아이디 : <input type="text" name="id"> <p> 비밀번호 : <input type="text" name="passwd"> <p><input type="submit" value="전송"> </form> </body> </html>

mvc_success.jsp

<%@ page contentType="text/html; charset=utf-8"%> <%@ page import="ch18.LoginBean"%> <html> <head> <title>MVC</title> </head> <body> <p>로그인 성공했습니다 <p> <% LoginBean bean = (LoginBean) request.getAttribute("bean"); out.print("아이디: " + bean.getId()); %> </body> </html>

mvc_error.jsp

<%@ page contentType="text/html; charset=utf-8"%> <html> <head> <title>MVC</title> </head> <body> <p> 아이디와 비밀번호를 확인해주세요 <%@include file="mvc.jsp"%> </body> </html>
 

핵심 키워드

  • 웹 MVC에서는 클라이언트로부터 컨트롤러인 서블릿을 통해 요청을 받아야 한다. 이흘 위해 web.xml에 서블릿 클래스와 웹 브라우저의 요청 url 패턴을 등록해야 한다.
  • 컨트롤러는 뷰와 모델 간의 인터페이스 역할을 하여 웹 브아우저의 모든 요청 URL을 받아들이고 요청 URL과 함께 전달되는 요청 파라미터를 받아 처리하는 서블릿 클래스다.
  • 모델은 웹 애플리케이션의 비즈니스 로직을 포함하는 데이터로 웹 애플리케이션의 상태를 나타낸다.
 

결론

해당 코드를 작성하면서 MVC의 개념, MVC 패턴 구조, MVC 패턴 구현 방법을 이해할 수 있었다.
Share article

More articles

See more posts
RSSPowered by inblog