First Servlet, website structure according to J2EE standards.

To carry out the deployment of a web application in Tomcat we have to create a folder with the name of the application, inside webapps\. In the last tutorial we have already created the application "first-servlet" and we have included a static page "welcome.html".

According to the J2EE standars, a web application must have a structure of folders and a file named web.xml as we can see in the following diagram:

J2EE is a committee made up of business java companies, which define standards so that the products of the different companies are compatible.

The static web pages are situated inside de application folder (first-servlet\ in our case), but never inside the folder WEB-INF\ because this can be access from the Browser.

Inside the folder WEB-INF\classes\ we will include the compilated java classes which are part of the application (including the servlets) In the folder:WEB-INF\lib\, we can include the necessary libraries (archivos .jar) for the application.

We are going to create a Servlet called com.edu4java.servlets.FirstServlet with the following source code:

package com.edu4java.servlets;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Date;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class FirstServlet extends HttpServlet{

 

 @Override

 protected void doGet(HttpServletRequest req, HttpServletResponse resp)

                  throws ServletException, IOException {

           

            PrintWriter out = resp.getWriter();

            out.println("<html>");

            out.println("<body>");

            out.println("hoy es " + new Date());

            out.println("</body>");

            out.println("</html>");

 }

}

 

The class FirstServlet extends from javax.servlet.http.HttpServlet and overwrites the method doGet(HttpServletRequest req, HttpServletResponse resp) which is called when the Http Server receives a GET type request from the Browser (for an explication of the methods GET and POST, see the tutorial Web Server or HTTP Server).

The method doGet receives two parameters, the first one is an object type HttpServletRequest which contains all the information about the request of the web page and the second one is an object of the type HttpServletResponse which is used to fill in the response which will be sent back to the Browser.

In the source code we can see how we obtain an object java.io.PrintWriter from the object HttpServletResponse and we use it to send a web page with the current date obtained from the system using an object type java.util.Date.

To deploy this Servlet in the server we must copy the compiled file; FirstServlet.class in the folder WEB-INF\Classes\ as we can see below:

We have to configurate the file; web.xml so that Tomcat knows when to call the Servlet "FirstServlet". We are going to configurate it so that it is called when somebody asks for http://localhost:8080/first-servlet/what-time-is-it

<web-app>

   <servlet>

      <servlet-name>timeservlet</servlet-name>

      <servlet-class>com.edu4java.servlets.FirstServlet</servlet-class>

   </servlet>

   <servlet-mapping>

      <servlet-name>timeservlet</servlet-name>

      <url-pattern>/what-time-is-it</url-pattern>

   </servlet-mapping>

</web-app>

<web-app> is the first element in web.xml. Inside, the element; <servlet>, indicates that a Servlet from the class com.edu4java.servlets.FirstServlet will be created, with the internal name timeservlet. The element; <servlet-mapping> will associate the URL /what-time-is-it to the Servlet, with internal name; timeservlet.

Lastly we have to restart the Tomcat server; close the server ms-dos window, execute startup.bat again

and point a Browser to http://localhost:8080/first-servlet/what-time-is-it

<< What is a Servlet Container. Apache Tomcat installation. Parameters, GET and POST methods in Servlets. >>