[HeadFirst-JSPServlet学习笔记][第三章:实战MVC]

第三章 实战MVC

J2EE如何集成一切

  • Java2企业版(Java 2 Enterprise Editon,J2EE)是一种超级规范。规定了servlets2.4,JSP2.0,EJB2.1(Enterprise JavaBean)
  • Web容器用于Web组件(servlet和JSP)。EJB容器用于业务组件。

  • 一个完全兼容的J2EE应用服务器必须有Web容器和一个EJB容器

  • Tomcat是一个Web容器

[HeadFirst-JSPServlet学习笔记][第三章:实战MVC]_第1张图片

构建一个真正的(小)应用

  1. 分析用户视图
  2. 创建用于开发这个项目的开发环境
  3. 创建用于部署这个项目的部署环境(Eclipse环境:http://www.runoob.com/jsp/eclipse-jsp.html)
  4. 对Web应用的各个组件完成迭代的开发和测试。

第4步又分为5个小步骤

  1. 构建和测试用户最初请求的HTML表单
  2. 构建控制器servlet的第一个版本,并用前面的HTML表单测试这个控制器。这个版本通过HTML表单来调用,并打印它接受到的参数
  3. 为专家/模型类构建一个测试类,然后构建并测试专家/模型类本身。
  4. 把servlet 升级到第二版,这个版本增加一个功能,可以调用模型类获得啤酒建议。
  5. 构建JSP,把servlet升级到第三版,然后再测试整个应用

1.表单HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Project2</title>
</head>
<body>
<h1 align="center">Beer Selection Page</h1>
<form method="post"
 action="SelectBeer.do">
 <p> 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>

selectbeer.do只是一个逻辑名,在XML部署文件中会映射到一个servlet文件

2.创建部署描述文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <servlet>
    <servlet-name>Ch3 Beer</servlet-name>
    <servlet-class>com.example.web.BeerSelect</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Ch3 Beer</servlet-name>
    <url-pattern>/SelectBeer.do</url-pattern>
  </servlet-mapping>
</web-app>

3.部署测试控制器 第一版servlet

package com.example.web;
import java.io.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Servlet implementation class BeerSelect
 */
@WebServlet("/BeerSelect")
public class BeerSelect extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public BeerSelect() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");//确定返回的类型
        PrintWriter out = response.getWriter();
        out.println("Beer Selection Advice<br>");
        String c = request.getParameter("color");//得到请求中name为color的字符串
        out.println("<br>Got beerH color"+ c);
    }

}

4.构建和测试模型类

  • model 必须有自己的工具包,因为它不知道自己会被servlet调用。

    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");
                brans.add("Gout Stout");
            }
            return brands;
        }
    }
    

5.构建和部署第二版servlet

调用model

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    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 Advise=be.getBrands(c);
    for(Iterator it = Advise.iterator();it.hasNext();)
    {
        out.print("<br>try:"+it.next());
    }
}

6.构建jsp

<%@ page import="java.util.*" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<p>
<%
    List styles=(List)request.getAttribute("styles");
    Iterator it = styles.iterator();
    while(it.hasNext())
    {
    out.print("<br>try: "+it.next());
    }
%>
</p>
</body>
</html>

7.第三版servlet

package com.example.web;
import java.io.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import javax.servlet.http.*;
import com.example.model.*;
import java.util.*;
/**
 * Servlet implementation class BeerSelect
 */
@WebServlet("/BeerSelect")
public class BeerSelect extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public BeerSelect() {
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //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 Advise=be.getBrands(c);
        request.setAttribute("styles",Advise);

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

    }

}

值此一个MVC程序开发完毕

Eclipse坑点:

eclipse 写servlet 有几个坑点

  1. servlet代码放src里,而不是放web的src里。
    编译出来的class在build里

  2. 改了java文件 记得clean刷新Tomat的缓存

这本书看不下去了

你可能感兴趣的:(java,tomcat,应用服务器,mvc,servlet)