2

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.

user3794853
  • 55
  • 1
  • 8

2 Answers2

1

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.

mmalik
  • 185
  • 2
  • 11
0

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.

John Kuhns
  • 506
  • 4
  • 20
  • I haven't placed the java code in any package.Can you please elaborate on the default package.Thank you – user3794853 Jun 19 '15 at 17:30
  • Are you using some IDE to develop this? Eclipse or Netbeans? It is considered a best practice to place classes in packages which would have the effect of adding them to subfolders under the WEB-INF/classes folder. I put Welcome.class in the WEB-INF/classes folder and used the same web.xml and wel.html and it worked, so one guess may be that your class file is actually being deployed to someplace else, or not at all. – John Kuhns Jun 19 '15 at 17:49
  • I am not using any IDE.I have placed the Welcome.class in WEB-INF/classes folder.There are no other subfolders in classes folder – user3794853 Jun 19 '15 at 17:55
  • Can you try the action again and then post the log file that should be in C:\apache-tomcat-8.0.22\logs? Probably named catalina.out. Sounds like you may be missing a dependency.... – John Kuhns Jun 19 '15 at 18:05
  • 19-Jun-2015 23:28:45.901 INFO [http-apr-3010-exec-98] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/MyApp] has started 19-Jun-2015 23:28:46.253 INFO [http-apr-3010-exec-98] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/MyApp] is completed – user3794853 Jun 19 '15 at 18:08
  • Given everything here, it all should work. Try recompiling the class and replacing the Welcome.class with the new one, restart Tomcat and see if that helps, otherwise I'm out of ideas. – John Kuhns Jun 19 '15 at 19:08