Error in Java: org.apache.jasper.JasperException - Class compilation error in JSP:

I'm looking to create a website for uploading photos to MySQL using JSP code, but I'm encountering an error that I'm struggling to resolve in the code.

First, I found an example "upload.html"

<title>Select your File to upload</title>
<form action="loading_blob.jsp" method=POST ENCTYPE="multipart/form-data">
Staff ID: 
<input type=text name=id>
Photo:
<input type=file name=uploadfile>

<input type=submit name=button value=upload>
</form>

I then used the example (loading_blob.jsp) and downloaded the cos.jar file, placing it into tomcat->lib.

<%@ page import="java.io.*, java.sql.*, java.util.*, javax.servlet.*, javax.servlet.http.*, com.oreilly.servlet.multipart.*"  %>

<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);


String dirname = "C:\\apache-tomcat-6.0.26\\webapps\\ROOT\\upload-example\\";
File dir = new File(dirname);
String value = "";
String fileName = "";
String filePath = "";
long size = 0;

MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
Part part;
while ((part = mp.readNextPart()) != null) {
  String name = part.getName();
  if (part.isParam()) {
    // it's a parameter part
    if (name.equals("id"))
      {   
         ParamPart paramPart = (ParamPart) part;
     value = paramPart.getStringValue();
     out.println("param; name=" + name + ", value=" + value);
      }
  }
     else if (part.isFile()) {
            // it's a file part
        FilePart filePart = (FilePart) part;
        fileName = filePart.getFileName();
        if (fileName != null) {
               filePath = filePart.getFilePath();
           // the part actually contained a file
           size = filePart.writeTo(dir);
           out.println("file; name=" + name + "; filename=" + fileName +
          ", filePath=" + filePath +
          ", content type=" + filePart.getContentType() +
          ", size=" + size);
      }
      else {
        // the field did not contain a file
        out.println("file; name=" + name + "; EMPTY");
           }
    }

  }

String dirname1 = dirname + fileName;
out.println(dirname1 + size);
Class.forName("com.mysql.jdbc.Driver"); 
 Connection con =  DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1", "root", "0000"); 

InputStream fis = new FileInputStream(dirname1);

 PreparedStatement ps = con.prepareStatement("Update person set photo = ?, filename = ? where staff_id = ?");

ps.setBinaryStream (1, fis, size); 

ps.setString (2, fileName);
 ps.setString (3, value);
 ps.executeUpdate();
 ps.close();

 fis.close(); 

%>

Error message:

An error occurred at line: 16 in the jsp file: /loading_blob.jsp
The type Part is ambiguous
13: long size = 0;
14: 
15: MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
16: Part part;
17: while ((part = mp.readNextPart()) != null) {
18:   String name = part.getName();
19:   if (part.isParam()) {


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Thank you

Answer №1

As per the suggestion from @Gimby, it is important to carefully consider the imports being used;

There are two Part object options available: javax.servlet.http.Part and com.oreilly.servlet.multipart.Part. It is crucial to specify which one is being utilized.

To use either javax.servlet.http.Part or

com.oreilly.servlet.multipart.Part
, follow this format.

Additionally, consider initializing the Part object as null.

MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
com.oreilly.servlet.multipart.Part part = null; //For example purposes, remember not to keep 'part' as null when using 'part = mp.readNextPart()' in the while loop below.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Add a fresh column in the table that includes modified values from an existing column, affected by a multiplication factor provided through a text input

I'm currently working on a Laravel website where I have a pricing table sourced from a database. The Retail Price column in this table serves as the base for calculating and populating a Discount Price column using a multiplier specified in a text inp ...

Double injection of Redux-saga

I've encountered a strange issue with my redux-saga - it's being called twice instead of just once. Here is the action that triggers the saga: export function createRequest (data) { return { type: CREATE_REQUEST, payload: {data} }; ...

Creating a JSON structure using an array in Typescript

Here is an example of my array structure: [ { "detail": "item1", "status": "active", "data": "item1_data" }, { "detail": "item2", "status": ...

Executing JavaScript functions within Vue JS components

I'm currently working on a project that involves Vue JS within a Laravel Project Can someone guide me on how to fetch data from another JS file? Here's what I have so far: MainComponent.vue data() { return this.getData() } DataComponent.j ...

What is a creative way to get rid of old content on a webpage using jQuery without relying on the

As I work on my crossword project, I have almost completed it, but encountering a problem. Within my HTML code, I have a select list that loads a .JSON file using Javascript. <form method="post" id="formulaire"> <div class="toto"> <sel ...

In Node.js, fast-xml-parse is only returning a single object instead of an array

Currently, I am working on implementing tracking functionality using a specific service that provides responses in XML format. For parsing the XML response, I have opted to utilize the fast-xml-parser package. However, I have encountered an issue: Everyth ...

Leveraging jquery's $.ajax method to send GET values to a PHP endpoint

Is it possible to use an AJAX call to pass a value to a PHP script? For example, if I have the URL example.com/test.php?command=apple, how can I make sure that the code is executed properly on the server side? This is how my code looks like: PHP: <?p ...

Unlock the Power of Typescript: Using the Browser Console to Access Functions

Scenario Within the file ts/app.ts, the following function exists: function foo() { console.log('Hello Word'); } After successful compilation with Webpack, it generates a file named bundle.js. To load this file, use the following script tag ...

Steps for embedding a custom function in a switch statement

I am attempting to run a switch statement based on the argument provided to the function below. I have been trying to call different functions depending on the argument passed. However, I encountered an Uncaught ReferenceError in the beginning of the .js f ...

Understanding the functionality of a notification system

Planning to create an admin tasking system utilizing PHP, MySQL, and JavaScript. Curious about how the notification system operates and how it stores real-time data. Are there any good examples of a notification system? ...

Error found in event.PreventDefault()

I'm currently implementing Twitter Bootstrap on my application. I added e.preventDefault for the link button within $(document).ready(), but it doesn't seem to be functioning properly. Below is the code snippet: Master page: <a id="lnkLogou ...

Encountering an issue of receiving "Undefined" while attempting to access constant data from a ReactJS file within my NodeJS

I am currently working on a file named check-rates that stores useStates() which are inputs provided by users. These inputs are essential for me to calculate and provide an estimated value for their shipment using the DHL API. In my nodejs express server, ...

What is the best way to exchange the chosen selection within 2 select elements?

I have been trying to swap the cities in my select fields using two select elements, but for some reason, it is not working when the button is clicked: https://i.sstatic.net/mHg4Y.jpg <div class="select-wrapper"> <select class="airport-selec ...

Utilize a randomized dynamic base URL without a set pattern to display a variety of pages

I am intrigued by the idea of creating a dynamic route that can respond to user-generated requests with specific files, similar to how Github handles URLs such as www.github.com/username or www.github.com/project. These URLs do not follow a set pattern, ma ...

Achieve a scrolling effect for just a specific section of an HTML page

Hey there! I'm currently working on adding scrolling text along the side of a webpage for a specific section. My goal is to have three different sections, each with text that stops scrolling when you reach it and then transitions to the next section o ...

Nextjs: Issues with Dropdown functionality when using group and group-focus with TailwindCSS

My goal is to make an array visible once a button is clicked. By default, the array should be invisible, similar to drop-down menus in menu bars. I am utilizing the group and group-focus classes. While the array disappears as expected, it does not reappear ...

Easy Registration Page using HTML, CSS, and JavaScript

In the process of creating a basic login form using HTML and CSS, I'm incorporating Javascript to handle empty field validations. To view my current progress, you can find my code on jsfiddle My goal is to implement validation for empty text fields ...

Can a single button click be shared across multiple forms?

The main concept involves a grid where when a user double-clicks on a row, a modal window (Bootstrap panel) opens with a panel-body section for editing the data and a panel-footer containing a btn-group for actions like "Save", "Cancel", or "Close". This s ...

When utilizing useEffect in Next.js, an error may occur stating that the window is

It seems like there is a challenge with executing script code server side in next.js 13 when it needs to be executed client side. Currently, I am trying to implement the bulma calendar found at When importing the required library: import PasswordStrengthB ...

Performing a mass insertion of records in MySQL using Node.js with the added functionality of updating existing rows if there is a duplicate

I am currently facing a challenge while attempting to perform a bulk row insert using node-mysql. Typically, this process is straightforward; however, I need to apply a MySQL function to one of the columns. Usually, I would execute a query like the follow ...