Servlet Returns "Http Status 404 the Requested Resource (/Servlet) Is Not Available"

“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



Leave a reply



Submit