Servlet快速入门

Servlet:server applet

*概念:运行在服务器端的小程序

Servlet就是一个接口,定义了java类被浏览器访问到(Tomcat识别)的规则。

将来我们定义一个类,实心Servlet接口,复写方法。

*快速入门:

1.创建一个JAVAEE的项目

2.定义一个类,实现Servlet接口

3.实现接口中的抽象方法

package cn.itcast.web.servlet;

import java.io.IOException;


import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ServletDemo1 implements Servlet{
    
    //提供服务的方法
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("Hello Servlet");
            
        }
    public void destroy() {
        // TODO Auto-generated method stub
    }

    public ServletConfig getServletConfig() {
        // TODO Auto-generated method stub
        return null;
    }

    public String getServletInfo() {
        // TODO Auto-generated method stub
        return null;
    }

    public void init(ServletConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        
    }
     
    

}

4.配置Servlet

<?xml version="1.0" encoding="UTF-8"?>
<!-- <web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0 " metadata-complete="false">
    
    <!-- 配置sevlet -->
  <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>
  
</web-app>

*执行原理:

  1.当服务器接受客户端浏览器的请求时,会解析请求URL路径,获取访问的Servlet的资源路径

  2.查找web.xml文件,是否有对应的<url-pattern>标签体的内容

  3.如果有,则再找到对应的<servlet-class>全类名

  4.tomcat会将字节码文件加载进内存,并且创建其对象

  5.调用其方法

*servlet中的方法

    提供服务的方法。每一次Service被访问时执行。可执行多次。
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("Hello Servlet");
            
        }

销毁方法。在servlet正常关闭时执行。只执行一次。
public void destroy() { // TODO Auto-generated method stub } public ServletConfig getServletConfig() { // TODO Auto-generated method stub return null; } public String getServletInfo() { // TODO Auto-generated method stub return null; }
在servlet被创建时执行,只会执行一次
public void init(ServletConfig arg0) throws ServletException { // TODO Auto-generated method stub }

 *Servlet注解:在类名上加@WebServlet("路径名")就不用写.xml文件了。

*Servlet的体系结构

  Servlet--接口

    |

  GenericServlet--抽象类:将servlet接口中其他的方法做了默认空实现,只将service()方法作为抽象。可以只实现Service()方法即可。

    |

  HttpServlet--抽象类:对Http协议的一种封装,简化操作;步骤:1.定义类继承HttpServlet  2.复写doGet/doPost方法

*Servlet的相关配置

  1.urlpartten:servlet访问路径

原文地址:https://www.cnblogs.com/laoyangtou/p/12310315.html