Java Novice Learning Guide [day31]---Tomcat installation and use

One, the understanding of the server

1. The classification of the server:

  • Hardware server: a computer with a software server installed and a higher configuration

  • Software server: just a piece of software

    • Database server: Mysql, Oracle, etc.;
    • Web server: open source server: Apache, Tomcat, etc.
    • Application server: Commercial server: WebSphere (IBM), WebLogic (Oracle), JBoss (red hat)
    • Others: mail server, FTP server, proxy server, etc.

2. Common javaweb server

WebSphere (IBM), WebLogic (Oracle), JBoss (red hat) above three all support the JavaEE specification

Tomcat is open source and free, and only implements the Servlet specification

Second, the understanding of Tomcat

Tomcat is open source and free. The mainstream version currently in use is version 8.0. The default port number is 8080. Port 80 is the default http port. You can access it directly without writing.

1. The structure of the Tomcat directory

bin: some tools (open, closed)
conf: configuration file server.xml [Modify Port]
80 is the default port
index.html is the default home page lib: jar package repository log: Log webapps: Project Can be put inside​ temp​ work
<Context docBase="软件路径" path="名称" />




Insert picture description here

2. Common Tomcat errors

1. You need to turn on the Tomcat service before use, otherwise you will not be able to connect

2. It shows Address already in use: JVM_Bindthat the port is occupied, which may be the repeated start of the Tomcat service, or the port itself may be occupied

3. A 404 error indicates that the access resource is not in Tomcat. Check where the project is placed

4. The remaining errors can be viewed in logs

Three, JaveWeb project

1. JavaWeb project structure

  • Follow the directory (take whatever you want, it is recommended to take webapp, webapps, webContext...)

    • Resource files (heml, css, pictures) are optional, you can decide by yourself

    • WEB-INF (It can only be written like this, must be strictly followed, and the content inside cannot be directly accessed)

      • classes (store bytecode files, that is, Java code)
      • web.xml (some configurations, configuration files) optional
      • lib (jar package to be used)

Insert picture description here

2. Web project deployment

1. Put the project folder directly into Tomcat's webapps

2. It needs to be configured in the server.xml file (add the following code under the Value in the Host)

Context: represents the context configuration

docBase: represents the physical path of the project

path: context path, you can leave it alone

Four, Servlet foundation

Servlet is an interface, a specification made by Sun Company, but Sun Company did not implement it, but was implemented by major server vendors (Tomcat).

Tomcat is a java web server and a servlet container, which stores servlets

1. Realize the Servlet method

① Create a web project according to the rules;

② Create an implementation class to implement the Servlet interface/ inherit the HttpServlet class (recommended) , and implement or rewrite the service method;


public class HelloServlet extends HttpServlet{
    
    
	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
    
    
		//super.service(arg0, arg1);
		System.out.println("hello servlet....");
	}
}

③. Configure in web.xml and tell Tomcat that there is now a Servlet to be managed;

The first way: configure in the xml file

<!-- 声明一个servlet -->
<servlet>
   		<!-- 这个是为Servlet取的名称 -->
  		<servlet-name>heihei</servlet-name>
    	<!-- 这个是一个Servlet类的全限定名 -->
  		<servlet-class>cn.xxxx.LifeServlet</servlet-class>
 </servlet>
<!-- servlet的一个映射,向外告知地址 -->
<servlet-mapping>
    	<!-- 对应的映射的Servlet是哪一个 -->
  		<servlet-name>heihei</servlet-name>
    	<!-- 访问的路径 -->
  		<url-pattern>/heihei</url-pattern>
</servlet-mapping>

The second way: configuration by annotation

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    
    
    
}

④, configure the context path in the Tomcat service file server.xml:

Note: Don’t write the wrong path to the docBase, you don’t need to write to the specific file that needs to be executed, just go to the upper level folder

⑤ Access path:

http://ip address<:port></context path>/resource path
ip address: localhost(local)/127.0.0.1(local)/172...
port: if it is 80, you can not write the
resource path: if you don’t write the default Will find index.html/index.jsp

Implementation process:

Insert picture description here

Note: It must be implemented in accordance with the standards of web projects, and don't forget the classes file!

2. Servlet life cycle

  • Born (construction method) -> initialization (init) -> service (service->doGet, doPost,...) -> destroy (destroy)

Insert picture description here

  • Servlet creation time
    —> By default, the Servlet will be created when it is accessed for the first time
    —> It can be set to be created when tomcat starts

    <load-on-startup>1</load-on-startup>
    
  • Invoke the service method: the nth visit (except the first time)

  • Destroy: The destroy method (destory()) will be called when it is normally closed

  • Servlet is singleton (only created once) ----> Remember: if not necessary, do not create member variables in it

Insert picture description here

3. The key object HttpServletRequest/HttpServletResponse

  • HttpServletRequest: request object
    req.setCharacterEncoding("UTF-8"); -> Set the request encoding to solve the garbled problem of POST request
    req.getParameter("username") -> Get the parameters passed by the front end
  • HttpServletResponse: response object
    resp.setContentType("text/html;charset=UTF-8"); -> set the response type and encoding

Guess you like

Origin blog.csdn.net/WLK0423/article/details/110306316
Recommended