Hot questions for Using Ubuntu in servlets

Question:

I'm new to servlets. I've installed Tomcat7 in Ubuntu14.04 and the service runs fine. But it gives 404(not found) error for servlet (http://localhost:8080/hello/HelloServlet). The html within the application runs fine. So are the Tomcat managerial tools.

Any idea of what I'm doing wrong?

Tomcat webapps structure: /var/lib/tomcat7/webapps

webapps
|-- ROOT
    |-- hello
    |   |-- index.html
    |   `-- WEB-INF
    |       |-- classes
    |       |   |-- HelloServlet.class
    |       |   `-- HelloServlet.java
    |       |-- lib
    |       `-- web.xml
    |-- index.html
    `-- META-INF
        `-- context.xml

web.xml:

<?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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">
<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
     <servlet-name>HelloServlet</servlet-name>
     <url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>

HelloServlet.java:

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

@WebServlet(name="HelloServlet", urlPatterns={"/HelloServlet"})

public class HelloServlet extends HttpServlet{
  private String message;
  public void init() throws ServletException{
      message = "Hello World";
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
  }
  public void destroy(){

  }
}

Answer:

Finally I got it running :)

It was the directory structuring that was creating the problem. The application directory should be just outside the 'webapps/ROOT' directory. To be specific, just put the application directory directly under the 'webapps' (Seems to be specific case for Tomcat7 and Ubuntu 14.04LTS)

The corrected structure:

webapps
|-- ROOT
`-- hello
    |-- index.html
    `-- WEB-INF
        |-- classes
        |   |-- HelloServlet.class
        |   `-- HelloServlet.java
        |-- lib
        `-- web.xml

The previous structure (with error):

webapps
|-- ROOT
    |-- hello
    |   |-- index.html
    |   `-- WEB-INF
    |       |-- classes
    |       |   |-- HelloServlet.class
    |       |   `-- HelloServlet.java
    |       |-- lib
    |       `-- web.xml
    |-- index.html
    `-- META-INF
        `-- context.xml

Question:

I am having this very weird issue.

I have one servlet in which I want to access one system variable.

As per this documentation I have set my environment variable in /etc/environment as

TEMP=Temp.something

Now when I do printenv TEMP I get

Temp.something

Also I have wrote simple program like this

public class env {
        public static void main(String[] args) {
                String test = System.getProperty("TEMP");
                System.out.println(test);
        }
}

which outputs

Temp.something

Now, I have wrote one servlet which is deployed on tomcat on the same machine, which has this statement

System.out.println("Config : " + System.getenv("TEMP"));

This always prints null. I'm pretty new to servlets. Is something I'm missing?


Answer:

I had to make entry inside /usr/share/tomcat7/bin/setenv.sh

TEMP=Temp.something

I'm not sure why do I had to do this. When I say get system environment variable it should get me system variable :(