Home > Software design >  How to fix " HTTP Status 500 – Internal Server Error "
How to fix " HTTP Status 500 – Internal Server Error "

Time:08-19

I have checked almost everything, even matched the code in youtube but there, for someone, it is running successfully but not for me :(

HTML code :

<!DOCTYPE html>
<html>
<head>
<style>
.abc{
text-align:center;
background-color: lightgrey;
  width: 500px;
  height:250px;
  border: 5px solid green;
  padding: 50px;
  margin: auto;
}
</style>
<title>Java's Servlet</title>
</head>
<body>
<h1 style="text-align:center">FORM</h1>
<div >
<form action="SubmitForm" method="post" >
<label>First Name</label>
<input type="text"> <br><br>
<label>Last Name</label>
<input type="text"><br><br>
<span>Gender</span>
<input type="radio" name="gender">
<label>Male</label>
<input type="radio" name="gender">
<label>Female</label><br><br>
<button type="submit">Login</button>
<button type="reset">Reset</button>
</form>
</div>
</body>
</html>

Java Code :

package com.sonal;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Form {
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();
        out.println("<h1>Welcome to Servlet</h1>");
    }
}

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>com.sonal.Form</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/SubmitForm</url-pattern>
</servlet-mapping>
</web-app>

The error it's giving is :

Type Exception Report

Message Class [com.sonal.Form] is not a Servlet

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

Exception

javax.servlet.ServletException: Class [com.sonal.Form] is not a Servlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.base/java.lang.Thread.run(Thread.java:832) Root Cause

java.lang.ClassCastException: class com.sonal.Form cannot be cast to class javax.servlet.Servlet (com.sonal.Form is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @24d4d7c9; javax.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader @573f2bb1) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.base/java.lang.Thread.run(Thread.java:832) Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/9.0.65

Can anyone please help me out saying what's wrong here ?

CodePudding user response:

Your class need to extend class javax.servlet.Servlet or javax.servlet.HttpServlet, e.g.

public class Form extends HttpServlet{

  • Related