我正在尝试从数据库中检索图像...但我得到了一个nullpointer异常
这是我的JSP
<html>
<head>
<title>Student Registration Form</title>
<style type="text/css">
h3{font-family: Calibri; font-size: 22pt; font-style: normal; font-weight: bold; color:SlateBlue;
text-align: center; text-decoration: underline }
table{font-family: Calibri; color:white; font-size: 11pt; font-style: normal;
text-align:; background-color: SlateBlue; border-collapse: collapse; border: 2px solid navy}
table.inner{border: 0px}
</style>
</head>
<body>
<%
long prodid=(long)request.getAttribute("prodid");
String prodname=(String)request.getAttribute("prodname");
String proddesc=(String)request.getAttribute("proddesc");
long prodcode=(long)request.getAttribute("prodcode");
%>
<h3>Update Category</h3>
<form action="update_category" method="get">
<table align="center" cellpadding = "10">
<tr>
<td>ID</td>
<td><%=prodid %></td>
<tr>
<td>Product Name</td>
<td><input type="text" name="category_name" maxlength="30" value="<%=prodname %>"/>
(max 30 characters a-z and A-Z)
</td>
</tr>
<tr>
<td>Product Description</td>
<td><input type="text" name="category_name" maxlength="30" value="<%=proddesc %>"/>
(max 30 characters a-z and A-Z)
</td>
</tr>
<tr>
<td></td>
<td><input type="hidden" name="prodid" maxlength="30" value="<%=prodid %>"/></td>
</tr>
<tr>
<td>Product Image</td> //The below servlet is called in this line
<td><img src="ProductImage" alt="" width="122" height="123" />
(max 30 characters a-z and A-Z)
</td>
</tr>
<tr>
<td>Product Code</td>
<td><%=prodcode %></td>
(max 30 characters a-z and A-Z)
</tr>
<tr>
<td colspan="2" align="center">
<input type="Submit" name="UpdateProd" value="Update">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
这是我的Servlet
package com.kunal.servlet;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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;
import org.omg.CORBA.portable.InputStream;
import com.mysql.jdbc.Blob;
/**
* Servlet implementation class ShowImage
*/
@WebServlet("/ShowImage")
public class ShowImage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ShowImage() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String productid=request.getParameter("productid");
System.out.println("I am printing " + productid);
Blob blob=null;
byte[ ] imgData = null ;
InputStream input=null;
Connection connection=null;
ResultSet rs1;
String connectionURL = "jdbc:mysql://localhost:3306/agro";
try
{
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "");
//Add the data into the database
Statement stmt;
stmt=connection.createStatement();
rs1=stmt.executeQuery("select * FROM tbl_products where product_id = '"+productid+"'");
rs1.beforeFirst();
while(rs1.next())
{
blob=(Blob) rs1.getBlob(4);
imgData=blob.getBytes(1, (int) blob.length());
}
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
System.out.println("Entering into image process loop");
o.write(imgData);
o.flush();
o.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
finally {
// Always close the database connection.
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String productid=request.getParameter("productid");
System.out.println("I am printing " + productid);
Blob blob=null;
byte[ ] imgData = null ;
InputStream input=null;
Connection connection=null;
ResultSet rs1;
String connectionURL = "jdbc:mysql://localhost:3306/agro";
try
{
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "");
//Add the data into the database
Statement stmt;
stmt=connection.createStatement();
rs1=stmt.executeQuery("select * FROM tbl_products where product_id = '"+productid+"'");
rs1.beforeFirst();
while(rs1.next())
{
blob=(Blob) rs1.getBlob(4);
imgData=blob.getBytes(1, (int) blob.length());
}
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
System.out.println("Entering into image process loop");
o.write(imgData);
o.flush();
o.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
finally {
// Always close the database connection.
try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
}
}
}
}
堆栈跟踪
Aug 16, 2014 4:39:20 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Aug 16, 2014 4:39:20 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Agro' did not find a matching property.
Aug 16, 2014 4:39:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Aug 16, 2014 4:39:20 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 16, 2014 4:39:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Aug 16, 2014 4:39:20 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Aug 16, 2014 4:39:20 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 629 ms
Aug 16, 2014 4:39:20 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 16, 2014 4:39:20 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.9
Aug 16, 2014 4:39:21 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Aug 16, 2014 4:39:21 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Aug 16, 2014 4:39:21 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Aug 16, 2014 4:39:21 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 699 ms
Category:Apple
1
Apple
Apple
567895
I am printing null
Entering into image process loop
java.lang.NullPointerException
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:90)
at com.kunal.servlet.ShowImage.doGet(ShowImage.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
servlet应该从JSP中读取“productid”。但它为该变量提供了空指针异常....
答案 0 :(得分:0)
您尝试访问的变量名称是&#34; productid&#34;在servlet中你使用&#34; prodid&#34;在你的jsp上你得到空指针异常