我使用servlet和jsp页面在表中显示数据库信息。我可以成功地将信息插入数据库但是当我尝试在另一个页面中显示信息时,我遇到了问题。我在for(itr = data.iterator(); itr.hasNext();)
行收到错误。我的代码就在下面,我得到的错误低于我的代码。我知道我的主要问题是迭代,所以如果有人可以提供帮助,我会非常感激。 (顺便说一句,这段代码是关于youtube视频的一个字一个字,我正在观看用户让它工作得很好)。
<%@page import="java.util.Iterator" %>
<%@page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Scheduling</title>
</head>
<body>
<table>
<%Iterator itr;%>
<%List data = (List)request.getAttribute("jobsData");
for(itr = data.iterator(); itr.hasNext();){
%>
<tr>
<% String s = (String) itr.next();%>
<td><%=s%></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><%= itr.next() %></td>
<td><input type="submit" value="Edit" name="edit" onclick ="editRecord(<%=s%> %>);"></td>
<td><input type="submit" value="Delete" name="delete" onclick ="deleteRecord(<%=s%> %>);"></td>
<%} %>
</tr>
</table>
错误:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
根本原因
java.lang.NullPointerException
org.apache.jsp.displayjobs_jsp._jspService(displayjobs_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
更新:这是我的servlet代码。
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class displayjobs
*/
@WebServlet("/schedule")
public class schedule extends HttpServlet {
List list = new ArrayList();
Statement stmt;
ResultSet res;
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public schedule() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://10.11.1.246;databaseName=Test;integratedSecurity=false;user=sa;password=S0l1dConcepts";
Connection con = DriverManager.getConnection(connectionUrl);
String query = "select * from jobs";
stmt = con.createStatement();
res = stmt.executeQuery(query);
while(res.next()){
list.add(res.getString(1));
list.add(res.getString(2));
list.add(res.getString(3));
list.add(res.getString(4));
list.add(res.getString(5));
list.add(res.getString(6));
list.add(res.getString(7));
list.add(res.getString(8));
list.add(res.getString(9));
list.add(res.getString(10));
}
res.close();
}
catch(Exception e){
RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
}
finally{
request.setAttribute("jobsData",list);
RequestDispatcher rd = request.getRequestDispatcher("/displayjobs.jsp");
rd.forward(request, response);
list.clear();
out.close();
}
}
}
答案 0 :(得分:0)
在jsp中,你试图获得&#34; jobsData&#34;的值。在servlet中,您将列表存储在&#34; jData&#34;中。由于这个原因,无法获取请求属性并且数据设置为空
答案 1 :(得分:0)
1 List data = (List)request.getAttribute("jobsData");
2 for(itr = data.iterator(); itr.hasNext();
复制代码的第一行返回NULL
列表,因为servlet中属性的名称已由不同的名称设置(如果您打算将“list”作为“jobsData”传递),即“jData” ”
finally{
request.setAttribute("jData",list);
RequestDispatcher rd = request.getRequestDispatcher("/displayjobs.jsp");
rd.forward(request, response);
list.clear();
out.close();
}
您需要修复此命名才能使其正常工作。
Edit 1:
您的代码块在不同的时间点被调用。在页面加载时调用GET
,在表单提交时调用POST
。你正在获得NPE,因为这个区块没有做任何事情。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
请通过doGet and doPost in Servlets查看基本信息。您所指的块是POST块。您需要在get块中预先填充相同的对象,以便在页面加载时传递数据。