JavaWeb getAttribute()方法获取不到

servlet

    private void orderlist(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("com to orderlist");

        User user = (User) req.getSession().getAttribute(Contacts.USER_SESSION);

        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> userList = mapper.selectOrderByUserName(user.getUser_name());
        req.getSession().setAttribute(Contacts.USER_ORDER,userList);
        try {
            req.getRequestDispatcher("index/orderlist.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

js
<table border="0" cellspacing="0" cellpadding="0"
			class="table table-hover table-striped font12 table-bordered v-align-top">
			<tr>
				<th style="width:10%;">航班号</th>
				<th style="width:15%;">乘机人</th>
				<th style="width:18%;">乘机日期</th>
				<th style="width:10%;">舱位</th>
				<th style="width:17%;">乘客证件</th>
				<th style="width:15%;">联系人</th>
				<th style="width:15%;">联系电话</th>
			</tr>
			
			<%
				List<UserOrder> attribute = (List<UserOrder>) request.getAttribute(Contacts.USER_ORDER);
				for (UserOrder order:attribute) {
			%>
			
			<tr>
				<td>
					<%=order.getFlight_number() %>
				</td>
				<td><p><%=order.getPassenger_name() %></p></td>
				<td><%=order.getDate() %></td>
				<td><%=order.getFlight_class() %></td>
				<td><%=order.getPassenger_id() %></td>
				<td><%=order.getContact() %></td>
				<td><%=order.getContact_phone() %></td>
				
			</tr>
			
			<%
				}
			%>

		</table>

tomcat报错:

当我看到空指针异常时嘴角微微上扬,又是这个错但是当我重新检查时,发现又没有错误

突然想起自己当时在写这一段的时候不知为什么加了getSession()

 

 

 然后我将上面的代码改成下面这样时错误又解决了

 所以getSession().getAttribute()和直接getAttribute()有什么区别我也不是很清楚.

        就先先纪念一下自己遇到的问题吧

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值