Home > Java > javaTutorial > A brief introduction to tomcat in Java

A brief introduction to tomcat in Java

黄舟
Release: 2017-07-21 16:27:55
Original
2248 people have browsed it

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" />
Copy after login

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
Copy after login

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>
Copy after login

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
Copy after login

3. Configure the default homepage: in The last


in conf/web.xml

<welcome-file>index.html</welcome-file>
Copy after login

说明默认为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">
Copy after login

接下来是一个最简单的应用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>
Copy after login

jsp文件:


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

web应用目录布局建议:


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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template