javaweb-Tomcat&Servlet-032

Review of web related concepts

1. 软件架构
	1. C/S:客户端/服务器端
	2. B/S:浏览器/服务器端

2. 资源分类
	1. 静态资源:所有用户访问后,得到的结果都是一样的,称为静态资源.静态资源可以直接被浏览器解析
		* 如: html,css,JavaScript
	2. 动态资源:每个用户访问相同资源后,得到的结果可能不一样。称为动态资源。动态资源被访问后,需要先转换为静态资源,在返回给浏览器
		* 如:servlet/jsp,php,asp....
		

3. 网络通信三要素
	1. IP:电子设备(计算机)在网络中的唯一标识。
	2. 端口:应用程序在计算机中的唯一标识。 0~65536
	3. 传输协议:规定了数据传输的规则
		1. 基础协议:
			1. tcp:安全协议,三次握手。 速度稍慢
			2. udp:不安全协议。 速度快

Web server software:

* 服务器:安装了服务器软件的计算机
* 服务器软件:接收用户的请求,处理请求,做出响应
* web服务器软件:接收用户的请求,处理请求,做出响应。
	* 在web服务器软件中,可以部署web项目,让用户通过浏览器来访问这些项目
	* web容器


* 常见的java相关的web服务器软件:
	* webLogic:oracle公司,大型的JavaEE服务器,支持所有的JavaEE规范,收费的。
	* webSphere:IBM公司,大型的JavaEE服务器,支持所有的JavaEE规范,收费的。
	* JBOSS:JBOSS公司的,大型的JavaEE服务器,支持所有的JavaEE规范,收费的。
	* Tomcat:Apache基金组织,中小型的JavaEE服务器,仅仅支持少量的JavaEE规范servlet/jsp。开源的,免费的。


* JavaEE:Java语言在企业级开发中使用的技术规范的总和,一共规定了13项大的规范

* Tomcat:web服务器软件
	1. 下载:http://tomcat.apache.org/
	download
	2. 安装:解压压缩包即可。
		* 注意:安装目录建议不要有中文和空格
	3. 卸载:删除目录就行了

Insert picture description here
5. Start:
* bin/startup.bat, double-click to run the file
* Access: browser input: http://localhost:8080 Enter to visit yourself
http://others’ ip:8080 visit others

		* 可能遇到的问题:
			1. 黑窗口一闪而过:
				* 原因: 没有正确配置JAVA_HOME环境变量
				* 解决方案:正确配置JAVA_HOME环境变量,就是必须有个javahome,因为这个是写在tomcat配置文件里的
				* tomcat纯java编写,nb

			2. 启动报错:
				1. 暴力:找到占用的端口号,并且找到对应的进程,杀死该进程
					* netstat -ano找到8080,找到pid
					*任务管理器中关闭pid对应的进程,一般使用此方法
				2. 温柔:修改自身的端口号
					* conf/server.xml
					* <Connector port="8888" protocol="HTTP/1.1"
		               connectionTimeout="20000"
		               redirectPort="8445" />
					* 一般会将tomcat的默认端口号修改为80。80端口号是http协议的默认端口号。
						* 好处:在访问时,就不用输入端口号
						* 和系统抢端口号?
						* 访问一些网站的时候默认端口80可以不写
					如果修改之后没有效果,看看环境变量,可能有其他版本的tomcat被引用了,导致现在运行的不是现在用的				 tomcat.另外如果想启用多个tomcat也需要修改8009,8005端口,不然会被占用无法访问。
	6. 关闭:
		1. 正常关闭:
			* bin/shutdown.bat
			* ctrl+c
		2. 强制关闭:
			* 点击启动窗口的×
	7. 配置:
		* 部署项目的方式:
			1. 直接将项目放到webapps目录下即可。
				* /hello:项目的访问路径-->虚拟目录
				* localhost/hello/1.html:8080
				* 简化部署:将项目打成一个war包,再将war包放置到webapps目录下。
					* war包会自动解压缩,然后即可访问
				缺点:位置不灵活,名字固定了

			2. 配置conf/server.xml文件
				在<Host>标签体中配置
				<Context docBase="D:\hello" path="/hehe" />
				* docBase:项目存放的路径
				* path:虚拟目录
			缺点:名字固定了 不安全,会导致xml错误
			3. 在conf\Catalina\localhost创建任意名称的xml文件。在文件中编写
				<Context docBase="D:\hello" />
				* 虚拟目录:xml文件的名称
			解决
		* 静态项目和动态项目:
			* 目录结构
				* java动态项目的目录结构:
					-- 项目的根目录
						-- WEB-INF目录:
							-- web.xml:web项目的核心配置文件
							-- classes目录:放置字节码文件的目录
							-- lib目录:放置依赖的jar包


		* 将Tomcat集成到IDEA中,并且创建JavaEE的项目,部署项目。

Insert picture description here
Insert picture description here
Insert picture description here
Set the server to restart the update content.
Insert picture description here
If it is a/you can not write the name of the project, the virtual directory, but if it is not, you need to force it, otherwise the path is wrong
Insert picture description here

Servlet: server applet

* 概念:运行在服务器端的小程序
	* Servlet就是一个接口,定义了Java类被浏览器访问到(tomcat识别)的规则。
	* 将来我们自定义一个类,实现Servlet接口,复写方法。

Insert picture description here

  • Quick start:
    1. Create a JavaEE project
    2. Define a class to implement the Servlet interface
    * public class ServletDemo1 implements Servlet
    3. Implement the abstract method in the interface
    4. Configure the Servlet configuration
    in web.xml:

    Insert picture description here
package learn.myweb;

import javax.servlet.*;
import java.io.IOException;

public class servlet_test implements Servlet {
    
    
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
    
    

    }

    @Override
    public ServletConfig getServletConfig() {
    
    
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    
    
        System.out.println("你好,我醉了");
    }

    @Override
    public String getServletInfo() {
    
    
        return null;
    }

    @Override
    public void destroy() {
    
    

    }
}

  • Implementation principle:
    1. When the server receives the request from the client browser, it will parse the request URL path to obtain the resource path of the accessed Servlet.
    2. Find the web.xml file and see if there is a corresponding tag body content.
    3. If so, find the corresponding full class name
    4. Tomcat will load the bytecode file into memory and create its object
    5. Call its method
    Insert picture description here

  • Life cycle methods in Servlet:
    1. Created: Execute the init method, only executed once
    * When is the Servlet created?
    * By default, the servlet is created when it is accessed for the first
    time. * The creation timing of the execution servlet can be configured. web,xml
    * is configured under the label
    1. When accessed for the first time,
    the value created * is a negative number
    2. When the server starts,
    the value created * is 0 or a positive integer

      	* Servlet的init方法,只执行一次,说明一个Servlet在内存中只存在一个对象,Servlet是单例的
      	* 就是就一个对象,但是被众多用户访问
      		* 多个用户同时访问时,可能存在线程安全问题。
      		* 解决:尽量不要在Servlet中定义成员变量。即使定义了成员变量,也不要对修改值
      		* 加锁会影响性能,比如12306,几亿人排队搞笑
      		* 就是定义局部变量,不让数值共享
    
      2. 提供服务:执行service方法,执行多次
      	* 每次访问Servlet时,Service方法都会被调用一次。
      3. 被销毁:执行destroy方法,只执行一次
      	* Servlet被销毁时执行。服务器关闭时,Servlet被销毁
      	* 只有服务器正常关闭时,才会执行destroy方法。
      	* destroy方法在Servlet被销毁之前执行,一般用于释放资源
    
    • Servlet3.0:
      • benefit:

        • Support annotation configuration. No need for web.xml anymore.
      • step:

        1. Create a JavaEE project, choose Servlet version 3.0 or higher, you don’t need to create web.xml
        2. Define a class to implement the Servlet interface
        3. Copy method
        4. Use @WebServlet annotation on the class to configure
          web.xml
package learn.myweb;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
//使用注解可以更加简便。其中有默认参数,例如loadstarup
@WebServlet(urlPatterns = "/head_easy")
//可以更加简化,将url省略掉,使用value就可以,进一步value也不用写
public class servlet_test implements Servlet {
    
    
//   初始化方法, 在servlet创建的时候执行,只执行一次
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
    
    

    }
//servlet的配置对象
    @Override
    public ServletConfig getServletConfig() {
    
    
        return null;
    }
//提供服务的方法,每一次servlet被访问时都会执行
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    
    
        System.out.println("你好,我醉了");
    }
//获得servlet的信息,比如版本号等
    @Override
    public String getServletInfo() {
    
    
        return null;
    }
//销毁方法,servlet杀死时执行
    @Override
    public void destroy() {
    
    

    }
}

serverlet_test.java

package learn.myweb;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
//使用注解可以更加简便。其中有默认参数,例如loadstarup
@WebServlet(urlPatterns = "/head_easy")
//可以更加简化,将url省略掉,使用value就可以,进一步value也不用写
public class servlet_test implements Servlet {
    
    
//   初始化方法, 在servlet创建的时候执行,只执行一次
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
    
    

    }
//servlet的配置对象
    @Override
    public ServletConfig getServletConfig() {
    
    
        return null;
    }
//提供服务的方法,每一次servlet被访问时都会执行
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    
    
        System.out.println("你好,我醉了");
    }
//获得servlet的信息,比如版本号等
    @Override
    public String getServletInfo() {
    
    
        return null;
    }
//销毁方法,servlet杀死时执行
    @Override
    public void destroy() {
    
    

    }
}

IDEA and tomcat related configuration

1. IDEA会为每一个tomcat部署的项目单独建立一份配置文件
	* 查看控制台的log:Using CATALINA_BASE:   "C:\Users\Administrator\.IntelliJIdea2017.3\system\tomcat\_web_tom"
	* conf/
	* 虚拟目录部署,catalina,xml,内部项目存放路径,一个完整的工程

2. 工作空间项目和tomcat部署的web项目
	* tomcat真正访问的是“tomcat部署的web项目”,"tomcat部署的web项目"对应着"工作空间项目" 的web目录下的所有资源
	* WEB-INF目录下的资源不能被浏览器直接访问。
3. 断点调试:使用"小虫子"启动 dubug 启动

Guess you like

Origin blog.csdn.net/lidashent/article/details/107586152