Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Plz help with servlet problem///// Type Exception Report Description The server

ID: 3915905 • Letter: P

Question

Plz help with servlet problem/////

Type Exception Report

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

Note The full stack trace of the root cause is available in the server logs.

----------------- I try to upload image file but the system give me this ---------------- I Bloded the line 55,66, and 105 for you ----

@WebServlet("/upload")

@MultipartConfig

public class upload extends HttpServlet {

   private static final long serialVersionUID = 1L;

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println(getServletContext().getRealPath("/images"));

      

       response.setContentType("text/html");

       PrintWriter out = response.getWriter();

       out.println("<!DOCTYPE html> " +

               "<html lang="en"> " +

               " <head> " +

               " <title>File Upload</title> " +

               " <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> " +

               " </head> " +

               " <body> " +

               " <form method="post" action="upload" enctype="multipart/form-data" > " +

               " File: " +

               " <input type="file" name="imageFilename" id="file" /> <br/> " +

               " Destination: " +

               " <input type="text" value="/tmp" name="destination"/> " +

               " </br> " +

               " <input type="submit" value="Upload" name="upload" id="upload" /> " +

               " </form> " +

               " </body> " +

               "</html>");

   }

  

   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       processRequest(request, response);  // here is line 55

      

   }

  

   protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      response.setContentType("text/html;charset=UTF-8");

      // Create path components to save the file

      //final String path = request.getParameter("destination");

      final String path = getServletContext().getRealPath("/images");

      final Part filePart = request.getPart("imageFile");

      final String fileName = getFileName(filePart); // ehre is line 66

      OutputStream out = null;

      InputStream filecontent = null;

      final PrintWriter writer = response.getWriter();

      try {

      out = new FileOutputStream(new File(path + File.separator + fileName));

      filecontent = filePart.getInputStream();

      int read = 0;

      final byte[] bytes = new byte[1024];

      while ((read = filecontent.read(bytes)) != -1) {

      out.write(bytes, 0, read);

      }

      writer.println("New file " + fileName + " created at " + path);

  

      } catch (FileNotFoundException fne) {

      writer.println("You either did not specify a file to upload or are "

      + "trying to upload a file to a protected or nonexistent "

      + "location.");

      writer.println("<br/> ERROR: " + fne.getMessage());

  

      } finally {

      if (out != null) {

      out.close();

      }

      if (filecontent != null) {

      filecontent.close();

      }

      if (writer != null) {

      writer.close();

      }

      }

   }

   private String getFileName(final Part part) {

      final String partHeader = part.getHeader("content-disposition");   /// here is line 105

      for (String content : part.getHeader("content-disposition").split(";")) {

      if (content.trim().startsWith("filename")) {

      return content.substring(

      content.indexOf('=') + 1).trim().replace(""", "");

      }

      }

      return null;

   }

}

Explanation / Answer

You are getting a NULL POINTER EXCEPTION. Your are getting this error because the part object that you are passing to getFileName() as a parameter is NULL.

I see that you are getting the part value as request.getPart("imageFile");. I think this is the place where you getting NULL value, which means that your request does not any part object with the name 'imageFile'.

In order to avoid this problem, please make sure that the part you pass to getFileName() is not NULL. Or, you can include a null check too, like

if(part == null)

return null;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote