An overview of java servlets, including their advantages over cgi, setup process on personal computers and afs, and security considerations. It covers topics such as servlet mechanics, servlet engines, servlet advantages, comparing servlets to cgi, servlet initialization, and implementing servlets on personal computers and afs.
Download Java Servlets: Advantages, Setup, and Security and more Slides Java Programming in PDF only on Docsity!
Java Servlets
Warning
If you are implementing servlets using Tomcat on AFS you will have to wait until the server is restarted before your code can be tested.
It will not work until the server is restarted at 6 a.m., 2 p.m., and 8 p.m. daily. It takes about half an hour to restart each time as Tomcat has to check about 5,000 home directories for JSP and servlets. See the last slide in this lecture for more information on using Tomcat on AFS.
Servlet Definition
Servlets are protocol- and platform-independent server side components, written in Java, which dynamically extend Java-enabled servers.
They have become more and more popular as they benefit from all the advantages of the Java programming language in particular Architecture and Platform Independence and the ability to build robust and secure applications.
The Servlet Interface
A Servlet, in its most general form, is an instance of a class, which implements the javax.servlet.Servlet interface.
Most Servlets, extend one of the standard implementations of that interface, usually javax.servlet.http.HttpServlet.
HTTP Servlets extend the javax.servlet.http.HttpServlet class.
Servlet Definition
A Servlet, in simple terms, is a Java program running under a web server taking a 'request' object as an input and responding back by a 'response' object. Typically a web browser will send the request in HTTP format. The Servlet container will convert that into a request object. Similarly the response object - populated by the Servlet is converted into an HTTP response by the Servlet container.
Servlet Definition
This mechanism makes the browser - web server interaction very easy. Java Servlets are more efficient, easier to use, more powerful, more portable, and cheaper than traditional CGI and many alternative CGI-like technologies.
(More importantly, Servlet developers tend to get paid more than Perl programmers :-).
Servlet Mechanics
• A servlet is a Java class and thus needs to be
executed in a Java Virtual Machine by a
service called a servlet engine. The servlet
engine loads the servlet before it can be used.
The servlet then stays loaded until it is
unloaded or the servlet engine is shut down.
Servlet Engines
• Some web servers, including Sun Java Web
Server, W3C Jigsaw, and Gefion LiteWebServer
are implemented in Java and have a built in
servlet engine. High end commercial servers
like Websphere and Web Logic tend to include a
servlet engine. Other web servers such as
Microsoft IIS and Apache require a servlet add-
on module which must be loaded separately.
Servlet Advantages
Possibly the biggest advantage of Java Servlets is that they run within the context of the server. This along with the multithreading characteristic of Java means that the server doesn’t have to begin a new process for every request made. Instead, the server creates a single instance of the Servlet, which handles all client requests, thus allowing Servlets to out perform both the CGI approach and the Fast-CGI approach.
Comparing Servlets to CGI
Servlets are more efficient. With traditional CGI, a new process is started for each HTTP request. If the CGI program does a relatively fast operation, the overhead of starting the process can dominate the execution time.
With servlets, the Java Virtual Machine stays up, and each request is handled by a lightweight Java thread, not a heavyweight operating system process.
Comparing Servlets to CGI
Java Servlets can do things that are difficult or impossible with regular CGI: - Talk directly to the Web server. This
simplifies operations that need to look up
images and other data stored in standard
places.
Share data with other Servlets, making
useful things like database connection
pools easy to implement.
Maintain information from request to
request, simplifying things like session
tracking and caching of previous
computations.
Java Web Server
In order to run Java Servlets you will need the JavaWebServer. If you do use the JavaWebServer you will need Third-party Servlet containers. Java can be difficult for some programmers to learn.
Init Method
A Servlet is a Java class that implements the Servlet interface. This interface has three methods that define the Servlet's life cycle:
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.
Service Method
public void service(ServletRequestrequest, ServletResponse response) throwsServletException, 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.