The two-tiered client-server model has been superseded by the multi-tiered architecture prevalent in the enterprise applications
Benefits of having a tiered application
Encapsulates rules and functionality together providing for easier maintenance & development
Enhances flexibility and reusability of logic and software components
Allows developers to focus on the area of their specialty e.g. database, servers, web page, etc.
Server Side Development Technologies
Servlet
JSP
JDBC
Servlet
A server side Java based programming language widely used to create dynamic web applications. Servlet runs inside a servlet container.
Execution of Servlets
1. Client sends a request
2. Web server receives the request
3. Sends to corresponding servlet
4. Servlet processes the request
5. Response to the server
6. Response to the client
Servlet Life Cycle
1. Servlet is initialized by calling the init() method
2. Servlet calls service() method to process a client's request
3. Servlet is terminated by calling the destroy() method
4. Servlet is garbage collected by the garbage collector of the JVM
Servlet API Packages
javax.servlet
javax.servlet.http
Servlet Example
HelloWorld
Deployment Descriptor (web.xml)
A xml file where <web-app> is the root element. Web server uses web.xml file to map the URL of the request to the specific code that handles the request.
Deployment Descriptor (web.xml)
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
<url-pattern>/HelloWorld</url-pattern>
Using Annotations
Annotations and web.xml are two ways to configure Java Servlets. Annotations are more convenient and prevent you from having bloated web.xml with hundreds of entries (in case of bigger application). It is also a part of Convention over configuration approach.
@WebServlet Annotation
The @WebServlet annotation is used to declare a servlet. The annotated class must extend the javax.servlet.http.HttpServlet class.
Using Annotations
@WebServlet("/processForm")
public class MyServlet extends HttpServlet { ... }
Servlet Annotations
displayName
description
smallIcon
largeIcon
Servlet Request Methods
getParameter()
getParameterMap()
getParameterValues()
getQueryString()
getRequestURI()
getRequestURL()
getMethod()
getRemoteAddr()
getRemoteHost()
getServerName()
getServerPort()
Servlet Response Methods
setContentType()
setStatus()
setHeader()
setIntHeader()
setDateHeader()
getWriter()
getOutputStream()
Servlet Request and Response
HTML form with action="serv" method="get"
Servlet code to get request parameters and print response
Servlet Request Dispatcher
RequestDispacther is an interface used to receive requests from the users and bind it with other files such as HTML file, Servlet file, JSP file etc. Servlet container is responsible to create RequestDispatcher object. RequestDispacther provides forward() and include() methods.
Servlet Request Dispatcher
HTML form with action="serv" method="post"
Servlet code to check password length and forward/include request
Servlet Redirection
The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It works at client side because it uses the url of the browser to make another request.
Servlet Redirection
Servlet code to redirect to google.com
Servlet code to redirect to another servlet named "redirected"
Servlet Session Management
A session is a sequence of related requests and responses between a Web client and a Web server. HTTP is a state-less protocol, so session management is required to remember previous communications.
Session Tracking Mechanisms
HttpSession
Cookies
HttpServletResponse
Servlet interface that provides methods to send a response to the client