Servlet Architecture
Servlet Architecture
1. Client
The client shown in the architecture above is the web browser and
it primarily works as a medium that sends out HTTP requests over
to the web server and the web server generates a response based
on some processing in the servlet and the client further processes
the response.
2. Web Server
Primary job of a web server is to process the requests and
responses that a user sends over time and maintain how a web
user would be able to access the files that has been hosted over
the server. The server we are talking about here is a software
which manages access to a centralized resource or service in a
network. There are precisely two types of webservers:
Static web server
Dynamic web server
3. Web Container
Web container is another typical component in servlet architecture
which is responsible for communicating with the servlets. Two
prime tasks of a web container are:
Managing the servlet lifecycle
URL mapping
Web container sits at the server-side managing and handling all
the requests that are coming in either from the servlets or from
some JSP pages or potentially any other file system.
How does a Servlet Request flow?
Every servlet should override the following 3 methods namely:
1. init(): To initalize/instantiate the servlet container.
2. service(): This method acts like an intermediatory between the
HTTP request and the business logic to serve that particular
request.
3. destroy(): This method is used to deallocate the memory
allocated to the servlet.
These methods are used to process the request from the user.
Following are the steps in which a request flows through a servlet
which can be observed in the architecture diagram:
The client sends over a request.
The request is accepted by the web server and forwarded to the
web container.
In order to obtain the servlet’s address, the web container
traces web.xml file corresponding to the request URL pattern.
By the time above process takes place, the servlet should have
been instantiated and initialized. The init() method is invoked to
initialize the servlet.
By passing ServletRequest and Response
object, public service() method is called by the container.
In the next step, the ServletRequest and ServletResponse objects
are type casted
to HttpServletRequest and HttpServletResponse objects by
the public service() method.
Now protected service() method is called by the public
service() method.
The protected service() method dispatches the request to the
correct handler method based on the type of request.
When servlet container shuts down, it unloads all the servlets
and calls destroy() method for each initialized servlet.
Advantages
Prime functionality of a servlet is that they are independent of
server configuration, and they are pretty much compatible with
any of the web servers.
Servlets are also protocol-independent supporting
FTP, HTTP, SMTP, etc. protocols to the fullest.
Until destroyed manually, servlets can be retained in the
memory helping process several requests over time. Also, once a
database connection is established, it can facilitate process
several requests for a database in the very same database
session.
Servlets inherit Java’s property of portability and hence are
compatible with nearly any web server.
Servlets are first converted into byte codes and then executed,
which helps in increasing the processing time.
Disadvantages
Designing a servlet can be pretty laborious.
Exceptions need to be handled while designing a servlet since
they are not thread safe.
Developers may need additional skills to program a servlet.
The doGet() and doPost() are most frequently used methods with
in each service request. Here is the signature of these two
methods.
Architecture Diagram
The following figure depicts a typical servlet life-cycle scenario.
Database Program
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER,
PASS);
stmt = conn.createStatement();
String sql;
sql = "INSERT INTO Employee (empcode, empname,
designation, department, basic_salary) VALUES ('" + empcode + "', '"
+ empname + "', '" + desig + "', '" + dept + "', '" + basic + "')";
stmt.executeUpdate(sql);
out.println("<html>");
out.println("<head><title>Employee
Details</title></head>");
out.println("<body>");
out.println("<h2>Employee Details</h2>");
out.println("<table border=\"1\">");
out.println("<tr><th>Empcode</th><th>Empname</th><th
>Designation</th><th>Department</th><th>BasicSalary</th></
tr>");
out.println("<tr><td>" + empcode + "</td><td>" + empname
+ "</td><td>" + desig + "</td><td>" + dept + "</td><td>" + basic +
"</td></tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}