1. Write a Java program on Servlet life cycle.
Six Steps to Running Your First Servlet
Once Tomcat is installed and configured, you can put it to work. Six steps take you
from writing your servlet to running it. These steps are as follows:
1. Create a directory structure under Tomcat for your application.
a) Create a directory called myApp under the webapps directory. The directory
name is important because this also appears in the URL to your servlet.
b) Create the src and WEB-INF directories under myApp, and create a directory
named classes under WEB-INF. The src directory is for your source files, and
the classes directory under WEB-INF is for your Java classes. If you have
html files, you put them directly in the myApp directory.
2. Write the servlet source code. You need to import the javax.servlet package and
the javax.servlet.http package in your source file.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class TestingServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Servlet Testing</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Testing Center");
out.println("</BODY>");
out.println("</HTML>");
}
}
save your TestingServlet.java file to the src subdirectory under myApp. or can place
the source files anywhere.
3. Compile your source code.
javac -d ..\WEB-INF\classes\ TestingServlet.java
-d option specifies where to place the generated class files.
4. Create a deployment descriptor.
A deployment descriptor is an optional component in a servlet application,
taking the form of an XML document called web.xml. The descriptor must be
located in the WEB-INF directory of the servlet application.
WEB.XML FILE
<web-app>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Testing</servlet-name>
<url-pattern>Testing</url-pattern>
</servlet-mapping>
</web-app>
5. Run Tomcat.
If it is not already running, you need to start Tomcat
6. Call your servlet from a web browser.
If you run the web browser from the same computer as Tomcat, you can
replace domain-name with localhost. Therefore, the URL for your servlet would
be http://localhost:8080/myApp/servlet/Testing.
In the deployment descriptor you wrote in Step 4, you actually mapped the servlet
class file called TestingServlet with the name "Testing" so that your servlet can
be called by specifying its class file (TestingServlet) or its name (Testing).
Without a deployment descriptor, your servlet must be called by specifying its
class name; that is, TestingServlet.
2. Write a Java program to handle HTTP requests and responses.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HttpServletExample extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<html><body>");
pw.println("<h1>doPost() method is invoked</h1>");
pw.println("</body></html>");
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("<html><body>");
pw.println("<h1>doGet() method is invoked</h1>");
pw.println("</body></html>");
}
}