Tomcat file configuration

xiaoxiao2021-04-02  205

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.

element properties

Property Description Defines the name of the sevlet (can be taken) Specify the initialization parameters of the service to implement this servlet (including the parameter name and parameter value), one There are multiple Specify the order of servlet when the web application is started. When this value is positive or zero, the servlet container first loads a small servlet if this value is negative or not set, then the servlet container will load when the web client first accesses servlet.

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 attribute set for servlet in Web.xml.

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

HTTP Response Example </ Title></p> <p></ hEAD></p> <p><body></p> <p>Hello http!</p> <p></ body></p> <p></ html></p> <p>HTTPSERVLET function</p> <p>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.</p> <p>Common way of httpservletRequest</p> <p>Method Name Description getCookies () Returns the HTTP request's cookies getHeader (String Name) Returns the HEADER Data specified by the HTTP request. Request method</p> <p>HTTPSERVLETRESPONSE common method</p> <p>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</p> <p>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</p> <p>(1) Extend the HTTPServlet abstraction class.</p> <p>(2) Cover some methods of httpservlets, such as overwriting the Doget () or dopost () method.</p> <p>(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:</p> <p>l getParameterNames (): Returns an enumeration object that contains all parameter name information.</p> <p>l getParameter (String Name): Returns the parameter value of the parameter name Name object.</p> <p>l getParameterValue (): Returns an Enumeration object that contains all parameter values.</p> <p>(4) Generate an HTTP response result.</p> <p>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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-131438.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="131438" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.035</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'jEwzsoMoEmXFPilitAE3VOLYpeFn1BWRUFycCdDwssWZtex4wDyIDdE33eyFLX051CfIiVQ1GWYLes6L'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>