0

I recently upgraded my Java web application to use Tomcat 10 and Java 11.

The code performs an ajax call from a javascript file to a back end servlet during the loading of a particuar web page. However, now, I am getting the following error:

java.lang.ClassCastException: class GenericServlets.All.PageLoadServlet_2023_2024 cannot be cast to class jakarta.servlet.Servlet (GenericServlets.All.PageLoadServlet_2023_2024 is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @a0a0ebe; jakarta.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader @1f7030a6)

After searching online, many solutions suggest changing the import from:

javax.servlet.*;

to

jakarta.servlet.*;

For example, as shown here:

Servlet 5.0 JAR throws compile error on javax.servlet.* but Servlet 4.0 JAR does not

However I have done this and it is still not working, with the same error message being presented.

For reference, here is the code for the ajax call I am using from the javascript file:

function pageLoad()
{
   $.ajax({
            type: 'POST', 
            data: {pageLoadData : "J"},
            url: '../../../../All/PageLoadServlet_2023_2024',
            success: function (data)
            { 
              ...

And here are the import statements from my servlet:

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@WebServlet("/All/PageLoadServlet_2023_2024")
public class PageLoadServlet_2023_2024 extends HttpServlet 
{
    

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


    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        
        // Get parameter
        String pageLoadData = request.getParameter("pageLoadData");  

        ...

  • 1
    Tomcat 10 does not use javax inside the server binaries or the framework .jar's e.g. the jsp or servlet spec jars. They must now all be "jakarta" unless they are SE Core javax classes. Be sure your page loader servlet extends jakarta.servlet.http.HttpServlet and you have started with at least the doPost method override. – Samuel Marchant Jun 09 '23 at 15:31
  • My servlet does indeed extend jakarta.servlet.http.HttpServlet. Also. the first method override is doGet then I override the doPost. Surely that is OK as it works with my other servlets? I have other servlets that work hence the reason why Im confused the only ones not working are the ones being called from the ajax call. Ive amended my original question with the servlet details. – Luke_Skywalker007 Jun 09 '23 at 20:44
  • 1
    You are not showing the import for `HttpServlet` in your servlet. Is it possible that it is still using `javax.servlet.HttpServlet`? – Luke Woodward Jun 09 '23 at 21:11
  • You are correct, I actually don't have `javax.servlet.HttpServlet` and my other servlets do however that has confused me. Shouldnt Netbeans pick that up? Anyway, I added in the import, cleaned and built the project however im still getting the same error. Is there a way to check what is actually being extended in this servlet? Could it be using `javax.servlet.HttpServlet` even though its not explicitly listed as an import? – Luke_Skywalker007 Jun 09 '23 at 21:29
  • If it were using javax, the jar in the server would have javax, Tomcat 10 does not, it has Jakarta in 10. However ??? How do you deploy, is it through Netbeans, open the tab in NBs and see which server if it is deployed through NBs. I never deploy that way in testing because of settings, I go into the project folder and open the .war with either ark (Linux) or WinRAR (ms windows) and open whichever "server apps folder" then drop the folder or contents over or into the appropriate app folder. E.g. Wildfly , Glassfish ,Tomcat 9 (javax version) or "webapps/theprojimade" tomcat 10 (Jakarta vrs) – Samuel Marchant Jun 10 '23 at 03:32
  • I also shutdown and restart the server after I deploy to clear anything li getting in settings or classes from last instance. Note , you can find errors and system.out.println in "logs/catalina.out" – Samuel Marchant Jun 10 '23 at 03:37
  • Also check your web.xml declaration for the servlet. That seems odd, also the name appears like a jsp script let segment class. [ ignore this potentially : With AJAX it is technically a call to a standard configured servlet, though at some time it may need a synchronized filter class if it outputs repeatedly verbose ] – Samuel Marchant Jun 10 '23 at 03:49
  • Sorry about Spell checker errors, I found this explains how bad these devices go (lyrics are in bottom right corner they are certain death to coherence) https://youtu.be/mDR9BzUXvfk disaster area in this phone. – Samuel Marchant Jun 10 '23 at 04:08
  • That's a piece nutty in there, you can get parameters from inside doPost. Use the HttpServletRequest to getParameter( name ). It's inherited from ServletRequest – Samuel Marchant Jun 10 '23 at 04:27
  • 1
    Another problem can go wrong swapping API levels is "annotation" library level. Did NBs insert that @WebServlet annotation with the library or is it carry over? And the error message is saying what should load it is a different class loader to that is trying to load it, The annotation path may the key. – Samuel Marchant Jun 10 '23 at 04:45
  • Too, if it did not originally extend http servlet, is it a "helper class" called by a servlet, or more particularly are you trying to bolt into the server binaries? (to compile a server). ! You don't want AJAX in Jasper e.g. ! The other maybe. – Samuel Marchant Jun 10 '23 at 04:54
  • @SamuelMarchant thank you very much for the comments and feedback which is really appreciated. Im off work now until Monday however I may have a look at this over the weekend if I have time. If I make any progress with the steps you mention above I will let you know :) – Luke_Skywalker007 Jun 10 '23 at 07:05
  • @SamuelMarchant thank you very much for all your help. I went down your solutions and the one relating to the annotation path was the issue. I re-entered the code for the annotation and forced my class to use the `jakarta.servlet.annotation.WebServlet;` then it began to work. My program has obviously got into a fickle somehow during the automatic changing of imports from javax to jakarta within Netbeans. – Luke_Skywalker007 Jun 12 '23 at 08:08

1 Answers1

0

Re-typing the @WebServlet annotation within my class and forcing Netbeans to select the jakarta.servlet.annotation.WebServlet; import got this to work.

The automatic updating of the imports from javax to jakarta seemed to have caused my program to get into a fickle somehow.

The solution provided by @SamuelMarchant above helped me solve this issue. His comments where:

Another problem can go wrong swapping API levels is "annotation" library level. Did NBs insert that @WebServlet annotation with the library or is it carry over? And the error message is saying what should load it is a different class loader to that is trying to load it, The annotation path may the key.