Tomcat file configuration

Source: Internet
Author: User

Tomcat Working Mode

Independent servelet containers:

Servlet container in process: JNI Communication Mechanism

Servlet container outside process: IPC Communication Mechanism

Tomcat Environment Variables

Windows:

Java_home: Java installation root directory (for example, C:/j2sdk1.4.2 ).

Catalina_home: the root directory for Tomcat installation (for example, C:/tomcat 5.0 ).

Linux environment: (assume that Java is installed in/home/Java/j2sdk1.4.2, and Tomcat is installed in/home/tomcat)

Shell Type

Set the java_home environment variable command

Bash

Java_home =/home/Java/j2sdk1.4.2; export java_home

TSH

Setenv java_home/home/Java/j2sdk1.4.2

Shell Type

Command for setting the catalina_home environment variable

Bash

Catalina_home =/home/tomcat; export catalina_home

TSH

Setenv catalina_home/home/tomcat

Tomcat running script (how to use Catalina. BAT)

Command Line Parameters

Description

Start

Start the Tomcat server in the new DOS window

Run

Start the Tomcat server in the current DOS window

Debug

Start the Tomcat server in tracking mode

Stop

Disable Tomcat server

Create and publish Web Applications

Tomcat directory structure

Directory

Description

/Bin

Stores script files for starting and disabling Tomcat on Windows and Linux platforms

/Conf

Stores various configuration files of the Tomcat server. The most important configuration file is server. xml.

/Server

Contains three subdirectories: classes, Lib, and webapps.

/Server/lib

Store various jar files required by the Tomcat server (other applications are not accessible)

/Server/webapps

Stores two built-in Tomcat web applications: the admin application and the Manager application.

/Common/lib

Stores jar files accessible to Tomcat servers and all web applications.

/Shared/lib

Stores jar files accessible to all web applications (Tomcat unavailable)

/Logs

Store Tomcat log files

/Webapps

When a web application is published, the Web application file is stored in this directory by default.

/Work

Tomcat stores the servlet generated by JSP in this directory

You can create lib sub-directories in the WEB-INF directory under the Java Web application to store various jar files, these jar files can only be accessed by the current web application.

During the running process, the Tomcat Class Loader first loads classes under the classes directory and then classes under the lib directory. If two directories have classes of the same name, the classes in the classes Directory have priority.

Directory structure of Web Applications

Directory

Description

/Helloapp

Root directory, where all JSP and HTML files are stored

/Helloapps/WEB-INF

Stores the web application release description file web. xml

/Helloapp/WEB-INF/classes

Stores various class files. servlet class files are also stored in this directory.

/Helloapp/WEB-INF/lib

Stores various jar files required by web applications. Example: JDBC driver jar

Context

Context can be directly added to the server. XML host, but according to my observation, if we use Tomcat's Visual Management Interface (Tomcat administration) to generate context, we will find that tomcat5.0 will automatically be in .. add an XML file named "/Hello" under/tomcat 5.0/CONF/Catalina/localhost (for example, if your context is "/Hello", it will generate a "hello. XML file ). In addition, if you use Tomcat administration for the first time, it will put its server. XML and all other existing contexts are rewritten once (as shown in the original server. all comments in XML are missing ). For server. xml rewriting, it will also generate a backup file. In addition, I found that as long as the context is changed, server. XML will be rewritten once.

Attributes of the context element

Attribute

Description

Path

Specifies the URL entry for accessing the web application (you can name it as needed)

Docbase

Specify the file path of the Web application. You can specify the absolute path or the relative path of the appbase attribute 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 of the war file.

Reloadable

If this property is set to true, the Tomcat server will monitor changes to the class files in the WEB-INF/classes and WEB-INF/lib directories while running. If a class file is updated, the server automatically reloads the web application.

In the development phase, setting the reloadable attribute to true helps debug Servlet and other class files. However, this function will increase the load on the server, so we recommend that you change it to false in the product release phase of the Web application.

<Servlet> attributes of an element

Attribute

Description

<Servlet-Name>

Define the sevlet name (optional)

<Servlet-class>

Specify the servlet class

<Init-param>

Defines servlet initialization parameters (including parameter names and parameter values). A <servlet> can have multiple <init-param>

<Load-on-startup>

Specify the order of servlet loading when the web application starts. When the value is positive or zero, the servlet container first loads the servlet with a small value. If the value is negative or not set, the servlet container loads the servlet when the Web client accesses the servlet for the first time.

Configure a VM

Servlet Technology

A servlet is a platform-independent server component that can run in a servlet container.

The servlet framework consists of two Java packages: javax. servlet and javax. servlet. HTTP.

The core of the servlet framework is the javax. servlet. servlet interface. All servlets must implement this interface.

Three methods represent the servlet lifecycle:

L init method, responsible for initializing servlet objects.

L service method, responsible for responding to customer requests.

L destroy method. When the servlet Object exits the lifecycle, it is responsible for releasing the occupied resources.

The servlet class must expand 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 class

HTTP request methods include Delete, get, option, post, put, and tract. The corresponding doxxx () methods are provided in the httpservlet class respectively. We only need to rewrite the corresponding methods.

The following describes the servletrequest and servletresponse interfaces.

L servletrequest Interface

It encapsulates the customer request information, such as the customer request method, parameter name and parameter value, the protocol in use by the client, and the remote host information that sends the customer request. It also provides servletinputstream for servlet to directly read the customer request data stream in binary mode.

Some methods of the servletrequest Interface

Method Name

Description

Getattribute

Returns the property value based on the property name specified by the parameter.

Getcontenttype

Mime Type of the customer request data returned

Getinputstream

Returns the input stream that directly reads the data requested by the customer in binary mode.

Getparameter

Returns the parameter value based on the given parameter name.

Getremoteaddr

Returns the IP address of the remote client host.

Getremotehost

Returns the remote client host name.

Getremoteport

Back to remote room host port

Setattribute

Set attributes in servletrequest (including attribute names and attribute values)

L servletresponse Interface

Provides the servlet with a method to return results. It allows the servlet to set the length and Mime Type of the returned data, and provides the output stream servletoutputstream.

Some methods of the servletresponse Interface

Method Name

Description

Getoutputstream

Returns the output stream object servletoutputstream that can send binary data to the client.

Getwriter

Returns the printwriter object that can send character data to the client.

Getcharacterencoding

Returns the character encoding of the response data sent by the servlet.

Getcontenttype

Returns the MIME type of the response data sent by the servlet.

Setcharacterencoding

Sets the character encoding of the response data sent by the servlet.

Setcontenttype

Set the MIME type of the response data sent by the Servlet

Servlet Lifecycle

1. Initialization phase

Servlet container loading servlet in the following situations

L some servlets are automatically loaded when the servlet container starts.

L after the servlet container is started, the customer sends a request to the servlet for the first time.

L after the servlet class file is updated, reload the Servlet

Whether the servlet is automatically loaded at startup is determined by the <load-on-startup> attribute set for the servlet in Web. xml.

The init method has two forms:

Public void Init (servletconfig config) throws servletexception

Public void Init () throws servletexception

In the initialization phase, the servlet container creates a servletconfig object for the servlet to store the Initialization Configuration Information of the servlet, such as the initial parameters of the servlet. If the servlet class overwrites the first init method with parameters, you should first call super. the init (config) method ensures that the parameter config references the servletconfig object. If the second init method without parameters is overwritten, super. the init () method. To access the servletconfig object in the init method, you can call the getservletconfig () method of the servlet class.

HTTP Request

It consists of three parts:

L request method URI protocol/version

L request header)

L request body

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

It consists of three parts:

L Protocol Status Code Description

L Response Header)

L response body

Example:

HTTP/1.1 200 OK

Server: apachetomcat/5.0.12

Date: Mon, 6 Oct 2003 13:23:42 GMT

Content-Length: 112

<HTML>

<Head>

<Title> HTTP response example </title>

</Head>

<Body>

Hello HTTP!

</Body>

</Html>

Functions of httpservlet

It must first read the content of the HTTP request. The servlet container is responsible for creating the httprequest object and encapsulating the HTTP request information into the httprequest object.

Common Methods of httpservletrequest

Method Name

Description

Getcookies ()

Return HTTP request cookies

Getheader (string name)

Return the header data of the HTTP request specified by the parameter.

Getrequesturi ()

Returns the URI of the HTTP request.

Getquerystring ()

Returns the query string in the HTTP request data.

Getmethod ()

Return HTTP Request Method

Common Methods of httpservletresponse

Method Name

Description

Addcookie)

Add cookie to HTTP Response

Setheader (string name, string value)

Sets the HTTP Response Header. If the header corresponding to the parameter name already exists, the original header data will be overwritten.

Addheader (string name, string value)

Add a header to the HTTP Response

In addition to the methods listed in the two tables, the servletrequest of httpservletrequest provides a general method for reading client requests, and the servletresponse of httpservletresponse provides a general method for generating server responses.

Create an httpservlet

(1) extends the HTTP servlet abstract class.

(2) Some methods that overwrite httpservlet, such as the doget () or dopost () methods.

(3) obtain the HTTP request information. For example, you can use the 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 a URL, it is stored in the httpservletrequest object as a parameter name/parameter value. You can retrieve the parameter information using the following methods:

L getparameternames (): returns an enumeration object that contains all parameter names.

L getparameter (string name): return the parameter value of the parameter name object.

L getparametervalue (): returns an enumeration object that contains information about all parameter values.

(4) generate HTTP response results.

You can obtain a printwriter object by using the getwriter () method of the httpservletresponse object. The print () or println () method of printwriter can be used to send string data streams to the client.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.