为什么requestscope属性同时为null和notnull?

c7rzv4ha  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(403)

为什么requestscope同时为null和not null?这是index.jsp文件代码的一部分

<c:if test="${!pageContext.request.servletPath.equals('/Login')}">

    <c:if test = "${requestScope.Cars!=null}">
        <%
            List<Car>  cars = (ArrayList<Car>)request.getAttribute("Cars");
            System.out.println(cars.get(0));
        %>
    </c:if>

    <c:if test="${requestScope.Cars==null}">
        <%
            System.out.println(request.getAttribute("Cars")+" test");
        %>
        <jsp:forward page="/AllCarCategories"/>
    </c:if>

</c:if>

第一个if语句只是检查它如何同时为null和notnull
这就是所有CarCategories的外观

@WebServlet(name = "AllCarCategories", urlPatterns = {"/AllCarCategories","/Login/AllCarCategories"})
public class AllCarCategories extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MySQLDAOFactory dao =(MySQLDAOFactory) DAOFactory.getDAOFactory(1);
        MySQLCarDao carDao = (MySQLCarDao) dao.getCarDao();
        MySQLCarCategoryDao categoryDao = (MySQLCarCategoryDao)dao.getCarCategoryDao();
        List<CarCategory> carCategories = categoryDao.findAllCarC();
        List<Car> cars = carDao.findAllCars();
        System.out.println(carDao.findAllCars().size()+" size ");
        request.setAttribute("Cars",cars);
        request.setAttribute("Categories",carCategories);
        request.setAttribute("ImageMan", ImageManager.getInstance());
        System.out.println("request url:" +request.getRequestURL()+" requst servlet Path"+request.getServletPath());
        request.getRequestDispatcher("index.jsp").forward(request,response);

    }

}

所以,第一次当它的null时,我把它转发给allcarcategories,并创建了car对象列表,然后我把它转发给jsp文件,从逻辑上讲,它不能为null,因为我创建了一个car对象列表,但它是,另一个谜是,它同时在if语句中输入null和NOTNULL
这就是控制台输出的样子(只是为了显示那里发生了什么)

它怎么可以同时为空和不为空?谢谢

wlsrxk51

wlsrxk511#

在第一次打印之前添加其他打印 if 测试 null (最终一秒又一秒 if )像

<c:if test="${!pageContext.request.servletPath.equals('/Login')}">
    <%
        System.out.println("new login");
    %>

    <c:if test = "${requestScope.Cars!=null}">

(只是为了显示更多的信息,不是真正的答案,我不开发jsp)

相关问题