zoukankan      html  css  js  c++  java
  • 《Head First Servlets & JSP》-3-1st servlet MVC demo

    项目结构

    用户首页

    form.html

    1. <html>
    2. <body>
    3. <h1 align='center'>Beer Selection Page</h1>
    4. <form method="post"
    5. action="SelectBeer.do">
    6. Select
    7. <p>
    8. Color:
    9. <select name="color" size="1">
    10. <option value="light"> light</option>
    11. <option value="amber"> amber</option>
    12. <option value="brown"> brown</option>
    13. <option value="dark"> dark</option>
    14. <option value="red"> red</option>
    15. </select>
    16. <br/><br/>
    17. <center>
    18. <input type="submit">
    19. </center>
    20. </form>
    21. </body>
    22. </html>

    MVC之C——Servlet

    BeerSelect.java

    1. package com.example.web;
    2. import com.example.model.BeerExpert;
    3. import javax.servlet.RequestDispatcher;
    4. import javax.servlet.ServletException;
    5. import javax.servlet.http.HttpServlet;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. import java.io.IOException;
    9. import java.util.List;
    10. /**
    11. * Created by LTN on 2016/10/30.
    12. */
    13. public class BeerSelect extends HttpServlet {
    14. public void doGet(HttpServletRequest request,
    15. HttpServletResponse response)throws IOException ,ServletException{
    16. response.setContentType("text/html");
    17. String c = request.getParameter("color");
    18. BeerExpert expert = new BeerExpert();
    19. List result = expert.getBrands(c);
    20. request.setAttribute("styles", result);
    21. RequestDispatcher view = request.getRequestDispatcher("result.jsp");
    22. view.forward(request, response);
    23. }
    24. public void doPost(HttpServletRequest request,
    25. HttpServletResponse response)throws IOException,ServletException {
    26. doGet(request,response);
    27. }
    28. }

    MVC之M——模型

    BeerExpert.java

    1. package com.example.model;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. public class BeerExpert
    5. {
    6. public List getBrands(String color)
    7. {
    8. List<String> brands = new ArrayList<String>();
    9. if ( color.equals("amber") )
    10. {
    11. brands.add("Jack Amber");
    12. brands.add("Red Moose");
    13. }
    14. else
    15. {
    16. brands.add("Jail Pale Ale");
    17. brands.add("Gout Stout");
    18. }
    19. return (brands);
    20. }
    21. }

    MVC之V——JSP

    result.jsp

    1. <%@ page import="java.util.*" %>
    2. <html>
    3. <body>
    4. <h1 align="center">Beer Recommendations JSP</h1>
    5. <p>
    6. <%
    7. ArrayList styles = (ArrayList)request.getAttribute("styles");
    8. Iterator it = styles.iterator();
    9. while(it.hasNext()) {
    10. out.print("<br>try " + it.next());
    11. }
    12. %>
    13. </body>
    14. </html>

    配置文件

    web.xml

    1. <!DOCTYPE web-app PUBLIC
    2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
    4. <web-app>
    5. <display-name>Archetype Created Web Application</display-name>
    6. <servlet>
    7. <servlet-name>Ch3 Beer</servlet-name>
    8. <servlet-class>com.example.web.BeerSelect</servlet-class>
    9. </servlet>
    10. <servlet-mapping>
    11. <servlet-name>Ch3 Beer</servlet-name>
    12. <url-pattern>/SelectBeer.do</url-pattern>
    13. </servlet-mapping>
    14. </web-app>

    附件列表

    • 相关阅读:
      Mysql索引优化分析
      mysql学习
      linux安装mysql
      MVC实例应用模式
      模型-视图-控制器MVC模式
      设计模式理解
      XX系统质量属性战术
      XX系统可用性易用性
      属性常见属性场景
      架构漫谈读后感
    • 原文地址:https://www.cnblogs.com/myitroad/p/6192518.html
    Copyright © 2011-2022 走看看