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");
...