0

I am trying to run a simple program for inputting a number in a form and displaying it using servlet.

This is the file structure of a simple Servlet program using TomCat10.0

enter image description here

web.xml which connects the 2 files

<?xml version="1.0" encoding="UTF-8"?>
<web-app  xmlns="http://JAVA.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"   version="2.4" >
    <servlet>
        <servlet-name>com.one.first</servlet-name>
        <servlet-class>com.one.first</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>com.one.first</servlet-name>
        <url-pattern>/first</url-pattern>
    </servlet-mapping>
</web-app>

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="first">
        <input type="text" name="num1" /><br>
        <input type="Submit"/><br>
    </form>
</body>
</html>

first.java

package com.one;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class first extends HttpServlet{

    public void service(HttpServletRequest req, HttpServletResponse resp) {
        
        int i = Integer.parseInt(req.getParameter("num1"));
        
        System.out.println("Result is:" + i);
        
    }
}

Error While I submit the index.html form

INFO: Server startup in [808] milliseconds
Aug 29, 2021 4:14:06 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet [one] as unavailable
Aug 29, 2021 4:14:06 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet [one]
java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
Dhruv Mistry
  • 355
  • 1
  • 2
  • 6

0 Answers0