post
和doPost
中。不能正常工作,而是获取并且服务正常工作。
而且Blob图片文件也没有上传。
jsp代码:
<form method="post" enctype="multipart/form-data" action="insertbooks">
servlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//retrieving image
InputStream inputStream = null; // input stream of the upload file
String Title = request.getParameter("title");
// obtains the upload file part in this multipart request
Part filePart = request.getPart("image");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
pw.println("Got photooo");
}
String Author = request.getParameter("author");
String Price = request.getParameter("price");
float Pricef = Float.valueOf(Price);
pw.println("Got parameters");
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
pw.println("Driver Loaded");
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
pw.println("Connection Established_1");
stmt = conn.createStatement();
// String query = "Insert into books(title,image,author,price) values('"+Title +"','"+inputStream +"','"+Author+"','"+Pricef +"')" ;
String query = "Insert into books(title,image,author,price) values(?,?,?,?)" ;
PreparedStatement ps =conn.prepareStatement(query);
ps.setNString(1,Title);
if(inputStream != null){
// fetches input stream of the upload file for the blob column
ps.setBinaryStream(2, inputStream);
}
ps.setNString(3,Author);
ps.setFloat(4, Pricef);
// Sends the statement to the database server
int row= ps.executeUpdate();
if(row>0){
pw.println("data inserted");
}
/*int row = stmt.executeUpdate(query);
if (row > 0) {
pw.println("Data Inserted");
} */
} catch (SQLException ex) {
ex.getMessage();
// ex.printStackTrace();
ex.printStackTrace(pw);
pw.println("Connection not Established_1");
} catch (ClassNotFoundException ex) {
Logger.getLogger(insertbooks.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
System.out.println("Successfully");
// sets the message in request scope
// request.setAttribute("message", message);
// forwards to the message page
// getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
}
pw.close();
}
}
错误:
HTTP状态500- 输入例外报告
消息
说明服务器遇到内部错误,导致服务器无法执行 完成此请求。
例外
java.lang.NullPointerException
sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
java.lang.Float.parseFloat(Float.java:451)
java.lang.Float.valueOf(Float.java:416)
insertbooks.doPost(insertbooks.java:63)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)