Java 为什么requestScope属性同时为null和not null?
为什么requestScope同时为null和not null? 这是index.jsp文件代码的一部分Java 为什么requestScope属性同时为null和not null?,java,jsp,tomcat,servlets,requestdispatcher,Java,Jsp,Tomcat,Servlets,Requestdispatcher,为什么requestScope同时为null和not null? 这是index.jsp文件代码的一部分 <c:if test="${!pageContext.request.servletPath.equals('/Login')}"> <c:if test = "${requestScope.Cars!=null}"> <% List<Car> cars =
<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);
}
}
@WebServlet(name=“AllCarCategories”,urlPatterns={”/AllCarCategories“,“/Login/AllCarCategories”})
公共类AllCarCategories扩展了HttpServlet{
@凌驾
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
MySQLDAOFactory dao=(MySQLDAOFactory)DAOFactory.getDAOFactory(1);
MySQLCarDao carDao=(MySQLCarDao)dao.getCarDao();
MySQLCarCategoryDao categoryDao=(MySQLCarCategoryDao)dao.getCarCategoryDao();
List carCategories=categoryDao.findAllCarC();
List cars=carDao.findAllCars();
System.out.println(carDao.findAllCars().size()+“size”);
request.setAttribute(“Cars”,Cars);
request.setAttribute(“类别”,carCategories);
setAttribute(“ImageMan”,ImageManager.getInstance());
System.out.println(“请求url:+request.getRequestURL()+“请求servlet路径”+request.getServletPath());
getRequestDispatcher(“index.jsp”).forward(请求、响应);
}
}
所以,在第一次它为null时,我将它转发给所有的carcategories,并创建了Car对象列表,然后我将它转发给jsp文件,从逻辑上讲,它不能为null,因为我创建了一个Car对象列表,但它是
另一个谜团是它同时出现在两个if语句中,所以它同时为null和notnull
这就是控制台输出的样子(只是为了显示那里发生了什么)
它如何可以同时为空和不为空?谢谢在第一个
if
测试null
(最后一个在第二个if
之后)之前添加额外的打印
(只是为了显示更多的信息,不是真正的答案,我不开发JSP)它不可能同时为null和not null。但正如我在上面所展示的,不,你只是认为你在上面展示了它,你只是认为它是。是的,我在上面展示了它,我对它同时为null和notnull感到好奇这些print语句甚至不在同一范围内,更不用说同时了。他们根本不是“同一时间”。
<c:if test="${!pageContext.request.servletPath.equals('/Login')}">
<%
System.out.println("new login");
%>
<c:if test = "${requestScope.Cars!=null}">