Java 已编辑:JSP未使用数据库从servlet打开

Java 已编辑:JSP未使用数据库从servlet打开,java,jsp,servlets,Java,Jsp,Servlets,我在从servlet打开JSP时遇到问题。。我正在尝试使用我的数据库在jsp中输出它。。我还有一个名为Item.java的类将其放在HashMap上,并将其放在会话中,以便能够在我的下一个JSP上使用它 这是我试图打开的JSP <%@page import="classes.Item"%> <%@page import="java.util.HashMap"%> <%@ page language="java" contentType="text/html; cha

我在从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"%>
错误

严重:路径为[/MachineProblem]的上下文中Servlet[jsp]的Servlet.service()引发异常[在第41行处理jsp页/ShowInventoryList.jsp时发生异常]

{
%>

错误:
位于servlet.ShowInventoryListServlet.doGet(ShowInventoryListServlet.java:48)
。解决方案:1)查看doGet()的第48行方法。2)确定第48行引用了什么对象…以及为什么它可能为null。3)确保该对象已正确初始化。4)或者更好,更改代码,以便如果对象未初始化,它会抛出一个有意义的错误,说明其未能初始化的原因。我已经看到错误,谢谢!但现在我发现此错误为我的for循环有问题吗?在你的问题中发布相关代码和错误堆栈跟踪。不是在JSFIDLE。这滥用了JSFIDLE和stackoverflow。已经编辑了。我在编辑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) 
        {

        }
    }
}
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;
    }
}
    {
    %>
    <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>