As promised in my last post, i will describe the process of writing and deploying servlets on Tomcat.
Servlets are java classes which can handle HTTP request and send dynamic response back to the client. Unlike JSP pages, servlets have to be compiled by the developer, wherese JSP pages are first transformed to servlet source files by JSP engine and then compiled to servlet classes by the servlet engine. The advantage of servlet over JSP is they need no compilation time for first time as required by JSP pages.
When deploying a Servlet class we have to put the class file in pre-defined "classes" directory which has to be under the "WEB-INF" directory. We can also put the source file there.
Here is a simple servlet source file, save it as HelloServlet.java under "WEB-INF\classes" directory.
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* The simplest possible servlet.
*
* @author Put Your Name Here
*/
public class HelloServlet 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 Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("Hello World!, I am a servlet");
out.println("</body>");
out.println("</html>");
}
}
Before compiling the servlet we have to set our CLASSPATH correctly. To compile a servlet, the JAR file "servlet-api.jar" containing the required classes has to be in our classpath. It is generally found at "common\lib" directory under Tomcat installation directory. We can either set it in our environment variable or we can declare it in command-line.
Now, we will goto command prompt and compile the servlet using the following command.
This will generate a class file "HelloServlet.class" in the same directory. But, the work is still not over. We have to inform Tomcat about this servlet through our applications configuration file which is "web.xml".
<servlet>
<servlet-name> HelloServlet </servlet-name>
<servlet-class> HelloServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
The contains of <servlet></servlet> tags define a servlets name ands its corresponding class by using the <servlet-name> and <servlet-class> tag. The contains of <servlet-mapping></servlet-mapping> tags define the URL which is mapped to the servlet class. It is required because when a client request a servlet class, it cannot be directly send to the client. So when a client request an URL which point to a servlet Tomcat server executes the servlet for the client and send the output back to the client. The <servlet-name> is used for identifying the servlet and the <url-pattern> tag is used to inform Tomcat that when any request for this URL arrives, executes this servlet and send its output back to the client.
Now, we are ready to test the servlet. Start Tomcat server and point our browser to the URL http://localhost:8080/app1/Hello.
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* The simplest possible servlet.
*
* @author Put Your Name Here
*/
public class HelloServlet 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 Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("Hello World!, I am a servlet");
out.println("</body>");
out.println("</html>");
}
}
Before compiling the servlet we have to set our CLASSPATH correctly. To compile a servlet, the JAR file "servlet-api.jar" containing the required classes has to be in our classpath. It is generally found at "common\lib" directory under Tomcat installation directory. We can either set it in our environment variable or we can declare it in command-line.
Now, we will goto command prompt and compile the servlet using the following command.
D:\tomcat5\webapps\app1\WEB-INF\classes>javac -classpath %CLASSPATH%;D:\tomcat5\common\lib\servlet-api.jar HelloServlet.java
This will generate a class file "HelloServlet.class" in the same directory. But, the work is still not over. We have to inform Tomcat about this servlet through our applications configuration file which is "web.xml".
So, add the following lines in web.xml between the tags <web-app></web-app>.
<servlet>
<servlet-name> HelloServlet </servlet-name>
<servlet-class> HelloServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
The contains of <servlet></servlet> tags define a servlets name ands its corresponding class by using the <servlet-name> and <servlet-class> tag. The contains of <servlet-mapping></servlet-mapping> tags define the URL which is mapped to the servlet class. It is required because when a client request a servlet class, it cannot be directly send to the client. So when a client request an URL which point to a servlet Tomcat server executes the servlet for the client and send the output back to the client. The <servlet-name> is used for identifying the servlet and the <url-pattern> tag is used to inform Tomcat that when any request for this URL arrives, executes this servlet and send its output back to the client.
Now, we are ready to test the servlet. Start Tomcat server and point our browser to the URL http://localhost:8080/app1/Hello.