With the advent of the Catalina Servlet Engine, tomcat version 4 claims that its performance has been improved, making it a Servlet/JSP Container worth considering. However, there are few file resources that can help you get started with tomcat, and there are only a few books, and some materials on the Jakarta website are not very clear.
Let's take a step-by-step look at the tomcat installation process, and then discuss how to use some Tomcat 4/jsp functions to build Web applications.
What is tomcat?
Tomcat is a Java-based Web Application Software container that runs open source code, Servlet, and JSP web application software. Tomcat is supported by APACHE-Jakarta sub-projects and maintained by volunteers from the open source code Java Community. Tomcat server is executed according to Servlet and JSP specifications. Therefore, we can say that Tomcat server implements APACHE-Jakarta specifications and is better than most commercial application software servers.
Install Tomcat 4
To start using Tomcat 4, you must download at least the Java Development Kit (JDK) 1.2 and Tomcat 4 installation packages, which are zip files and Windows Installation packages. Once you have installed these two data packets, you must add two environment variables: java_home and atat_home. JDK will be installed in the java_home directory, while Tomcat 4 will be installed in the tomcat_home directory.
If you install Tomcat through the Windows Installer, you can use the menu options to start Tomcat, or open a command prompt window to enter the tomcat_home/bin directory and run startup. BAT/SH file. Open a browser and enter the URLHttp: // localhost: 8080. The default Tomcat page will appear in front of you.
Preliminary reading of Web Application Software
Before we start, let's review the components contained in the next web application software. Java servlet specification defines Web applications as a set of servlets, HTML pages, classes, and other resources that are bundled and run on multiple containers from multiple marketers. In tomcat, these resources are placed in a special directory structure under the tomcat_home/webapps folder. For a sample of a Web application (MyApp), the directory structure is as follows:
Tomcat_home/webapps/MyApp/WEB-INF/classes/lib
The MyApp directory is regarded as the root directory of Web application software. 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 the WEB-INF is not in the public document root directory. All files in this directory cannot be accessed by the client. The class directory (under the WEB-INF) contains the Servlets, beans, and Utility Classes required to run MyApp. In some instances (log4j), attribute files are placed in the directory of the class. The lib directory (under the WEB-INF) contains Java archive files (jars), such as a JDBC drive or tag library, which MyApp relies on. 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 platform-independent component that uses the request/response model managed by JSP/servlet containers to communicate with web clients. To compile a servlet, both genericservlet and httpservlet classes will be extended. genericservlet has an abstract service method that must be implemented. Httpservlet extends genericservlet and requires you to implement one of the following methods: doget, dopost, doput, dodelete, init, destroy, and getservletinfo. In typical cases, the doget and dopost methods are implemented.
Valuable references
We have taken a look at the chapter in James Goodwill's "Apache Jakarta-Tomcat," as a source of information for these articles. Currently, this is the only reference book that specifically introduces tomcat. Other books from wrox, Sams, and Wiley will soon be available.
Servlet has the following lifecycle:
1. When the servlet is cited, the init method is called.
2. servlet can serve more than one request.
3. Web applications are disabled, causing the destroy method to be called and objects to be recycled.
The init method can be used to initialize servlet attributes through the servletconfig parameter. The initialization parameters set in the web. xml file are passed to the init method in servletconfig and can be accessed by getinitparameter.
The Java Server Page allows you to embed Java logic into required files to generate dynamic HTML pages. JSP is a mix of HTML, special tags, and embedded Java code. Every JSP is translated into a servlet from this mix. this servlet is used to send HTML back to the client when JSP is needed. The Java code and class files generated by Servlet are stored in a specific location of tomcat_home/work by default. This directory is specified in the context element. In many instances, if a JSP page does not operate as expected or if a stack trace with row numbers is displayed, you can view the generated code to help identify the problem.
Here are some implied variables available for JSP:
- Request-Reference to httpservletrequest
- Response-Reference to httpservletresponse
- Pagecontext-Provide access to the namespace in JSP
- Session-Reference to httpsession
- Application-Reference to servletcontext
- Config-Reference to servletconfig
- Page-Reference the currently accessed JSP instance. Similarly, this variable is used for the servlet generated by this JSP instance.
- Exception-For reference, only available on JSP when iserrorpage is set to true
The standard JSP tag is:
- <JSP: usebean>-Allows you to place or search for a bean within the scope of pages, requests, sessions, or applications (this tag is very useful for persistence ).
- <JSP: setproperty>-You can set attributes for the defined bean.
- <JSP: getproperty>-Obtain the attribute value of a bean, convert it to a string, and place it in the output stream.
- <JSP: param>-Provide parameters and values for standard activities such as forward and insert.
- <JSP: Include>-Add a page in the request time to replace the translation time indicated by @ include.
- <JSP: Forward>-Forward requests to another resource in existing web applications.
- <JSP: plugin>-Enable JSP creators to generate HTML using the Independent Architecture of the client browser, this can cause download and subsequent execution of the specified applet or component (the tag value is replaced by a <Object> or <embed> label ).
Servletcontext can be considered as a holistic storage area for Web applications. Each web application has a servletcontext. Objects stored in servletcontext will be retained until they are deleted.
Web Application Software Samples
Construct a web application software sample
Now let's construct a web application software sample that contains a servlet, a bean, and several JSP pages. This web application is named MyApp, and the file will exist in the tomcat_home/webapps/MyApp directory.
The./directory contains all JSP files, as shown in table. The index. jsp page uses the <JSP: Forward> label to effectively set form. jsp to the homepage of MyApp. The form. jsp page uses the <JSP: Include> label to add navigation and two separate forms to complete the form page. Note how to use the <JSP: Include> label <JSP: param> label to pass a parameter to the jsp_form.jsp page. The servlet_form.jsp page is an example of using Servlet for form processing when the jsp_form.jsp page is submitted to another JSP page. The company_name.jsp page uses parameters submitted from the jsp_form.jsp page to construct the company's color logo. The bean_values.jsp page uses the <JSP: usebean> label to store/restore/modify bean attributes within the scope of the application software. The variables. jsp page displays examples of some available implicit variables in JSP.
The./WEB-INF/classes/MyApp directory contains the servlet and Bean class files used by the application software, as shown in table B. The mybean class has a property named stringvalue. The bean_values.jsp page is used to store an instance of this bean within the scope of the application software. The companynameservlet is used as the destination of the submitted Form on the servlet_form.jsp page. This servlet generates HTML output based on the request parameters. Note that the init method is ignored to set the companyName attribute in the web. xml file. It is a common practice to call other executions by executing doget () or dopost () in a method.
The./WEB-INF directory contains a web. xml file, as shown in table C, which defines Servlet and a content parameter. Note that init-Param specifies the company name. This parameter is extracted from servletconfig In the init method of companynameservlet. Content parameters can be accessed on 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 the URL in the browserHttp: // localhost: 8080/MyApp. In this way, the index. jsp page with form. jsp content will be called up.
Summary
This brief introduction to Tomcat can help with the development of work, but for JSP and tomcat configuration, it is much more than the content we mentioned in this article. Tomcat has a server. xml file located in the tomcat_home/conf directory, which allows further customization, provides a Manager application software andValvesThe request-Preprocessing Program, which can also implement the filter. Tomcat integrates log4j and Apache Web servers to provide more robust solutions for your web applications. In general, Tomcat is a very good choice, especially it is free.