search
HomeJavajavaTutorialA brief introduction to tomcat in Java

A brief introduction to tomcat in Java

Jul 21, 2017 pm 04:27 PM
javatomcatintroduce

This article mainly introduces the introduction of tomcat, and introduces tomcat and the basic configuration of Tomcat in detail. It has certain reference value. Those who are interested can learn about it

tomcat introduction:

The web server can only complete requests for static resources;

The web container can request dynamic resources;

tomcat is the simplest web container and is a sub-project of Apache's jarkata project.

tomcat 7.0.X supports the Servlet 3.0 specification, so it is recommended to use 7.0.X;

tomcat has two versions:

(1) Binary version: Environment variables need to be configured after decompression. It is recommended to use this version.
(2) Service version: executable file, the service will be registered in the system after installation.

tomcat directory introduction:

tomcat has Several folders:

(1)bin: stores executable commands, such as opening and closing tomcat: startup.bat;
(2)conf: configuration file.
(3)webapps: Publish web applications.
(4)lib: library file, which can be expanded through this directory, such as placing the database driver in this file.
(5)work: Temporary Java or class file.

Tomcat basic configuration

Note: Do not place Tomcat in a Chinese directory or a directory with spaces;

1. Configure environment variables

1. Configure JAVA_HOME: C:\Java\jdk1.6.0_27

2. Configure CATALINA_HOME: C:\ apache-tomcat-7.0.6 (This environment variable does not need to be configured)

Take my configuration as an example and adjust it according to your own installation location;

catalina_home is configured In the future, when the situation as shown in the figure appears:

When you click C:\tomcat\bin\startup.bat, D:\tomcat

# will be started.

##2. Configure the port

The default is 8080, which can be configured in tomcat/conf/server.xml.



<Connector port="8888" protocol="HTTP/1.1"   //此处可以把8888换成任意端口. 
      connectionTimeout="20000"  
      redirectPort="8443" />

After configuring these, you can already use tomcat. Enter http://localhost:8888 in the browser and the following page will pop up:

Port Occupied problem:

If other software occupies this port, tomcat will not be able to start;


Solution:

Use FPort and other software to check the port usage, and then kill the process;


3. Configure user roles

In order to manage and deploy web applications in the page, a user must be present, so a new user and password need to be configured (originally there is no user).


Add the following statement in in tomcat/conf/tomcat-users.xml:



<role rolename="manager-gui"/> 
<user username="admin" password="12345" roles="manager-gui"/> //用户名为admin,密码为12345

JAAS: Java Authentication Authorization Service, that is, Java Authentication Authorization Service


As shown in the figure, you can log in using the user name and password set previously:

4. Configure and deploy web application path

1. You can deploy it in the webapps directory, that is, copy the entire web application directly to webapps.


2. You can create a test.xml in the conf/catalina/localhost directory and add:



<?xml version="1.0" encoding="GBK"?> 
<Context docBase="F:/publish" debug="0" privileged="true">  //docBase是路径,可以自己调整 
</Context>

In this way, you can enter http://localhost:8080/test to access. Note that test is the name of the xml file.


For example, if the name of the xml file is x.xml, then enter http://localhost:8888/x

and you need to create a new directory in F:\publish, which is the directory of the web application. A WEB-INF folder. Create a new web.xml in this folder. To be lazy, you can paste the tomcat/webapps/WEB-INF/web.xml file into F:\publish\WEB-INF.

3. It can be deployed in a web page, as shown in the figure:

It can be deployed through a war package or a path. The war package can be exported from Eclipse.

Common points to note:


1. Set the port number: modify it in conf/server.xml.


2. Configure the virtual directory: Add at the end in conf/server.xml,


For example, , then enter http://localhost:8080/test in the browser to run the content of D:\test .


Note: After configuration, you must also configure the default homepage in conf/web.xml


##

<param-name>listings</param-name>
<param-value>false</param-value>改成true

3. Configure the default homepage: in The last


in conf/web.xml

<welcome-file>index.html</welcome-file>

说明默认为index.html,因此如果输入http://localhost:8080/test则会自动调用index.html.

4.每个web应用中都需要WEB-INF和web.xml文件,最简单的办法就是去tomcat主目录下的webapps/ROOT/的WEB-INF拷贝到你所需要的web应用文件夹下。

5.第一次用户请求某个JSP会比较慢。

原因:JSP首先会转换成JAVA文件,然后再编译成class文件,最后才执行,这些java和class的文件都在tomcat主目录的work文件夹下。

HTTP常见状态:

(1)404错误:客户路径不正确造成。
(2)500错误:服务器内部错误。

因此总的来说:

(1)2##:请求成功。
(2)4##:客户端错误。
(3)5##:服务器错误。

JSP文件简介:HTML+JAVA代码。

中嵌入java代码。

在html中

的action属性可以设置服务器处理请求的jsp文件,例如:


<form action = "hello.jsp" method = "post">

接下来是一个最简单的应用tomcat的程序,用户输入任意字符串比如world,服务器返回hello加上字符串,比如hello,world.


<html> 
  <head> 
    <title>Demo</title> 
  </head> 
  <body> 
    输入内容: 
    <form action = "input.jsp" method = "post"> 
      <input type = "text" name = "info"/> 
      <input type = "submit" value = "显示"/> 
    </form> 
  </body> 
</html>

jsp文件:


<html> 
  <head> 
    <title>Demo</title> 
  </head> 
  <body> 
    <% 
      String str = request.getParameter("info"); 
      str = "Hello,"+str; 
      out.println("<h1 id="str">"+str+"</h1>"); 
    %> 
  </body> 
</html>

web应用目录布局建议:


主目录 
  | 
  |-WEB-INF 
  |  |-  web.xml 
  |  |-  classes 
  |  |  |-放置class文件 
  |  |-   lib 
  |  |  |-放置第三方jar包 
  |  |-   src 
  |  |  |-存放源文件 
  |-存放jsp、html 
  | 
  |-image 
  |  |-存放图片

The above is the detailed content of A brief introduction to tomcat in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function