Sevlet
Sevlet
The full functionality of the Java class libraries is available to a servlet. It can
communicate with applets, databases, or other software via the sockets and
RMI mechanisms that you have seen already.
Servlets Architecture
The following diagram shows the position of Servlets in a Web Application.
Else
The web container calls the destroy method when it needs to remove the servlet
such as at time of stopping server or underlying the project.
How web container handles the servlet request?
The web container is responsible to handle the request. Let's see how it handles the
request.
The public service method internally calls the protected service method
The protected service method calls the doGet method depending on the type
of request.
The doGet method generates the response and it is passed to the client.
After sending the response, the web container deletes the request and
response objects. The thread is contained in the thread pool or deleted
depends on the server implementation.
Advantage of Servlet: - There are many advantages of servlet over CGI. The
web container creates threads for handling the multiple requests to the servlet.
Threads have a lot of benefits over the Processes such as they share a common
memory area, lightweight, cost of communication between the threads are low. The
basic benefits of servlet are as follows:
1. Better performance: because it creates a thread for each request not
process.
2. Portability: because it uses java language.
Setting UP
A development environment is where you would develop your Servlet, test them
and finally run them.
Like any other Java program, you need to compile a servlet by using the Java
compiler javac and after compilation the servlet application, it would be deployed
in a configured environment to test and run.
This development environment setup involves following steps:
Setting up Java Development Kit
This
step
involves
downloading
an
implementation
of
the
Java
Software
Once you downloaded the installation, unpack the binary distribution into a
convenient location. For example in C:\apache-tomcat-5.5.29 on windows,
or /usr/local/apache-tomcat-5.5.29 on Linux/Unix and create CATALINA_HOME
environment variable pointing to these locations.
or
C:\apache-tomcat-5.5.29\bin\startup.bat
Tomcat can be started by executing the following commands on Unix (Solaris,
Linux, etc.) machine:
$CATALINA_HOME/bin/startup.sh
or
/usr/local/apache-tomcat-5.5.29/bin/startup.sh
After startup, the default web applications included with Tomcat will be available by
visiting http://localhost:8080/. If everything is fine then it should display
following result:
Further information about configuring and running Tomcat can be found in the
documentation
included
here,
as
well
as
on
the
Tomcat
web
site:
http://tomcat.apache.org
Tomcat can be stopped by executing the following commands on windows
machine:
C:\apache-tomcat-5.5.29\bin\shutdown
Tomcat can be stopped by executing the following commands on Unix (Solaris,
Linux, etc.) machine:
/usr/local/apache-tomcat-5.5.29/bin/shutdown.sh
Setting up CLASSPATH
Since servlets are not part of the Java Platform, Standard Edition, you must identify
the servlet classes to the compiler.
If you are running Windows, you need to put the following lines in your
C:\autoexec.bat file.
set CATALINA=C:\apache-tomcat-5.5.29
set CLASSPATH=%CATALINA%\common\lib\servlet-api.jar;%CLASSPATH%
Alternatively, on Windows NT/2000/XP, you could also right-click on My Computer,
select Properties, then Advanced, then Environment Variables. Then, you would
update the CLASSPATH value and press the OK button.
On Unix (Solaris, Linux, etc.), if you are using the C shell, you would put the
following lines into your .cshrc file.
setenv CATALINA=/usr/local/apache-tomcat-5.5.29
setenv CLASSPATH $CATALINA/common/lib/servlet-api.jar:$CLASSPATH
NOTE: Assuming that your development directory is C:\ServletDevel (Windows)
or /usr/ServletDevel (Unix) then you would need to add these directories as well in
CLASSPATH in similar way as you have added above .
A GET request results from a normal request for a URL or from an HTML form that
has no METHOD specified and it should be handled by doGet() method.
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The doPost() Method
A POST request results from an HTML form that specifically lists POST as the
METHOD and it should be handled by doPost() method.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The destroy() Method
The destroy() method is called only once at the end of the life cycle of a servlet.
This method gives your servlet a chance to close database connections, halt
background threads, write cookie lists or hit counts to disk, and perform other such
cleanup activities.
After the destroy() method is called, the servlet object is marked for garbage
collection. The destroy method definition looks like this: Java Servlets
public void destroy() {
// Finalization code...
}
1. GenericServlet
2. ServletInputStream
3. ServletOutputStream
4. ServletRequestWrapper
5. ServletResponseWrapper
6. ServletRequestEvent
7. ServletContextEvent
8. ServletRequestAttributeEvent
9. ServletContextAttributeEvent
10.ServletException
11.UnavailableException
3.5 Session
Session simply means a particular interval of time.
Session Tracking is a way to maintain state (data) of an user. It is also known
as session management in servlet.
Http protocol is a stateless so we need to maintain state using session tracking
techniques. Each time user requests to the server, server treats the request as the
new request. So we need to maintain the state of an user to recognize to particular
user.
HTTP is stateless that means each request is considered as the new request. It is
shown in the figure given below:
A webserver can assign a unique session ID as a cookie to each web client and for
subsequent requests from the client they can be recognized using the recieved
cookie.
This may not be an effective way because many time browser does not support a
cookie, so I would not recommend to use this procedure to maintain the sessions.
Hidden Form Fields:
A web server can send a hidden HTML form field along with a unique session ID as
follows:
<input type="hidden" name="sessionid" value="12345">
This entry means that, when the form is submitted, the specified name and value
are automatically included in the GET or POST data. Each time when web browser
sends request back, then session_id value can be used to keep the track of different
web browsers.
This could be an effective way of keeping track of the session but clicking on a
regular (<A HREF...>) hypertext link does not result in a form submission, so hidden
form fields also cannot support general session tracking.
URL Rewriting:
You can append some extra data on the end of each URL that identifies the session,
and the server can associate that session identifier with data it has stored about
that session.
For example, with http://tutorialspoint.com/file.htm;sessionid=12345, the session
identifier is attached as sessionid=12345 which can be accessed at the web server
to identify the client.
URL rewriting is a better way to maintain sessions and works for the browsers when
they don't support cookies but here drawback is that you would have generate
every URL dynamically to assign a session ID though page is simple static HTML
page.
The HttpSession Object:
Apart from the above mentioned three ways, servlet provides HttpSession Interface
which provides a way to identify a user across more than one page request or visit
to a Web site and to store information about that user.
The servlet container uses this interface to create a session between an HTTP client
and an HTTP server. The session persists for a specified time period, across more
than one connection or page request from the user.
Types of Cookie
There are 2 types of cookies in servlets.
1. Non-persistent cookie
2. Persistent cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user closes the
browser.
Persistent cookie
It is valid for multiple session . It is not removed each time when user closes the
browser. It is removed only if user logout or signout.
Advantage of Cookies
1. Simplest technique of maintaining the state.
2. Cookies are maintained at client side.
Disadvantage of Cookies
1. It will not work if cookie is disabled from the browser.
Description
Cookie()
constructs a cookie.
Description