Tomcat work mode
Independent Servelet container:
Servlet container within the process: JNI communication mechanism
SERVLET container outside the process: IPC communication mechanism
Tomcat environment variable
Windows environment:
Java_Home: Java installation root directory (such as: C: /J2SDK1.4.2).
Catalina_Home: Tomcat Installed root directory (such as: C: / Tomcat 5.0).
Linux environment: (Suppose Java is installed in /Home/java/j2sdk1.4.2, Tomcat installed under / home / tomcat)
Type JAVA_HOME environment variable SHELL command bash JAVA_HOME = / home / java / j2sdk1.4.2; export JAVA_HOME tsh Setenv JAVA_HOME /home/java/j2sdk1.4.2 SHELL command type setting environment variables CATALINA_HOME bash CATALINA_HOME = / home / tomcat; export Catalina_Home Tsh set setenv catalina_home / home / tomcat
Tomcat runs the script (how to use Catalina.bat)
Command line parameter Description Start Start Tomcat Server Run in the New DOS Window Run Start Tomcat Server DEBUG In Track Mode Start Tomcat Server Stop Close Tomcat Server
Create and publish web applications
Tomcat directory structure
Directory Description / BIN Starts the Windows platform and the Linux platform launched and closes Tomcat script file / confs the various profiles of the Tomcat server, where the most important configuration file is Server.xml / Server contains 3 subdirectories: Classes, LIB and WebAPPS / Server / lib stores the various JAR files required for Tomcat servers (other applications that are not accessible) / Server / WebAPPS stores two web applications comes with Tomcat: admin Apps and Manager Applications / Common / LIB Storage Tomcat Server and All Web applications can be accessed by JAR file / shared / lib store all web applications, JAR files (Tomcat are not accessible) / logs storage Tomcat log file / WebApps When publishing a web application, the web application file is put on by default This directory / Work Tomcat puts the servlet generated by JSP in this directory.
A lib sub-directory can be created in the web-inflint in the Java web application, and all JAR files are stored, which can only be accessed by the current web application.
During the operation, the Tomcat class loader is loaded in the class of the classs directory, and then load the class in the lib directory. If there is a class of the same name in both directories, the classes in the classes directory have priority.
Directory structure for web applications
Directory Description / HelloApp root directory, all JSP and HTML files are stored in this directory / HelloApp / Web-INF storage WEB application release description file Web.xml / HelloApp / Web-INF / CLASSES stores various Class files, servlet Class files are also placed in this directory / HelloApp / Web-INF / LIB stores the various JAR files required for web applications. For example: JDBC driver JAR
A little experience regarding context
Context can be added directly in the host of server.xml, but according to my observation, if we generate context through Tomcat's visualization management interface (Tomcat Administration), tomcat5.0 will automatically be automatically in ../tomcat 5.0 / Added an XML file named in the CONTEXT name (such as your context called "/ hello", which will generate a "hello.xml" file). And, if you are using Tomcat Administration, it will rewrite its server.xml and other existing contexts (performance as: all the comments in Server.xml are all). For server.xml overwriting, it also generates a backup file. And I found that as long as Context has changed, I will rewrite the server.xml. Context element properties
Property Description PATH Specifies the URL entry (can be named) DOCBASE to specify the file path of the web application, which can specify a relative path relative to the APPBase property relative to the HOST. If the web uses an open directory structure, specify the root directory of the web application; if the web application is a WAR file, specify the path to the WAR file. Reloadable If this property is set to true, the Tomcat server monitors changes in the Class file in the web-inf / class and web-inf / lib directory in the run state. If you monitor the Class file being updated, the server will automatically reload the web application.
During the development phase, set the RELOADABLE attribute to TRUE to help debug servlets and other Class files. However, since this feature increases the server's running load, it is recommended to change it to false at the product issuance of the web application.
Property Description
Configure virtual hosts
Servlet technology
Servlet is a server-side component-independent server-side component that can run in the servlet container.
The servlet framework is composed of two Java packages: javax.servlet and javax.servlet.http.
The Javax.Servlet.Servlet interface of the servlet framework, all servlets must be implemented.
There are 3 methods represent the life cycle of the servlet:
l Init method, responsible for initializing the servlet object.
l Service method, responsible for responding to the customer's request.
l Destroy method, when the servlet object exits the life cycle, it is responsible for the release of the resource. The Servlet class must extend one of the following two classes.
l extended GenericServlet class
The Service method must be implemented because the service method in the GenericServlet class is declared as an abstract method.
Public Abstract Void Service (ServletRequest Request, ServletResponse Response) THOWS ServletException, IOException
l extended HTTPSERVLET classes
The HTTP requests include DELETE, GET, OPTION, POST, PUT, TRACT, which provides the corresponding doxxx () method in the HTTPSERVLET class, and we only need to rewrite the corresponding method.
Here, describe the servletRequest and ServletResponse interfaces.
l ServletRequest interface
It encapsulates the customer request information, such as customer request, parameter name, parameter values, the client is using the protocol, and the remote host information of the client request. The servletinputstream is also provided to servlet directly to read the customer request data stream in binary.
Part of the SERVLETREQUEST interface
Method Name Description GetAttribute returns the attribute value based on the attribute name given by the parameter. GetContentType Returns the MIME Type GetInputStream requesting data. GetInputStream returns the input stream of the binary number to directly read the input of the customer request data. Room Host Port SetAttribute Sets properties in ServletRequest (including property names and properties)
l servletResponse interface
Provides a method of returning a result for servlet. It allows servlet settings to return the length of the data and the MIME type, and provide output flow servletoutputstream.
Part of the servletResponse interface
Method Name Description getOutputStream return may transmit the binary data to the client output stream object ServletOutputStream getWriter return may transmit character data to the client PrintWriter object getCharacterEncoding returned response data Servlet transmitted character encoding getContentType returns the response data Servlet transmitted MIME type setCharacterEncoding Set the character encoding of the response data sent by the servlet to set the MIME type of the response data sent by the servlet.
SERVLET lifecycle
Initialization phase
SERVLET container loading servlet in the following cases
l The servlet container is automatically loaded with some servlets when starting.
l After the servlet container is started, the customer first sends a request to servlet.
l Servlet's class file is updated, reload the servlet
Do servlets automatically load a servlet when starting, is determined by the
There are two forms:
Public Void Init (ServletConfig Config) Throws ServleTexception
Public Void Init () Throws ServletException Initialization Phase, the Servlet container creates a servletconfig object for servlet to store the initial configuration information of the servlet, such as the initial parameters of the servlet. If the servlet class overwrite the first type of INIT method, you should call the super.init (config) method to ensure the parameter config reference servletconfig object; if the second type of INIT method is overwritten, you can not call the super. Init () method, if you want to access the ServletConfig object in the init method, you can call the GetServletConfig () method of the servlet class.
HTTP request
Composed of 3 parts, namely:
l Request method URI protocol / version
l Request Header
l request text
Example:
Get /sample.jsp HTTP / 1.1
Accept: Image / GIF, Image / JPEG, * / *
Accept-language: zh-cn
Connection: Keep-alive
Host: Locakhost
User-agent: mozilla / 4.0 (compatible; msie5.01; windows NT 5.0)
Accept-encoding: Gzip, deflate
UserName = Werqin & Password = 1234
HTTP response
Composed of 3 parts, namely:
l Protocol status code description
l Respondent header (Response Header)
l responding body
Example:
HTTP / 1.1 200 ok
Server: Apachetomcat / 5.0.12
Date: Mon, 6 Oct 2003 13:23:42 GMT
Content-Length: 112
hEAD>
Hello http!
body>
html>
HTTPSERVLET function
It must first read the contents of the HTTP request. The Servlet container is responsible for creating an HttpRequest object and encapsulates the HTTP request information into the HttpRequest object.
Common way of httpservletRequest
Method Name Description getCookies () Returns the HTTP request's cookies getHeader (String Name) Returns the HEADER Data specified by the HTTP request. Request method
HTTPSERVLETRESPONSE common method
Method Name Description AddCookie (cookie cookie) Add cookie set to the HTTP response to set the HTTP response, if the parameter name corresponds to the header already exists, override the original Header data AddHeader (String Name, String Value) ) Add Header to HTTP response
In addition to the methods listed in these two tables, a general method of reading a client request is provided in the servletRequest of HTTPSERVLETREQUEST, providing a generic method for generating a server response in the parent class ServletResponse of HTTPSERVLETRESPONSE. Steps to create httpservlets
(1) Extend the HTTPServlet abstraction class.
(2) Cover some methods of httpservlets, such as overwriting the Doget () or dopost () method.
(3) Get HTTP request information, such as by httpservletRequest object to retrieve the data submitted by the HTML form or the query string on the URL. Whether it is an HTML form data or a query string on the URL, in the HTTPSERVLETREQUEST object, the parameter name / parameter value is stored in the form of parameter name / parameter value, and parameter information can be retrieved by the following method:
l getParameterNames (): Returns an enumeration object that contains all parameter name information.
l getParameter (String Name): Returns the parameter value of the parameter name Name object.
l getParameterValue (): Returns an Enumeration object that contains all parameter values.
(4) Generate an HTTP response result.
A PrintWriter object can be obtained by using the GetWriter () method of the HTTPSERVLETRESPONSE object. Use PrintWriter's print () or println () method to send a string data stream to the client.