form.html(这是原始表单):
<html>
<head>
<title>Beer Advisor</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Beer Selection Page</h1>
<form action="BeerSelect.do" method="POST">
Select bear characteristics
<p>
Color:
<select name="color" size="1">
<option value="light">light</option>
<option value="amber">amber</option>
<option value="brown">brown</option>
<option value="dark">dark</option>
</select>
<br><br>
<input type="SUBMIT" style="alignment-adjust: central">
</form>
</body>
</html>
Servlet(在原始表单上单击“提交查询”后,控制权转移到 servlet。servlet 添加一个 List 对象,它从 BeerExpert java 对象中获取,并将其添加到请求中。然后将请求对象转发到JSP):
package com.example.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.model.*;
import java.util.*;
import javax.servlet.RequestDispatcher;
public class BeerSelect extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try {
/* TODO output your page here. You may use following sample code. */
String c = request.getParameter("color");
BeerExpert be = new BeerExpert();
List brands = be.getBrands(c);
request.setAttribute("styles", brands);
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);
} finally {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
JSP(这是我遇到问题的地方。在下面的代码中,“样式”为空):
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beer Advisor</title>
</head>
<body>
<h1 style="alignment-adjust: central">Beer Recommendations JSP</h1>
<p>
<%
List styles = (List)request.getAttribute("styles");
if(styles != null) {
Iterator it = styles.iterator();
while (it.hasNext()) {
out.println("<br>try: " + it.next());
}
}
%>
</p>
</body>
</html>
BeerExpert.java(这个类由 Servlet 访问):
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.model;
import java.util.*;
/**
*
* @author
*/
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);
}
}
以下是我的 JSP 输出:
Beer Recommendations JSP
我正在使用 Apache Tomcat。
为什么会这样?有人可以为我提供解决方案吗?