05-Servlet体系结构与urlpartten配置

29次阅读

共计 2677 个字符,预计需要花费 7 分钟才能阅读完成。

Servlet:

1. 概念
2. 步骤
3. 执行原理
4. 生命周期
5. Servlet3.0 注解配置
6. Servlet 的体系结构    
    Servlet -- 接口
        |
    GenericServlet -- 抽象类
        |
    HttpServlet  -- 抽象类

    * GenericServlet:将 Servlet 接口中其他的方法做了默认空实现,只将 service() 方法作为抽象
        * 将来定义 Servlet 类时,可以继承 GenericServlet,实现 service() 方法即可

    * HttpServlet:对 http 协议的一种封装,简化操作
        1. 定义类继承 HttpServlet
        2. 复写 doGet/doPost 方法 

package cn.itcast.web.servlet;

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

@WebServlet(“/demo1”)
public class ServletDemo1 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("demo1....");
}

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

@Override
public void destroy() {}

}
package cn.itcast.web.servlet;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet(“/demo2”)
public class ServletDemo2 extends GenericServlet {

@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {System.out.println("demo2.....");
}

}
package cn.itcast.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(“/demo3”)
public class ServletDemo3 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("doGet....");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("doPost...");
}

}
可以在 web 下创建 login.html

7. Servlet 相关配置
    1. urlpartten:Servlet 访问路径
        1. 一个 Servlet 可以定义多个访问路径:@WebServlet({"/d4","/dd4","/ddd4"})
        2. 路径定义规则:1. /xxx:路径匹配
            2. /xxx/xxx: 多层路径,目录结构
            3. *.do:扩展名匹配 

package cn.itcast.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**

  • Servlet 路径配置

*/
//@WebServlet({“/d4″,”/dd4″,”/ddd4”})
//@WebServlet(“/user/demo4”)
//@WebServlet(“/user/*”)
//@WebServlet(“/*”)
@WebServlet(“*.do”)
public class ServletDemo4 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("demo4...");
    System.out.println(req);
}

}

正文完
 0