yoursyun

mini MVC - jsp ( ref - head first servlet & jsp ) 본문

program/design pattern

mini MVC - jsp ( ref - head first servlet & jsp )

yoursyun 2008. 11. 11. 17:33

* 한빛 헤드 퍼스트 교재 참조
* 작업을 들어가기전 해당 교재나 또는 개발, 배포 디렉토리 설정 같은게 필요 합니다.
   (구길에 가믄 더 좋은 형태? 로 볼 수 있네염.... )

[ web.xml ]
* 실 적용시 주석은 삭제

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

 <servlet>
  <servlet-name>MVC Beer</servlet-name> <!-- 이 서블릿이름으로 매핑처리 하단 메핑과 동일 하다 -->
  <servlet-class>com.example.web.BeerSelect</servlet-class> <!-- 서블릿 클래스의 완전한 이름 -->
 </servlet>

 <servlet-mapping>
  <servlet-name>MVC Beer</servlet-name>
  <url-pattern>/SelectBeer.do</url-pattern> <!-- 클라이언트가 사용할 이름 .do 는 무의미 -->
 </servlet-mapping>
</web-app>

< Servlet 1단계 형태 >
[ BeerSelect.java ]

// 개발 환경, 배포 환경 디렉토리와 일치 해야 합니다.
package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class BeerSelect extends HttpServlet {
 public void doPost(HttpServletRequest request,
     HttpServletResponse response)
     throws IOException, ServletException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("Beer Selection Advice<br />");
  String c = request.getParameter("color");
  out.println("<br />Got beer color " + c);
 }
}

< Servlet 2단계 형태 > model 구현
[ BeerSelect.java ]
package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class BeerSelect extends HttpServlet {
 public void doPost(HttpServletRequest request,
     HttpServletResponse response)
     throws IOException, ServletException {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("Beer Selection Advice<br />");
  String c = request.getParameter("color");
  out.println("<br />Got beer color " + c);

  BeerExpert be = new BeerExpert();
  List result = be.getBrands(c);
  Iterator it = result.iterator();
  while(it.hasNext()) {
   out.println("<br />try: " + it.next());
  }
 }
}
[ BeerExpert.java ]
package com.example.model;

import java.util.*;

public class BeerExpert
{
 public List getBrands(String color)
 {
  List brands = new ArrayList();

  if (color.equals("amber"))
  {
   brands.add("Jack Amber");
   brands.add("Red Moose");
  }
  else {
   brands.add("Jail Pale Ale");
   brands.add("Gout Stout");
  }

  return(brands);
 }
}

< Servlet 3단계 형태 > jsp view 구현
[ BeerSelect.java ]
// 개발 환경, 배포 환경 디렉토리와 일치 해야 합니다.
package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class BeerSelect extends HttpServlet {
 public void doPost(HttpServletRequest request,
     HttpServletResponse response)
     throws IOException, ServletException {
  /* response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("Beer Selection Advice<br />");
  */
  
  String c = request.getParameter("color");
  
  // out.println("<br />Got beer color " + c);

  BeerExpert be = new BeerExpert();
  List result = be.getBrands(c);
  
  request.setAttribute("styles", result);

  RequestDispatcher view =
   request.getRequestDispatcher("result.jsp");

  view.forward(request, response);

  /* Iterator it = result.iterator();
  while(it.hasNext()) {
   out.println("<br />try: " + it.next());
  }  */
 }
}
[ result.jsp ]
<%@ page import="java.util.*" %>
<html>
<body>
<h1 align="center">Beer Recommendations JSP</h1>
<p>
<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while(it.hasNext()) {
out.print("<br>try: " + it.next());
}
%>
</body>
</html>

반응형