Академический Документы
Профессиональный Документы
Культура Документы
Example
This tutorial explains the concept of Applets and Servlets including basic introduction,
comparison between them for better understanding, Life-cycle of applets and servlets with
the examples ...
This approach is made to provide you an easy and better understanding of these concepts.
Basically, Applets and Servlets both are the Java programs. Both have their different uses
and importance for different applications.
These topics are concluded in a Comparison way under different headings which will help
you to understand and remember them easily.
Applets :
Applets are the Java Programs which run in other applications, typically in webpage
displayed in web browser, on the Client machine. Applets are embedded in a HTML Page.
Like the Other Java Programs, Applets don`t have a main() method.
Servlets :
Servlets are the Java Programs which run on the web or application Server.
They extend the functionality of a web server and provide structure for a business
environment.They can be operating system and hardware platform independent.
Servlets process HTTP/HTML requests with all the benefits of the Java language
( portability, performance, re-usability, and crash protection ) from the client and generates
the response for the request as HTML pages.
HTTP Servlet typically used to provide dynamic content like getting the results of a database
query and returning to the client.
Applets Servlets
Require JRE or Web browser`s plug-in to run Require Java Enabled Web Server
Use More Network bandwidth as runs loads and Less Network Bandwidth as runs on Server
executes on Client machine and only Results sent to Client
Servlet Lifecycle :
init() : It intializes servlet after loading into memory. It perform operations done
only once at start-up
- reading a current properties
- clearing log files, notifying other services that the servlet is running.
Example : 2
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
The doGet servlet method is used to obtain the two objects: HttpServletRequest and
HttpServletResponse.
setContentType() method is used as the response object which sets the content type of
the response to text/html.
These examples will display " Welcome to First Sevrlet Program " to the user on visiting the
page.
<web-app>
<servlet>
<servlet-name>Servlet Example</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet Example</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>