request.attribute空指针异常问题!求教!
在tomcat上运行all.jsp就显示空指针异常,应该是attribute方法没有获取到,但为什么呢?
all.jsp:
<%@page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= request.getAttribute("id")%>
<%
List<String> names = (List)request.getAttribute("id");
for(String name: names){
%>
<%=name %><br>
<%
}
%>
%>
</body>
</html>
ListAllServlet.java:
package com.xugencheng.javaweb;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ListAllServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("id", "aa");
request.getRequestDispatcher("/all.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}