Reading:  

Introduction to Servlet technology


Packaging

All Java web application has standard WEB-INF subdirectory which is also supported by Servlet API. Under webapps folder we have top-level directory name as helloservlet. The directory structure looks like as below:

WEB-INF Folder tomcat

The WEB-INF subdirectory have application's deployment descriptor know as web.xml.

Creating Servlets in Packages:

The directory WEB-INF/classes contain all servlet classes and other class files which follow a structure with matching package name.

Servlet packaging

 

Following is example to create HelloWorldServlet class with a package name mypackage.

package mypackage; 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class HelloWorldServlet extends HttpServlet {
   @Override
   public void doGet(HttpServletRequest request, HttpServletResponse response)
               throws IOException, ServletException {
    
      response.setContentType("text/html;charset=UTF-8");

      PrintWriter out = response.getWriter();
 
      try {
         out.println("<!DOCTYPE html>");
         out.println("<html><head>");
         out.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
         out.println("<title>Hello World</title></head>");
         out.println("<body>");
         out.println("<h1>Hello world!</h1>");  
      
      } finally {
         out.close();  
      }
   }
     public void destroy()
  {
      // do nothing.
  }
}

 

Compiling Servlets in Packages:

Go to file path webapps\helloservlet\WEB-INF\classes\mypackage and compile the file as follow:

$ javac HelloWorldServlet.java

Packaged Servlet Deployment:

By default  servlet application is located at path <Tomcat-installation-directory>/webapps/ROOT and class file would reside in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="3.0"
  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">
 
   <servlet>
      <servlet-name>HelloWorldServlet</servlet-name>
      <servlet-class>mypackage.HelloWorldServlet</servlet-class>
   </servlet>  
   <servlet-mapping>
      <servlet-name>HelloWorldServlet</servlet-name>
      <url-pattern>HelloWorldServlet</url-pattern>
   </servlet-mapping>
</web-app>
 

Restart the apache server and access the below link to get output.

http://localhost:8080/examples/servlets/servlet/HelloWorldExample

Description

This guide introduces Servlet technology and we will cover below topics

  1. What is a Servlet?
  2. Initial Setup
  3. Life Cycle
  4. Various Examples
  5. Client Request
  6. Server Response
  7. Http Status Codes
  8. Writing Filters
  9. Exception Handling
  10. Cookies Handling
  11. Session Tracking
  12. Database Access
  13. Packaging
  14. Internationalization

This is to the point introduction to the topic to get you started

 



Prerequisites

Working knowledge of Core Java is essential

Audience

Beginner to Java Servlet technology or students looking to brush up their skills quickly

Learning Objectives

Learn Java Servlets

Author: Subject Coach
Added on: 2nd May 2015

You must be logged in as Student to ask a Question.

None just yet!