웹브라우져에 사용자 취향을 묻고 거기에 대한 맥주를 추천해주 는 프로그램이다. 사용자가 브라우져를 통해 Index.html 안에 있는 옵션을 선택하 여 Servlet 에게 내용이 보내진다.Servlet 에선 받은 내용을 Model 에서 처리하며 결과를 얻어 View 즉 Result.Jsp 로 보내지게된다.Result 에선 사용자에게 선택한 옵션에 대한 결과값을 출력하게 된다. 이것이 MVC 패턴을 간단하게 응용한 프로그램이라고 할수있다. Index소스 보기
<!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>Insert title here</title> </head> <body> <h1 align="center">Beer Slection page</h1> <form method="POST" action="SelectBeer.do">Select beer characteristics <p>Color: <select name="color" size="1"> <option>light <option>amber <option>brown <option>dark </select> <br> <br> <center><input type="SUBMIT"></center> </form> </body> </html>
Servlet 소스 보기
import model.*; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class for Servlet: Servlet3 * */ public class Servlet3 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#HttpServlet() */ public Servlet3() { super(); } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /* (non-Java-doc) * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String c = request.getParameter("color"); model1 mo = new model1(); List result = mo.getBrands(c); request.setAttribute("styles", result); RequestDispatcher view = request.getRequestDispatcher("result.jsp"); view.forward(request, response); } }
Model 소스 보기
import java.util.*; public class model1 { 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); } }
Result 소스 보기
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ page import = "java.util.*" %> <!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>Insert title here</title> </head> <body> <h1 align = "center">Beer Recommendations JSP<h1> <p> <% List stlyes = (List) request.getAttribute("styles"); Iterator it = stlyes.iterator(); while(it.hasNext()){ out.print("<br>try: " + it.next()); } %> </body> </html>
소스파일 크리에이티브 커먼즈 라이선스
http://dynast.tistory.com/trackback/149