The mental journey of learning the front-end web-----web server software Tomcat and Servlet introduction


insert image description here

web server software

Common concepts:

  • Server: A computer with server software installed
  • Server software: receives user requests, processes requests, and responds
  • Web server software: Receive user requests, process requests, and respond.
    • In web server software, web projects can be deployed and users can access these projects through a browser
    • web container
  • Common java-related web server software:
    • webLogic: Oracle Corporation, a large-scale JavaEE server, supports all JavaEE specifications, for a fee.
    • webSphere: IBM, a large-scale JavaEE server, supports all JavaEE specifications, for a fee.
    • JBOSS: JBOSS company, a large-scale JavaEE server, supports all JavaEE specifications, charges.
    • Tomcat: Apache Foundation, small and medium-sized JavaEE server, only supports a small number of JavaEE specification servlets/jsp. Open source and free.

Tomcat

Baidu Encyclopedia

  Tomcat is a core project in the Jakarta project of the Apache Software Foundation, developed jointly by Apache, Sun and some other companies and individuals. Thanks to Sun's involvement and support, the latest Servlet and JSP specifications are always present in Tomcat, and Tomcat 5 supports the latest Servlet 2.4 and JSP 2.0 specifications. Because of its advanced technology, stable performance, and free, Tomcat is deeply loved by Java enthusiasts and recognized by some software developers, making it a popular Web application server.

step

  • 1. Download: http://tomcat.apache.org/

  • 2. Installation: Unzip the compressed package.
    Note: It is recommended not to have Chinese and spaces in the installation directory

  • 3. Uninstall: just delete the directory

  • 4. Start:
      * bin/startup.bat , double-click to run the file
      * Access: Browser input: http://localhost:8080 Enter to access yourself
               http://others ip:8080 Access others

      * 可能遇到的问题:
      	1. 黑窗口一闪而过:
      		* 原因: 没有正确配置JAVA_HOME环境变量
      		* 解决方案:正确配置JAVA_HOME环境变量
    
      	2. 启动报错:
      		1. 暴力:找到占用的端口号,并且找到对应的进程,杀死该进程
      			* netstat -ano
      		2. 温柔:修改自身的端口号
      			* conf/server.xml
      			* <Connector port="8888" protocol="HTTP/1.1"
                     connectionTimeout="20000"
                     redirectPort="8445" />
      			* 一般会将tomcat的默认端口号修改为80。80端口号是http协议的默认端口号。
      				* 好处:在访问时,就不用输入端口号
    
  • 5. Shutdown:
    1. Normal shutdown:
       bin/shutdown.bat
       ctrl+c
    2. Forced shutdown:
       Click the × in the startup window

  • 6. Configuration:
    The way to deploy the project:

        1. 直接将项目放到webapps目录下即可。
      		* /hello:项目的访问路径-->虚拟目录
      		* 简化部署:将项目打成一个war包,再将war包放置到webapps目录下。
      			* war包会自动解压缩
      				
      	2. 配置conf/server.xml文件
      		在<Host>标签体中配置
      		<Context docBase="D:\hello" path="/hehe" />
      		* docBase:项目存放的路径
      		* path:虚拟目录
      	3. 在conf\Catalina\localhost创建任意名称的xml文件。在文件中编写
      		<Context docBase="D:\hello" />
      		* 虚拟目录:xml文件的名称
      
      * 静态项目和动态项目:
      	* 目录结构
      		* java动态项目的目录结构:
      			-- 项目的根目录
      				-- WEB-INF目录:
      					-- web.xml:web项目的核心配置文件
      					-- classes目录:放置字节码文件的目录
      					-- lib目录:放置依赖的jar包
    

Servlet

  • Concept: applet running on the server side
    • Servlet is an interface that defines the rules for Java classes to be accessed by browsers (identified by tomcat).
    • In the future, we will customize a class, implement the Servlet interface, and override the method.

Quick start:

  1. Create a JavaEE project
  2. Define a class that implements the Servlet interface
       public class ServletDemo1 implements Servlet
  3. Implement abstract methods in interfaces
  4. Configure servlets
 在web.xml中配置:
	    <!--配置Servlet -->
	    <servlet>
	        <servlet-name>demo1</servlet-name>
	        <servlet-class>cn.itcast.web.servlet.ServletDemo1</servlet-class>
	    </servlet>
	
	    <servlet-mapping>
	        <servlet-name>demo1</servlet-name>
	        <url-pattern>/demo1</url-pattern>
	    </servlet-mapping>

Execution principle:

  1. When the server receives the request from the client browser, it parses the request URL path and obtains the resource path of the accessed servlet.
  2. Find the web.xml file to see if there is a corresponding tag body content.
  3. If there is, then find the corresponding full class name
  4. tomcat will load the bytecode file into memory and create its object
  5. call its method

Lifecycle methods in servlets

1. 被创建:执行init方法,只执行一次
	* Servlet什么时候被创建?
		* 默认情况下,第一次被访问时,Servlet被创建
		* 可以配置执行Servlet的创建时机。
			* 在<servlet>标签下配置
				1. 第一次被访问时,创建
            		* <load-on-startup>的值为负数
	            2. 在服务器启动时,创建
	                * <load-on-startup>的值为0或正整数
	* Servlet的init方法,只执行一次,说明一个Servlet在内存中只存在一个对象,Servlet是单例的
		* 多个用户同时访问时,可能存在线程安全问题。
		* 解决:尽量不要在Servlet中定义成员变量。即使定义了成员变量,也不要对修改值
2. 提供服务:执行service方法,执行多次
	* 每次访问Servlet时,Service方法都会被调用一次。
3. 被销毁:执行destroy方法,只执行一次
	* Servlet被销毁时执行。服务器关闭时,Servlet被销毁
	* 只有服务器正常关闭时,才会执行destroy方法。
	* destroy方法在Servlet被销毁之前执行,一般用于释放资源

insert image description here

Guess you like

Origin blog.csdn.net/S_yyuan/article/details/122410270