我在从我的servlet打开我的JSP时遇到问题..我正在尝试使用我的数据库在我的jsp中输出它...我还有一个名为Item.java的类将它放在HashMap上并将其放入会话能够在我的下一个JSP上使用它..
这是我试图打开的JSP
<%@page import="classes.Item"%>
<%@page import="java.util.HashMap"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%
String User = null;
HashMap<String,Item> currentInventory = null;
if(session.getAttribute("username") == null)
response.sendRedirect("Login.html");
else
{
User = (String) session.getAttribute("username");
if(session.getAttribute("currentInventory") != null)
{
currentInventory = (HashMap<String,Item>) session.getAttribute("currentInventory");
}
}
%>
<%@include file="block/header.html"%>
<h2>Inventory List</h2>
<table border="1">
<tr>
<td>Stock ID</td>
<td>Name</td>
<td>Price</td>
<td>On Stock</td>
</tr>
<%
for(int i = 0; i < currentInventory.size(); i++)
{
%>
<tr>
<td><%= currentInventory.get(i).getStockID() %></td>
<td><%= currentInventory.get(i).getItemName() %></td>
<td><%= currentInventory.get(i).getUnitPrice() %></td>
<td><%= currentInventory.get(i).getOnStock() %></td>
</tr>
<%
}
%>
</table>
<%@include file="block/footer.html"%>
这是我的Servlet中的代码
package servlets;
import java.io.*;
import java.sql.*;
import java.util.HashMap;
import classes.Item;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
@WebServlet("/ShowInventoryListServlet")
public class ShowInventoryListServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HashMap<String,Item> currentInventory = new HashMap<String,Item>();
HttpSession session = request.getSession();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String url = "jdbc:mysql://localhost:3306/inventory";
String user = "root";
String password = "password";
String query = "SELECT * FROM items";
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
if(rs.isBeforeFirst())
{
while (rs.next())
{
String dbStockId = rs.getString("stockId");
String dbItemName = rs.getString("itemName");
float dbUnitPrice = Float.parseFloat(rs.getString("unitPrice"));
int dbOnStock = Integer.parseInt(rs.getString("onStock"));
Item items = new Item(dbStockId,dbItemName,dbUnitPrice,dbOnStock);
currentInventory.put(dbStockId, items);
}
session.setAttribute("currentInventory", currentInventory);
String encodedURL = response.encodeRedirectURL("ShowInventoryList.jsp");
response.sendRedirect(encodedURL);
}
else
{
out.println("No records found...");
}
}
catch (ClassNotFoundException ex)
{
out.println("Error. Class not found...");
}
catch (SQLException ex)
{
out.println("Something wrong happened...");
}
finally
{
out.close();
try
{
rs.close();
stmt.close();
conn.close();
}
catch (SQLException ex)
{
}
}
}
}
这是我的Item.java类
package classes;
public class Item
{
private String stockID;
private String itemName;
private float unitPrice;
private int onStock;
public Item(String stockID, String itemName, float unitPrice, int onStock)
{
this.stockID = stockID;
this.itemName = itemName;
this.unitPrice = unitPrice;
this.onStock = onStock;
}
public String getStockID()
{
return stockID;
}
public String getItemName()
{
return itemName;
}
public float getUnitPrice()
{
return unitPrice;
}
public int getOnStock()
{
return onStock;
}
}
错误
SEVERE:带有路径[/ MachineProblem]的上下文中servlet [jsp]的Servlet.service()抛出异常[在第41行处理JSP页面/ShowInventoryList.jsp时发生异常
{
%>
<tr>
<td><%= currentInventory.get(i).getStockID() %></td>
<td><%= currentInventory.get(i).getItemName() %></td>
<td><%= currentInventory.get(i).getUnitPrice() %></td>
<td><%= currentInventory.get(i).getOnStock() %></td>