Tomcat 4JSP Getting Started Guide

xiaoxiao2021-03-06  41

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:

- Allows you to place or find a bean within the range of the page, request, session, or application software (this label is very useful for keeping the state).

- Allows you to set properties for already defined beans.

- Gets the value of the properties of a bean, converts it to a string and placed among the output streams.

- Provides parameters and values ​​to standard activities including advancement and insertion.

- Add a page instead of the @include indicated translation time in the request time.

- For the existing web application, advance the request to another resource. - Enables JSP creators to generate HTML using client browsers to generate HTML, which can result in the specified applet or component download and subsequent execution behavior (label value by or The label replaced).

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 tab to effectively set form.jsp to myApp's homepage. The form.jsp page uses the tab to add navigation and two separate forms to the form page to be improved. Note that the third tag uses the tab to pass a parameter to the JSP_FORM.JSP page. The servlet_form.jsp page is an example of making form processing using servlet when submitting a JSP_FORM.JSP page to another JSP page. The company_name.jsp page uses the parameters submitted from the JSP_FORM.JSP page to construct the company's color logo. The bean_values.jsp page uses the tab to store / recover / modify the properties of the bean within the scope of the application. The Variables.jsp page displays examples of some of the useful hidden variables in JSP.

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

转载请注明原文地址:https://www.9cbs.com/read-55697.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.042, SQL: 9