创建和部署MVC Web应用:
第五步:部署描述文件(web.xml)
HTML表单、servlet控制器、模型(java类)、XML部署文件、JSP视图
第一步:构建用户最初请求的HTML表单
<
html
>
< body >
< h1 align ="center" >
Beer Selection 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>
< body >
< h1 align ="center" >
Beer Selection 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>
第二步:创建模型BeerExpert
package com.zyun.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);
}
}
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);
}
}
第三步:创建控制器BeerSelect
package com.zyun.web;
import java.io.IOException;
import java.util.List;
import com.zyun.model.BeerExpert;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String c = request.getParameter( "color");
BeerExpert be = new BeerExpert();
List result = be.getBrands(c);
request.setAttribute( "styles", result);
RequestDispatcher view = request.getRequestDispatcher( "result.jsp");
view.forward(request, response);
}
}
import java.io.IOException;
import java.util.List;
import com.zyun.model.BeerExpert;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String c = request.getParameter( "color");
BeerExpert be = new BeerExpert();
List result = be.getBrands(c);
request.setAttribute( "styles", result);
RequestDispatcher view = request.getRequestDispatcher( "result.jsp");
view.forward(request, response);
}
}
第四步:创建视图result.jsp
<
%@ page
import
="java.util.*"%
>
< html >
< body >
< h1 aglin ="center" >Beer Recommendations JSP < h1 >
< p >
<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while(it.hasNext()){
out.println(" < br >try: "+it.next());
}
%>
</boyd>
</html>
< html >
< body >
< h1 aglin ="center" >Beer Recommendations JSP < h1 >
< p >
<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while(it.hasNext()){
out.println(" < br >try: "+it.next());
}
%>
</boyd>
</html>
<?
xml
version
="1.0"
encoding
="ISO-8859-1"
?>
< web-app xmlns ="http://java.sun.com/xml/ns/javaee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version ="2.5" >
< servlet >
< servlet-name >Beer </ servlet-name >
< servlet-class >com.zyun.web.BeerSelect </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name >Beer </ servlet-name >
< url-pattern >/SelectBeer.do </ url-pattern >
</ servlet-mapping >
</ web-app >
< web-app xmlns ="http://java.sun.com/xml/ns/javaee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version ="2.5" >
< servlet >
< servlet-name >Beer </ servlet-name >
< servlet-class >com.zyun.web.BeerSelect </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name >Beer </ servlet-name >
< url-pattern >/SelectBeer.do </ url-pattern >
</ servlet-mapping >
</ web-app >
整个MVC应用的执行过程:
![](../p_w_upload/201007/201003221269233119874.jpg)
转载于:https://blog.51cto.com/bitzyun/346496