Tomcat-Server

Java-Examples

                                                                                                                                                                                                                    Show all | Close all
  • Overview

    • What is server

      In most common use, a server is a physical computer (a computer hardware system) dedicated to run one or more services (as a host), to serve the needs of the users of other computers on the network. Depending on the computing service that it offers it could be a database server, file server, mail server, print server, web server, gaming server, or some other kind of server. In the context of client-server architecture, a server is a computer program running to serve the requests of other programs, the "clients". Thus, the "server" performs some computational task on behalf of "clients". The clients either run on the same computer or connect through the network. In the context of Internet Protocol (IP) networking, a server is a program that operates as a socket listener. Servers often provide essential services across a network, either to private users inside a large organization or to public users via the Internet.
    • Usage

      The term server is used quite broadly in information technology. Despite the many server-branded products available (such as server versions of hardware, software or operating systems), in theory any computerised process that shares a resource to one or more client processes is a server. To illustrate this, take the common example of file sharing. While the existence of files on a machine does not classify it as a server, the mechanism which shares these files to clients by the operating system is the server. Similarly, consider a web server application (such as the multiplatform "Apache HTTP Server"). This web server software can be run on any capable computer. For example, while a laptop or personal computer is not typically known as a server, they can in these situations fulfill the role of one, and hence be labelled as one. It is, in this case, the machine's role that places it in the category of server. In the hardware sense, the word server typically designates computer models intended for hosting software applications under the heavy demand of a network environment. In this client-server configuration one or more machines, either a computer or a computer appliance, share information with each other with one acting as a host for the other[s]. While nearly any personal computer is capable of acting as a network server, a dedicated server will contain features making it more suitable for production environments. These features may include a faster CPU, increased high-performance RAM, and increased storage capacity in the form of a larger or multiple hard drives. Servers also typically have reliability, availability and serviceability (RAS) and fault tolerance features, such as redundancy in power supplies, storage (as in RAID), and network connections. Servers became common in the early 1990s as businesses increasingly began using personal computers to provide services formerly hosted on larger mainframes or minicomputers. Early file servers housed multiple CD-ROM drives, which were used to host large database applications.[citation needed] Between the 1990s and 2000s an increase in the use of dedicated hardware saw the advent of self-contained server appliances. One well-known product is the Google Search Appliance, a unit that combines hardware and software in an out-of-the-box packaging. Simpler examples of such appliances include switches, routers, gateways, and print server, all of which are available in a near plug-and-play configuration. Modern operating systems such as Microsoft Windows or Linux distributions rightfully seem to be designed with a client-server architecture in mind. These operating systems attempt to abstract hardware, allowing a wide variety of software to work with components of the computer. In a sense, the operating system can be seen as serving hardware to the software, which in all but low-level programming languages must interact using an API. These operating systems may be able to run programs in the background called either services or daemons. Such programs, such as the aforementioned Apache HTTP Server software, may wait in a sleep state for their necessity to become apparent. Since any software that provides services can be called a server, modern personal computers can be seen as a forest of servers and clients operating in parallel. The Internet itself is also a forest of servers and clients. Merely requesting a web page from a few kilometers away involves satisfying a stack of protocols that involve many examples of hardware and software servers. The least of these are the routers, modems, domain name servers, and various other servers necessary to provide us the world wide web.
    • Types of servers

      In a general network environment the following types of servers may be found. Application server, a server dedicated to running certain software applications Catalog server, a central search point for information across a distributed network Communications server, carrier-grade computing platform for communications networks Database server, provides database services to other computer programs or computers Fax server, provides fax services for clients File server, provides remote access to files Game server, a server that video game clients connect to in order to play online together Home server, a server for the home Name server or DNS Print server, provides printer services Proxy server, acts as an intermediary for requests from clients seeking resources from other servers Sound server, provides multimedia broadcasting, streaming. Standalone server, an emulator for client-server (web-based) programs Web server, a server that HTTP clients connect to in order to send commands and receive responses along with data contents
  • Apache Tomcat

    Apache Tomcat (or simply Tomcat, formerly also Jakarta Tomcat) is an open source web server and servlet container developed by the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Oracle Corporation, and provides a "pure Java" HTTP web server environment for Java code to run. Tomcat is not the same as the Apache web server, which is a C implementation of a HTTP web server; these two web servers are not bundled together, although they are frequently used together as part of a server application stack. Apache Tomcat includes tools for configuration and management, but can also be configured by editing XML configuration files. Apache Tomcat is developed in an open and participatory environment and released under the Apache License version 2. Apache Tomcat is intended to be a collaboration of the best-of-breed developers from around the world. We invite you to participate in this open development project. To learn more about getting involved, click here. Apache Tomcat powers numerous large-scale, mission-critical web applications across a diverse range of industries and organizations. Some of these users and their stories are listed on the PoweredBy wiki page. Apache Tomcat, Tomcat, Apache, the Apache feather, and the Apache Tomcat project logo are trademarks of the Apache Software Foundations
  • Components

    Tomcat 4.x was released with Catalina (servlet container), Coyote (an HTTP connector) and Jasper (a JSP engine). [edit]Catalina Catalina is Tomcat's servlet container. Catalina implements Sun Microsystems' specifications for servlet and JavaServer Pages (JSP). In Tomcat, a Realm element represents a "database" of usernames, passwords, and roles (similar to Unix groups) assigned to those users. Different implementations of Realm allow Catalina to be integrated into environments where such authentication information is already being created and maintained, and then use that information to implement Container Managed Security as described in the Servlet Specification.[1] [edit]Coyote Coyote is Tomcat's HTTP Connector component that supports the HTTP 1.1 protocol for the web server or application container. Coyote listens for incoming connections on a specific TCP port on the server and forwards the request to the Tomcat Engine to process the request and send back a response to the requesting client. [edit]Jasper Jasper is Tomcat's JSP Engine. Tomcat 5.x uses Jasper 2, which is an implementation of the Sun Microsystems's JavaServer Pages 2.0 specification. Jasper parses JSP files to compile them into Java code as servlets (that can be handled by Catalina). At runtime, Jasper detects changes to JSP files and recompiles them. From Jasper to Jasper 2, important features were added: JSP Tag library pooling - Each tag markup in JSP file is handled by a tag handler class. Tag handler class objects can be pooled and reused in the whole JSP servlet. Background JSP compilation - While recompiling modified JSP Java code, the older version is still available for server requests. The older JSP servlet is deleted once the new JSP servlet has finished being recompiled. Recompile JSP when included page changes - Pages can be inserted and included into a JSP at runtime. The JSP will not only be recompiled with JSP file changes but also with included page changes. JDT Java compiler - Jasper 2 can use the Eclipse JDT (Java Development Tools) Java compiler instead of Ant and javac. [edit]Features Tomcat 7.x implements the Servlet 3.0 and JSP 2.2 specifications.[2] It requires Java version 1.6, although previous versions have run on Java 1.1 through 1.5. Versions 5 through 6 saw improvements in garbage collection, JSP parsing, performance and scalability. Native wrappers, known as "Tomcat Native", are available for Microsoft Windows and Unix for platform integration. [edit]History Tomcat started off as a servlet reference implementation by James Duncan Davidson, a software architect at Sun Microsystems. He later helped make the project open source and played a key role in its donation by Sun to the Apache Software Foundation. The Apache Ant software build automation tool was developed as a side-effect of the creation of Tomcat as an open source project. Davidson had initially hoped that the project would become open sourced and, since many open source projects had O'Reilly books associated with them featuring an animal on the cover, he wanted to name the project after an animal. He came up with Tomcat since he reasoned the animal represented something that could fend for itself. Although the tomcat was already in use for another O'Reilly title, his wish to see an animal cover eventually came true when O'Reilly published their Tomcat book with a snow leopard on the cover.[3] [edit]Releases
  • Apache Tomcat versions

    Apache Tomcat versions
    Version Release Date Description
    3.0.x. (initial release) 1999 Merger of donated Sun Java Web Server code and ASF and Implements Servlet 2.2 and JSP 1.1 specifications.
    3.3.2 2004-03-09 Latest 3.x release.
    4.1.31 2004-10-11
    4.1.36 2008-03-24
    4.1.39 2008-12-03
    4.1.40 2009-06-25 Latest 4.x release.
    5.0.0 2002-10-09
    5.0.23
    5.0.24 2004-05-09
    5.0.28 2004-08-28
    5.0.30 2004-08-30 Latest 5.0.x release
    5.5.0 2004-08-31
    5.5.1 2004-09-07
    5.5.4 2004-11-10
    5.5.7 2005-01-30
    5.5.9 2005-04-11
    5.5.12 2005-10-09
    5.5.15 2006-01-21
    5.5.16 2006-03-16
    5.5.17 2006-04-28
    5.5.20 2006-09-01
    5.5.23 2007-03
    5.5.25 2007-09
    5.5.26 2008-02
    5.5.27 2008-09-08
    5.5.28 2009-09-04
    5.5.30 2010-07-09
    5.5.31 2010-09-16
    5.5.32 2011-02-01
    5.5.33 2011-02-10
    5.5.34 2011-09-22
    5.5.35 2012-01-16
    5.5.36 2012-10-10 Latest 5.5.x release, 30-Sep-2012 is End-of-Life for 5.5.x
    6.0.0 2006-12-01
    6.0.10 2007-03-01
    6.0.13 2007-05-15
    6.0.14 2007-08-13
    6.0.16 2008-02-07
    6.0.18 2008-07-31
    6.0.20 2009-06-03
    6.0.24 2010-01-21
    6.0.26 2010-03-11
    6.0.28 2010-07-09
    6.0.29 2010-07-22
    6.0.30 2011-01-13
    6.0.32 2011-02-03
    6.0.33 2011-08-18
    6.0.35 2011-11-28
    6.0.36 2012-10-19 Latest 6.x release.
    7.0.0 beta 2010-06-29 First Apache Tomcat release to support the Servlet 3.0, JSP 2.2, and EL 2.2 specifications.
    7.0.4 beta 2010-10-21
    7.0.5 beta 2010-12-01
    7.0.6 2011-01-14
    7.0.8 2011-02-05
    7.0.10 2011-03-07
    7.0.11 2011-03-11
    7.0.12 2011-04-06
    7.0.14 2011-05-12
    7.0.16 2011-06-17
    7.0.19 2011-07-19
    7.0.20 2011-08-11
    7.0.21 2011-09-01
    7.0.22 2011-10-01
    7.0.23 2011-11-20
    7.0.25 2012-01-21
    7.0.26 2012-02-21
    7.0.27 2012-04-05
    7.0.28 2012-06-19
    7.0.29 2012-07-03
    7.0.30 2012-09-06 Fourth stable version.
    7.0.31 ** Not Released.
    7.0.32 2012-10-03 Current stable version.
  • How to configure and install the Tomcat

    Install Java. Make sure you have Java installed. Details here. Unzip Tomcat. Unzip tomcat-6.0.28-preconfigured.zip into the top level of the C drive. Details here. Set JAVA_HOME. Set this environment variable to point at the top-level of your Java installation directory (e.g., C:\Program Files\Java\jdk1.6.0_21). Use the Control Panel or see this sample autoexec.bat file. Details here. Set CLASSPATH. Set this environment variable to include ".", C:\Servlets+JSP, and the servlet/JSP JAR files. Use the Control Panel or see this sample autoexec.bat file. Details here. Test the server. Make sure you can start/stop the server. Test some sample HTML files, JSP pages, and servlets. Details here.
  • Sample program in Tomcat server

    First download and install tomcat and JDK on your machine.



    Create following folder structure inside tomcat installation directory.




    [tomcat install directory]/webapps/hello
    [tomcat install directory]/webapps/hello/WEB-INF
    [tomcat install directory]/webapps/hello/WEB-INF/classes
    [tomcat install directory]/webapps/hello/WEB-INF/classes/test



    Now using any text based editor write following java code using any editor and save the file as HelloServlet.java at [tomcat install directory]/webapps/hello/WEB-INF/classes/test package test; import java.io.*;
    import javax.servlet.http.*;
    import javax.servlet.*;
    public class HelloServlet extends HttpServlet {
    public void doGet (HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException, IOException
    {
    PrintWriter out = res.getWriter();
    out.println("Hello, world!");
    out.close();
    }
    }



    After this go to command prompt and change directory to [tomcat install directory]/webapps/hello/WEB-INF/classesand give following command javac -cp .\..\..\..\..\lib\servlet-api.jar test\HelloServlet.java We should start understanding the servlets from the beginning. Lets start by making one program which will just print the "Hello World" on the browser. Each time the user visits this page it will display "Hello World" to the user. As we know that the our servlet extends the HttpServlet and overrides the doGet() method which it inherits from the HttpServlet class. The server invokes doGet() method whenever web server recieves the GET request from the servlet. The doGet() method takes two arguments first is HttpServletRequest object and the second one is HttpServletResponse object and this method throws the ServletException. Whenever the user sends the request to the server then server generates two obects, first is HttpServletRequest object and the second one is HttpServletResponse object. HttpServletRequest object represents the client's request and the HttpServletResponse represents the servlet's response. Inside the doGet(() method our servlet has first used the setContentType() method of the response object which sets the content type of the response to text/html. It is the standard MIME content type for the Html pages. After that it has used the method getWriter() of the response object to retrieve a PrintWriter object. To display the output on the browser we use the println() method of the PrintWriter class. Now configuring web.xml for deploying and running the Servlet. Create web.xml with following content at [tomcat install directory]/webapps/hello/WEB-INF directory





    hello
    test.HelloServlet


    hello
    /hello



    Now just start tomcat server with default configuration and open following URL in your browser. http://localhost:8080/hello/hello You will see "Hello, world!" as an output in your browser. Note: Before starting tomcat make sure you have set JAVA_HOME environment variable point to installation directory of JDK.
  • More...

    Cilck here for sample program from tomcat website