Issue
When I run my project it shows 404 error. To run successful I have to type my class name in the URL. My Servlet class First.java
import java.io.*;
import javax.servlet.*;
@WebServlet("/First")
public class First implements Servlet {
public void init(ServletConfig req) throws ServletException {
System.out.println("Initial");
}
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("Service");
}
public void destroy()
{
}
}
My XML file web.xml
<?xml version="1.0" encoding="UTF-8"?>
<element>
<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">
<display-name>FirstProject</display-name>
<servlet>
<servlet-name>firstServlet</servlet-name>
<servlet-class>First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstServlet</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
</element>
Like
http://localhost:8085/FirstProject/
shows 404 error but
http://localhost:8085/FirstProject/First
provide the desired output. When I click submit button in my login project it must provides desired output page but it didn't show. I have to type the servlet URL mapping name. Why this is happens please help me!
Solution
http://localhost:8085/FirstProject/First is the correct URL. The format will be like the following:
http://<IP_ADDRESS>:<PORT>/<CONTEXT_NAME>/<ADAPTER_NAME>
ADAPTER_NAME is the name we give as the url-pattern in the servlet mapping. CONTEXT_NAME is the ProjectName or the WAR name usually.
Answered By - Bruce wayne - The Geek Killer