post
и doPost
в jsp и servlet соответственно. не работают, а получают и сервисы работают.
И файл изображений BLOB-объектов также не загружается.
JSP код:
<form method="post" enctype="multipart/form-data" action="insertbooks">
код сервлета:
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 -
Тип Exception report
сообщение
description Сервер обнаружил внутреннюю ошибку, которая помешала ему
выполняя этот запрос.
исключение
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)