Servlet Overview
Servlet Overview
1. What are Servlets?
2. When it will be useful?
3. Advantage of Servlets over CGI.
4. Tools Requirement.
5. Setting up Environment for that.
6. Directory Structure of Servlets.
7. Servlets Life Cycle.
8. What is web.xml?
9. Important Packages of Servlet.
10. Simple “Hello World” program using Servlet.
11. How to Run Servlet
Servlet at a Glance
● What are Java Servlets?
Servlets are Java technology's answer to CGI programming. They are
programs that run on a Web server and build Web pages. It solves the
performance problem by executing all requests as threads in one process, or
in a loadbalanced system.
● When it will be useful?
a) The Web page is based on data submitted by the user.
b) The data changes frequently.
● Advantages of Servlets over CGI
1. Efficient
2. Convenient
3. Powerful
4. Inexpensive
● Tools Requirement
1. J2SDK1.5 ( or higher)
2. Apache Tomcat Server 5.0
3. Servlet.jar
● Setting up Environment Variable
1. java's home directory's location defined in JAVA_HOME variable.
2. configure apache tomcat by CATALINA_HOME variable assign
tomcat's home directory
3. define Servlet.jar file's path in PATH variable, must be take care that
existing path must not must be lost.
● Directory Structure for Servlet
1. Application Directory
1.1 WEBINF
1.1.1 Web.xml
1.1.2 classes
1.1.2.1 *.class
1.1.3 lib
1.1.3.1 *.jar
1.1.4 tlds
1.1.4.1 *.tld
1.2 METAINF
1.2.1 MANIFIST.MF
1.3 *.jsp files
1.4 *.html files
● Servlet Life Cycle
1. public void init(ServletConfig config) throws
ServletException
This method is called once when the servlet is loaded into the servlet
engine, before the servlet is asked to process its first request.
2.public void service(ServletRequest request,
ServletResponse response) throws ServletException,
IOException
This method is called to process a request. It can be called zero, one or
many times until the servlet is unloaded. Multiple threads (one per
request) can execute this method in parallel so it must be thread safe.
3. public void destroy()
This method is called once just before the servlet is unloaded and taken
out of service.
● Role of Web.xml
<?xml version="1.0" encoding="ISO88591"?>
<webapp>
<servlet>
<servletname>TestingServlet</servletname>
<servletclass>TestingServlet</servletclass>
</servlet>
<servletmapping>
<servletname>TestingServlet</servletname>
<urlpattern>/servlet/TestingServlet</urlpattern>
</servletmapping>
</webapp>
● Important Packages
The javax.servlet package contains interfaces and classes intended to
be protocol independent and the javax.servlet.http package contains
HTTP specific interfaces and classes.
● Simple “Hello World” example using Servlet
package bank;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestingServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
● HTML code for calling Servlet
<a href="servlet/TestingServlet"><img
SRC="images/execute.gif" HSPACE=4 BORDER=0
align=TOP></a><a href="servlet/TestingServlet">Execute</a>
● Combinng HTML and .java code into one file.
response.setContentType("text/html");
out.println("<HTML>\n" +
"<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n"
+ "<BODY>\n"
+ "<H1>Hello WWW</H1>\n"
+ "</BODY></HTML>");
● Sending Output to Client
Instead of writing the response to stdout as you do with CGI, you get an
OutputStream or a PrintWriter from the HttpServletResponse. The
OuputStream is intended for binary data, such as a GIF or JPEG image,
and the PrintWriter for text output.
● How to Run?
http://localhost:8080/myApp/
if we combine HTML and .java code then we have to write as
follow
http://localhost:8080/myApp/servlet/TestingServlet