来自Head First Servlet and JSP

XML语言: web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

version="2.4"> -->

Ch3 Beer

com.example.web.BeerSelect

Ch3 Beer

/SelectBeer.do

HTML语言: form.html

Beer Selection Page

Beer Selection Page

select beer characteristics

Color:

Java语言: BeerSelect

package com.example.web;

import com.example.model.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class BeerSelect extends HttpServlet

{

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

// response.setContentType("text/html");

// PrintWriter out = response.getWriter();

// out.println("Beer Selection Advice
");

String c = request.getParameter("color");

// out.println("
Got beer color " + c);

BeerExpert be = new BeerExpert();

List result = be.getBrands(c);

// Iterator it = result.iterator();

// while (it.hasNext())

// {

// out.print("
try: " + it.next());

// }

request.setAttribute("styles", result);

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

view.forward(request, response);

}

}

Java语言: BeerExpert

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");

brands.add("Gout Stout");

}

return brands;

}

}

Java Server Page语言: result.jsp

<%@ page import="java.util.*"%>

Beer Recommendations JSP

Beer Recommendations JSP

<% List sytles = (List)request.getAttribute("styles"); Iterator it = sytles.iterator(); while (it.hasNext()) { out.print("
try: " + it.next()); } %>


原文链接: https://blog.alswl.com/2009/02/servlet-jsp-s-java-type-of-the-most-simple-mvc-model/
3a1ff193cee606bd1e2ea554a16353ee
欢迎关注我的微信公众号:窥豹

Comments

comments powered by Disqus