Home >Java >javaTutorial >A brief introduction to tomcat in Java
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
##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:
Solution:
3. Configure user roles
<role rolename="manager-gui"/> <user username="admin" password="12345" roles="manager-gui"/> //用户名为admin,密码为12345JAAS: Java Authentication Authorization Service, that is, Java Authentication Authorization Service
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.<?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.
Common points to note:
##
<param-name>listings</param-name> <param-value>false</param-value>改成true
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代码。
749c0cbec5ee6d6d4df0936c2a62afa9中嵌入java代码。
在html中ff9c23ada1bcecdd1a0fb5d5a0f18437的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>"+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!