With the appearance of the Catalina Servlet engine, Tomcat's fourth edition has been improved, making it a servlet / JSP container worth considering. However, you can help you start to learn about Tomcat's file resources, only few books, and
Some information on the Jakarta website is not very clear.
Let's learn about Tomcat's installation process step by step, and then discuss the use of some Tomcat 4 / JSP features to construct web applications.
What is Tomcat?
Tomcat is an open source, running a Java-based web application container for Servlet and JSP web applications. Tomcat is supported by Apache-Jakarta subprojects and maintained by volunteers from an open source Java community. Tomcat Server is performed according to the Servlet and JSP specification, so we can say that Tomcat Server has also implemented Apache-Jakarta specifications and better than most commercial application software servers.
Install Tomcat 4
To start using Tomcat 4, you should at least download the Java Development Kit (JDK) version 1.2 and Tomcat 4 installation packages, which are ZIP files and Windows installation packets, respectively. Once you have installed these two packets, you have to join two environment variables: java_home and tomcat_home. JDK will be installed in the java_home directory, while Tomcat 4 will be installed in the Tomcat_home directory.
If you are installed by a Windows installer, you can use the menu option to launch Tomcat, or open a command prompt window, enter the tomcat_home / bin directory, run the Startup.bat / SH file. Then open a browser and enter the URL http: // localhost: 8080. The default Tomcat page will appear in front of you.
Web application software primary reading
Before we start, let's review the parts included in a web application. The Java Servlet specification defines the web application as a collection of resources bundled and run on multiple containers from multiple marketers. In Tomcat, these resources are placed in a special directory structure below the Tomcat_Home / WebApps folder. This directory structure will be like this for a sample (myApp) for a web application.
Tomcat_Home / WebApps / MyApp / Web-INF / CLASSES / LIB
MyApp directory is considered to be the root directory of web applications, all JSP, HTML, JavaScript files and other resources are located under this directory. The web-INF directory contains the resources used by the application, but web-infers are not in the public document root directory. The files included in this directory cannot be accessed by the client. In the Category (under Web-INF), the servlets, beans, and effects required to run MYAPP. In some instances (log4j), the properties file is placed in the catalog of the class. Lib Directory (under Web-INF) contains Java Archive Files (Jars), such as a JDBC drive or tag library, which is myApp. If a class appears in the JAR file and in the class directory, the class loader loads the one in the class directory.
Servlets, JSPS and ServletContext
Java servlet is a stand-alone component that uses a request / response model managed by JSP / Servlet container to communicate with a web client. To write a servlet, genericservlet, and httpservlet classes will be extension, GenericServlet has an Abstract service method must be implemented. Httpservlet extends genericServlet and requires you to implement one of the following: doget, dopost, doput, dodeete, init, design, and getServletInfo. DOGET and DOPOST methods are implemented in typical situations. Valuable Reference We refer to the chapter of James Goodwill's "Apache Jakarta-Tomcat," as a source of information in these articles. This is the only special introduction to Tomcat's reference books. Other books from Wrox, Sams and Wiley will also come out quickly.
The servlet has the following survival cycle:
1. When the servlet is exemplified, the init mode is called.
2. Servlet can serve zero multiple requests.
3. Web Application is turned off, causing the Destroy mode to be called and allows objects to be reclaimed.
The init mode can be used to initialize the attribute of the servlet through the servletconfig parameter. The initialization parameters set in the web.xml file are passed to the init mode in ServletConfig and can be accessed through the GetInitParameter.
The Java Server page allows you to generate a dynamic HTML page by embedding Java logic into the required file. JSP is a mix of HTML, special label and embedded Java code, each JSP is translated into a servlet from this mix, this servlet is used to send HTML back to the client when you need JSP. The generated Java code and class files generated by servlet are stored in a specific location default as Tomcat_Home / Work, which is specified in the Context element. In many instances, if a JSP page does not work as expected or if a STACK TRACE with a line number is displayed, you can view the resulting code to help determine the problems.
Here is some JSP available implied variables:
Request - Reference to HTTPSERVLETREQUEST
Response - Reference to HTTPSERVLETRESPONSE
PageContext - Provides access to namespaces in JSP
Session - Reference to HTTPSession
Application - Reference to ServletContext
Config - Reference to ServletConfig
Page - Reference to the currently accessed JSP instance, similarly, used this variable for the servlet generated by this JSP
Exception - Reference to exceptions generated by JSP, only available on JSP only in iSerrorPage
Standard JSP tags are:
ServletContext can be considered as a integral storage area for web applications. Each web application has servletContext. Objects stored in servletContext will have been retained unless it is deleted. Web application software sample
Constructed a web application sample
Now let's construct a Web application sample containing a servlet, a bean, and several JSP pages. This web application is called myApp, and the file will exist in the tomcat_home / webapps / myapp directory.
./ directory contains all JSP files as shown in Table A. The index.jsp page uses the
The ./web-inf/classes/myapp directory contains the class files of the servlets and beans used by the application, as shown in Table B. The MyBean class has a property name StringValue. The bean_values.jsp page is used to store an instance of this bean within the scope of the application. The companyNameServlet is submitted to the destination of the form as a servlet_form.jsp page. This servlet generates HTML output based on the requested parameters. Note We ignore the init mode to set the CompanyName property given in the web.xml file. Let the doget () or dopost () execute in one way are a universal practice.
The ./web-inf directory contains a web.xml file, as shown in Table C, which defines the servlet and one content parameters. Note that init-param specifies the company's name. This parameter is extracted from servletconfig in the init method of CompanyNameservlet. The content parameters can be accessed in the variables.jsp page.
To run the MyApp application, place the file in the Tomcat_Home / WebApps / MyApp directory and start Tomcat. Once Tomcat is started, type URL http: // localhost: 8080 / myApp in the browser. This will call the index.jsp page with form.jsp content.
to sum up