《Head First Servlets & JSP》-3-1st servlet MVC demo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《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>

附件列表

     

    以上是关于《Head First Servlets & JSP》-3-1st servlet MVC demo的主要内容,如果未能解决你的问题,请参考以下文章

    《Head First Servlets & JSP》-5-属性和监听

    《Head First Servlets & JSP》-4-请求和响应

    《Head First Servlets & JSP》-10-定制标记开发

    《Head First Servlets & JSP》-6-会话管理

    《Head First Servlets & JSP》-1-前言

    《Head First Servlets & JSP》-8-无脚本的JSP