The requested resource is not available in Tomcat 8.0.2

4.1k views Asked by At

wel.html file

<html>
<head><title>Welcome Page</title></head>
<body>
Welcome HTML Page
<form action="Welcome" method="post">
<input type="submit" value="submit"/>
</form>
</body>
</html>

web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>S1</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>S1</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
</web-app>

Welcome.java Servlet file

import java.io.*;
import javax.servlet.*;

public class Welcome implements Servlet
{
ServletConfig config;

public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletException
{
System.out.println("...init...");
this.config=config;
}

public javax.servlet.ServletConfig getServletConfig() 
{
System.out.println("...getServletConfig...");
return config;
}

public void service(javax.servlet.ServletRequest req,javax.servlet.ServletResponse res) throws javax.servlet.ServletException,java.io.IOException
{
System.out.println("...service...");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Welcome</h1>");
out.println("</body>");
out.println("</html>");
}

public java.lang.String getServletInfo()
{

System.out.println("...getServletInfo...");
return "";
}

public void destroy()
{
System.out.println("...destroy...");
}

}

Directory Structure C:\apache-tomcat-8.0.22\webapps\MyApp\WEB-INF\classes

web.xml in WEB-INF

compiled java class in classes folder

wel.html in MyApp folder

When I deploy the project,its working upto wel.html.But after clicking submit button,the following error page is getting displayed

HTTP Status 404 - /MyApp/Welcome

type Status report

message /MyApp/Welcome

description The requested resource is not available.

Apache Tomcat/8.0.22

I am at a loss as to what is causing this problem.Please help me. Thanks in advance.

2

There are 2 answers

6
John Kuhns On

It appears to work just fine for me, Tomcat 8.0.23 and Oracle JDK 1.8.0_45, using copied and pasted code from your post. Guesses at your problem would be your class is not being deployed or that it is someplace other than the default package. Try adding it to a package and modify your servlet mapping to reflect the qualified class name.

0
mmalik On

I also copied and pasted to a new WebApp Project in Netbeans and everything works fine. Make sure that you made no typos:

  • do you have web.xml and not, e.g. web.xml.xml file?
  • do you have Welcome.class file in WEB-INF\classes folder?

Except for that, you don't use your imports consistently. When you invoke import at the beginning, just use ServletConfig config instead javax.servlet.ServletConfig config over and over again.