Question

i am new to java servlet programing.I installed tomcat and it is running.But while calling a jsp file i am getting the below error

The requested resource is not available.

i kept jsp file in the path C:\apache-tomcat-7.0.52\webapps\Pradeeep_prctice

the url i am trying is : localhost:8080/Pradeeep_prctice/Helloworld.jsp

my programcode is

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

public class HelloWorld extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello World!</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

please find my web.xml file

<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"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
<servlet>
    <servlet-name>Helloworld</servlet-name>
    <servlet-class>Pradeeep_prctice.Helloworld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Helloworld</servlet-name>
    <url-pattern>/Helloworld</url-pattern>
  </servlet-mapping>
</web-app>
Was it helpful?

Solution

You have not configured your HelloWorld servlet in Deployment Descriptor (web.xml) file.

Deployment Descriptor (web.xml) file :

  • A web application's deployment descriptor describes the classes, resources and configuration of the application and how the web server uses them to serve web requests. When the web server receives a request for the application, it uses the deployment descriptor to map the URL of the request to the code that ought to handle the request.

  • The deployment descriptor is a file named web.xml. It resides in the app's WAR under the WEB-INF/ directory. The file is an XML file whose root element is <web-app>

  • web.xml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method (e.g. the doGet() method for HTTP GET requests).

  • To map a URL to a servlet, you declare the servlet with the <servlet> element, then define a mapping from a URL path to a servlet declaration with the <servlet-mapping> element.

  • The <servlet> element declares the servlet, including a name used to refer to the servlet by other elements in the file, the class to use for the servlet, and initialization parameters. You can declare multiple servlets using the same class with different initialization parameters. The name for each servlet must be unique across the deployment descriptor.

Your file should be

<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" metadata-complete="true">
  <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>yourpakage.HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
  </servlet-mapping>
</web-app>

References

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top