CreateServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String _token = getParamVal(request.getPart("_token")); // = (String) request.getParameter("_token");
if (_token != null && _token.equals(request.getSession().getId())) {
EntityManager em = DBUtil.createEntityManager();
Reshipi r = new Reshipi();
Picture p = new Picture();
String name = getParamVal(request.getPart("name")); // = request.getParameter("name");
r.setName(name);
String content = getParamVal(request.getPart("content")); // = request.getParameter("content");
r.setContent(content);
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
r.setCreated_at(currentTime);
r.setUpdated_at(currentTime);
//Get name
String file_name = getParamVal(request.getPart("file_name"));
p.setFile_name(file_name);
//Get the recipe ID
Reshipi reshipi_id = getParamVal(request.getPart("reshipi_id"));
p.setReshipi_id(reshipi_id);
//Get the id of the picture
Integer id = getParmVal(request.getPart("id"));
p.setId(id);
//Creating an error list
List<String> errors = ReshipiValidator.validate(r);
//If there is an error
if (errors.size() > 0) {
//Close DB
em.close();
request.setAttribute("_token", request.getSession().getId());
request.setAttribute("content", r);
request.setAttribute("errors", errors);
response.sendRedirect(request.getContextPath() + "/index");
return;
} else {
//Save to database
em.getTransaction().begin();
em.persist(r);
em.getTransaction().commit();
request.getSession().setAttribute("flush", "Registration has been completed");
em.close();
}
//Image processing received from the image form
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
Collection<Part> parts = request.getParts();
for (Part part : parts) {
String f_name = getFileName(part);
if(!f_name.contains("no_p_name") && !f_name.isEmpty()){
part.write(getServletContext().getRealPath("/images") + "/" + f_name);
response.getWriter().append("upload:").append(f_name);
}
}
I wrote, but in reality
CreateServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String _token = getParamVal(request.getPart("_token")); // = (String) request.getParameter("_token");
if (_token != null && _token.equals(request.getSession().getId())) {
EntityManager em = DBUtil.createEntityManager();
Reshipi r = new Reshipi();
Picture p = new Picture();
String name = getParamVal(request.getPart("name")); // = request.getParameter("name");
r.setName(name);
String content = getParamVal(request.getPart("content")); // = request.getParameter("content");
r.setContent(content);
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
r.setCreated_at(currentTime);
r.setUpdated_at(currentTime);
String file_name = getParamVal(request.getPart("file_name"));
p.setFile_name(file_name);
//Image and reshipi_Linking with id ★★★★★★★★★★★★★
p.setReshipi_id(r);
//Image processing received from the image form ↓ ★★★★★★★★★★★★★★★
//In the code above, it was at the bottom, but it does not work properly even under the save process etc.
//It works only when it is on the code to save to DB
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
Collection<Part> parts = request.getParts();
for (Part part : parts) {
String f_name = getFileName(part);
if(!f_name.contains("no_p_name") && !f_name.isEmpty()){
part.write(getServletContext().getRealPath("/images") + "/" + f_name);
response.getWriter().append("upload:").append(f_name);
p.setFile_name(f_name);
}
}
List<String> errors = ReshipiValidator.validate(r);
if (errors.size() > 0) {
em.close();
request.setAttribute("_token", request.getSession().getId());
request.setAttribute("content", r);
request.setAttribute("errors", errors);
response.sendRedirect(request.getContextPath() + "/index");
return;
} else {
//Save process
em.getTransaction().begin();
em.persist(r);
//Insert image saving process ★★★★★★★★★★★★★★★★★★★★★
em.persist(p);
em.getTransaction().commit();
request.getSession().setAttribute("flush", "Registration has been completed");
em.close();
}
Will be.