“HTTP Status 404 The requested resource is not available”
The issue is caused by a conflict between web.xml configuration and the WebServlet annotation. The web.xml is delcaring a servlet called login that target to the /reg url, but also in the RegistrationServlet there is a declaration throught @WebServlet annotation that reference to the same url /reg.
One possible solution is to remove the servlet declaration from the web.xml, that means that the web.xml content should be like this.
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_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Adhar</display-name>
<welcome-file-list>
<welcome-file>form1.html</welcome-file>
</welcome-file-list>
</web-app>
Just let the Servlet declared by annotation only. Hope this helps.
Java Servlet, http status 404, The requested resource is not available
You have missed @WebServlet("/HelloWorld")
. Given below is the completed code:
package mypkg;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/sayhello")
public class HelloWorld extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));
int sum = num1 + num2;
System.out.println(sum);
PrintWriter out = response.getWriter();
out.println("Addition : " + sum);
}
}
Make sure you have the following root declaration of your web.xml
otherwise it will not be interpreted as Servlet 3.0
which supports @WebServlet
annotation.
<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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
Update:
I can see that you have now added the content of your web.xml
in the question. As per this update, your HTML form action
should be as follows:
<form action="sayhello" method="post">
Servlet 404 the requested resource [/ServletDemo/AddServlet] is not available
You need to override doGet
and doPost
from HttpServlet
. For this reason you need to add @Override
annotation above the doGet
and doPost
.
Related Topics
What Is the Most Appropriate Way to Store User Settings in Android Application
Android/Java - Date Difference in Days
Java Processbuilder: Resultant Process Hangs
How to Avoid Java Code in Jsp Files, Using Jsp 2
Solving a "Communications Link Failure" With Jdbc and MySQL
Retain Precision With Double in Java
How to "Scan" a Website (Or Page) For Info, and Bring It into My Program
Broadcast Receiver For Checking Internet Connection in Android App
Can't Create Directory in Android 10
How to Count the Number of Documents Under a Collection in Firestore
Java Can't Connect to X11 Window Server Using 'Localhost:10.0' as the Value of the Display Variable
Should I Avoid the Use of Set(Preferred|Maximum|Minimum)Size Methods in Java Swing
Why Do I Need to Override the Equals and Hashcode Methods in Java
Convert a String Representation of a Hex Dump to a Byte Array Using Java