0

I'm new to Java servlets and stuff. I run Eclipse Luna SR2 (for Java EE) and configured my project to use tomcat 8 as a server (on Linux Mint 17.1, if it helps).

I created a new project from File -> New -> Dynamic Web Project.

While adding the tomcat 8 server I did not change any of the default params except specifying the tomcat root directory.

Then I imported the servlet-api.jar file (from tomcat/lib directory).

I have one class file in my project which does not have any errors. So when I run tomcat from within Eclipse I get The requested resource is not available.. I know this is very common error and I'd be happy to give the required information.

Here is the class file -

package ch1;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class serv1 {

public void doGet(HttpServletRequest request,
        HttpServletResponse response)
                throws IOException {
    PrintWriter out = response.getWriter();
    java.util.Date today = new java.util.Date();
    out.println("<html>" +
            "<body>" +
            "<h1 align=center>HF\'s Chapter1 Servlet</h1>" +
            "<br>" + today + "</body>" + "</html>");
}
}

This is the directory structure of my project -

.
|-- build
|   `-- classes
|       `-- ch1
|           `-- serv1.class
|-- .classpath
|-- .project
|-- .settings
|   |-- .jsdtscope
|   |-- org.eclipse.jdt.core.prefs
|   |-- org.eclipse.wst.common.component
|   |-- org.eclipse.wst.common.project.facet.core.xml
|   |-- org.eclipse.wst.jsdt.ui.superType.container
|   `-- org.eclipse.wst.jsdt.ui.superType.name
|-- src
|   `-- ch1
|       `-- serv1.java
`-- WebContent
    |-- META-INF
    |   `-- MANIFEST.MF
    `-- WEB-INF
        `-- lib

EDIT: Here's a screenshot - enter image description here

Can anyone help me as to where I got it wrong?

nsane
  • 1,715
  • 4
  • 21
  • 31

1 Answers1

3

You need put web.xml file in WEB-INF directory. Tomcat doesn't know anything about your servlet without mapping from web.xml file

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> 

        <!-- Name the application --> 
        <display-name>ch1</display-name> 
        <description>An example application which is used to play with some of the features of Tomcat</description> 

        <!-- ========================================================== --> 
        <!-- Servlets --> 
        <!-- ========================================================== --> 

        <!-- Simple Servlet, provide a name, class, description and map to URL /servlet/SimpleServlet --> 
        <servlet> 
                <servlet-name>Simple</servlet-name> 
                <servlet-class>ch1.serv1</servlet-class> 
                <description>This is a simple Hello World servlet</description> 
        </servlet> 
        <servlet-mapping> 
                <servlet-name>Simple</servlet-name> 
                <url-pattern>/*</url-pattern> 
        </servlet-mapping> 

        <welcome-file-list> 
                <welcome-file>index.html</welcome-file> 
        </welcome-file-list> 

</web-app>

Put in

<servlet-class>ch1.serv1</servlet-class>   

full class name with package name

Also you need extend your class from HttpServlet and then override #doGet() method.

Then try get your page by url http://localhost:8080/ch1