Search code examples
javaherokuservletswar

Why when I deploy a javaweb app (war) on heroku I get java.lang.UnsupportedClassVersionError and a 404


I am testing this simple app with heroku, which is a registry. When I run it on the eclipse server I have no problem, it does the registration correctly. However when deploying it on heroku and submitting to the servlet I have 2 errors.

Servlet code:

    @WebServlet("/Servlet")
    public class Servlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public Servlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 

    ServletException, IOException {
                // TODO Auto-generated method stub
                //response.getWriter().append("Served at: ").append(request.getContextPath());
                //Conexion.Conectar();
                boolean crear = create(request.getParameter("correo"),request.getParameter("nombre_usuario"),request.getParameter("pass"));
                //System.out.println(creado);
                
                if(crear){
                    request.getRequestDispatcher("correcto.jsp").forward(request, response);
                    System.out.println("ok");
                }else {
                    request.getRequestDispatcher("index.jsp").forward(request, response);
                    System.out.println("mal");
                }
                //System.out.println("ok");
            }
        
            /**
             * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
             */
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // TODO Auto-generated method stub
                doGet(request, response);
            }
            
            
            
            public boolean create(String correo, String nombre_usu, String password) {
                boolean creado = false;
                Connection con = null;
                PreparedStatement ps = null;
                try{
                                
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    con = DriverManager.getConnection("jdbc:mysql://XXXXXX","XXXXXXX","XXXXXXXXX");
                    String sql = "INSERT INTO usuarios (correo,nombre_usuario,clave) VALUES (?,?,?)";
                    System.out.println("Conexion bn");
                    ps = con.prepareStatement(sql);
                    ps.setString(1, correo);
                    ps.setString(2, nombre_usu);
                    ps.setString(3, password);
                    creado = ps.execute;
                
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally {
                    try {
                        ps.close();
                        con.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                return creado;
            }
        }

Errors

1)The first time I run the deployed app when do the sumbit I get this:

enter image description here

  1. Then I do the same and the app shows that: https://i.sstatic.net/kIKvT.jpg

My project https://i.sstatic.net/5ZNAD.jpg

https://i.sstatic.net/K9iMQ.jpg

index.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>Formulario</h1>
        <form action="Servlet" method="GET">
            Correo <input type="text" name="correo"> <br><br>
            Nombre de usuario <input type="text" name="nombre_usuario"> <br><br>
            Contraseña <input type="text" name="pass"> <br><br> 
            <input type="submit" value="Registrarse">
        </form>
    </body>
    </html>

From what I have investigated it could be due to the routes as discussed in this post https://es.stackoverflow.com/questions/320688/como-utilizar-las-rutas-relativas-en-javaee-para-war-en-hosting however, I´ve tried but it still doesn't work.


Solution

  • The first error message about class version means that you run the application using Java version which is older than the one you've used to compile it:

    • Java class version 60 is Java 16 — this is probably what you've used to compile;
    • Java class version 52 is Java 8 — this is the one you use to run it on Heroku.

    So you should either build your app using Java 8, or update the Java version that you use on Heroku to at least 16.

    The second error is most likely because your application wasn't deployed properly due to the first error.