request.getAttribute在转发后返回null

问题描述 投票:0回答:1

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(这是我遇到问题的地方。在下面的代码中,“styles”为null):

<%@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。

为什么会这样?有人可以为我提供解决方案吗?

jsp servlets
1个回答
0
投票

确实看起来像是在BeerExpert.getBrands()方法中引发异常。这是啤酒清单可能空的唯一方式。

肯定会调用servlet,因为我们看到转发的JSP的输出。

因此看起来好像列表是空的。只有在抛出异常时才会发生这种情况。我看到的唯一可能的原因是“color”参数是否为null。

您是否能够将调试器连接到tomcat Java进程?如果是这样,单步执行servlet代码将很快给出答案。

© www.soinside.com 2019 - 2024. All rights reserved.