栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Servlet-1

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Servlet-1

1、Servlet 1.1、什么是 Servlet

1、Servlet 是 JavaEE规范之一,规范就是接口
2、Servlet 是 JavaWeb 三大组件之一,三大组件分别是: Servlet 程序、Filter 过滤器、Listener 监听器
3、Servlet 是运行在服务器上的一个 java小程序,它可以接收客服端发送过来得请求,并响应数据给服务端

1.2、手动实现 Servlet 程序

1、编写一个类去实现 Servlet 接口
2、实现 service 方法,处理请求,并响应数据
3、到 web.xml 中去配置 servlet 程序的访问地址

代码实现

package com.aiguigu.servlet;

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

public class HelloServlet 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("Hello Servlet 被访问了");
    }

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

    @Override
    public void destroy() {

    }
}



    
    
        
        HelloServlet
        
        com.aiguigu.servlet.HelloServlet
    

    
    
        
        HelloServlet
        
        /hello
    


运行结果:

常见的错误 1:url-pattern 中配置的路径没有以斜杆打头。

常见错误 2:servlet-name 配置的值不存在

常见错误 3:servlet-class 标签的全类名配置错误:

1.3、url 地址到 Servlet 程序的访问

1.4、 Servlet 的生命周期

1、执行 Servlet 构造器方法
2、执行 init 初始化方法

第一步、第二步,是在第一次访问,的时候创建 Servlet 程序会调用

3、执行 service 方法

第三步,每次访问都会调用

4、执行 destroy 销毁方法

第四步,在web工程停止的时候调用

1.5、GET 和 POST 请求的分发处理

1、通过继承 HttpServlet 实现 Servlet 程序
2、使用 Eclipse 创建 Servlet 程序
3、Servlet 类的继承体系

在 ServletRequest 按 Ctrl + H 进入 接口关系图

可以看到 HttpServletRequest 子类 有 getMethod() 方法

package com.aiguigu.servlet;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.io.IOException;

public class HelloServlet implements Servlet {

    public HelloServlet() {
        System.out.println("1、执行 Servlet构造器");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("2、执行 init 初始化方法");
    }

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

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("Hello Servlet 被访问了");
        // 类型转换 (因为他有 getMethod()方法)
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        //获取请求方式
        String method = httpServletRequest.getMethod();

        if ("GET".equals(method)){
            doGet();
        }else if ("POST".equals(method)){
           doPost();
        }
    }

    
    public void doGet(){
        System.out.println(" get请求");
        System.out.println(" get请求");
    }
    
    public void doPost(){
        System.out.println("post 请求");
        System.out.println("post 请求");
    }



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

    @Override
    public void destroy() {
        System.out.println("执行 destroy销毁方法");
    }
}




    
    Title



1.6、通过继承 HttpServlet 实现 Servlet 程序

一般在实际项目开发中,都是使用继承 HttpServlet 类的方式去实现 Servlet 程序。
1、编写一个类去继承 HttpServlet 类
2、根据业务需要重写 doGet 或 doPost 方法
3、到 web.xml 中的配置 Servlet 程序的访问地址

package com.aiguigu.servlet;

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

public class HelloServlet2 extends HttpServlet {

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

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

    }
}


    
    
        HellowServlet2
        com.aiguigu.servlet.HelloServlet2
    
    
    
        HellowServlet2
        /hello2
    


1.7、使用 IDEA 创建 Servlet 程序


会给你创建好:

HelloServlet3
com.aiguigu.servlet.HelloServlet3

自己配置:


    HelloServlet3
    /hello3

1.8、Servlet 类的继承体系

2、ServletConfig 类

ServletConfig 类从类名上来看,就知道是 Servlet 程序的配置信息类
Servlet 程序和 ServletConfig 对象都是由 Tomcat 负责创建,我们负责使用
Servlet 程序默认是第一次访问的时候创建,ServletConfig 是每个 Servlet 程序创建时,就创建一个对应的 ServletConfig 对象

2.1、ServletConfig 类的三大作用

1、可以获取 Servlet 程序的别名 servlet-name 的值
2、获取初始化参数 init-param
3、获取 ServletContext 对象

 @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("2、执行 init 初始化方法");

        //1、可以获取 Servlet 程序的别名 servlet-name 的值
        System.out.println("HelloServlet程序的别名是:" +servletConfig.getServletName());
        //2、获取初始化参数 init-param
        System.out.println("初始化参数username的值是:" +servletConfig.getInitParameter("username"));
        System.out.println("初始化参数url的值是:" +servletConfig.getInitParameter("url"));
        //3、获取 ServletContext 对象
        System.out.println(servletConfig.getServletContext());

    }

web.xml的配置:

 
    
        
        HelloServlet
        
        com.aiguigu.servlet.HelloServlet
        
        
        
            
            username
                       
            root
        
        
        
            url
            jdbc:mysql://localhost:3306/test
        
    

运行结果:

1、执行 Servlet构造器
2、执行 init 初始化方法
HelloServlet程序的别名是:HelloServlet
初始化参数username的值是:root
初始化参数url的值是:jdbc:mysql://localhost:3306/test
org.apache.catalina.core.ApplicationContextFacade@7f41c15b
Hello Servlet 被访问了
 get请求
 get请求
3、ServletContext 类 3.1、什么是 ServletContext?

1、ServletContext 是一个接口,它表示 Servlet 上下文对象
2、一个web工程,只有一个 ServletContext 对象实例
3、ServletContext 对象是一个域对象
4、ServletContext 是在web 工程部署启动的时候创建。在web 工程停止的时候销毁

什么是域对象?
域对象,是可以像Map 一样存取数据的对象,叫域对象。
这里的域指的是存取数据的操作范围。整个 web 工程

3.2、ServletContext 类的四个作用

1、获取 web.xml中配置的上下文参数 context-param
2、获取当前的工程路径,格式:/工程路径
3、获取工程部署后在服务器硬盘上的绝对路径
4、像 Map 一样存取数据

代码实现:

package com.aiguigu.servlet;

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

public class ContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了 doGet方法");
//        1、获取 web.xml中配置的上下文参数 context-param
        ServletContext context = getServletConfig().getServletContext();

        String username = context.getInitParameter("username");
        System.out.println("context-param参数username的值是:" + username);
        System.out.println("context-param参数password的值是:" + context.getInitParameter("password"));
//        2、获取当前的工程路径,格式:/工程路径
        System.out.println("当前工程路径:" + context.getContextPath());
//        3、获取工程部署后在服务器硬盘上的绝对路径
        
        System.out.println("工程部署的路径是:" + context.getRealPath("/"));
        System.out.println("工程下css目录的绝对路径是:" + context.getRealPath("/css"));
        System.out.println("工程下img目录的绝对路径是:" + context.getRealPath("/img"));
//        4、像 Map 一样存取数据
    }
}

web.xml的配置:

 
    
        username
        context
    
    
    
        password
        root
    

  
        ContextServlet
        com.aiguigu.servlet.ContextServlet
    
 
        ContextServlet
        /contextservlet
    

运行效果:

执行了 doGet方法
context-param参数username的值是:context
context-param参数password的值是:root
当前工程路径:/06_servlet
工程部署的路径是:C:bianchenIdeaProjectsJavaWeboutartifacts06_servlet_war_exploded
工程下css目录的绝对路径是:C:bianchenIdeaProjectsJavaWeboutartifacts06_servlet_war_explodedcss
工程下img目录的绝对路径是:C:bianchenIdeaProjectsJavaWeboutartifacts06_servlet_war_explodedimg

ContextServlet1:

package com.aiguigu.servlet;

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

public class ContextServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //得到ServletContext对象
        ServletContext context = getServletContext();
        System.out.println("context1 中获取域数据key1的值是:" + context.getAttribute("key1"));
        context.setAttribute("key1","value1");
        System.out.println("context1 中获取域数据key1的值是:" + context.getAttribute("key1"));
        System.out.println("context1 中获取域数据key1的值是:" + context.getAttribute("key1"));
        System.out.println("context1 中获取域数据key1的值是:" + context.getAttribute("key1"));
    }
}

ContextServlet2:

package com.aiguigu.servlet;

import javax.servlet.ServletContext;
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;

public class ContextServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取 ServletContext对象
        ServletContext context = getServletContext();
        System.out.println("context2 中获取域数据key1的值是:" + context.getAttribute("key1"));

    }
}

运行结果:

context2 中获取域数据key1的值是:null
context1 中获取域数据key1的值是:null
context1 中获取域数据key1的值是:value1
context1 中获取域数据key1的值是:value1
context1 中获取域数据key1的值是:value1
context2 中获取域数据key1的值是:value1
4、Http 协议 4.1、什么是 HTTP协议

什么是协议?
协议是指双方,或多方,相互约定好,大家都需要遵守的规则,叫协议。
所谓 HTTP协议,就是指,客户端和服务器之间通信时,发送的数据,需要遵守的规则,叫HTTP 协议。
HTTP 协议中的数据又叫报文。

4.2、请求的 HTTP 协议格式

客户端给服务器发送数据叫请求
服务器给客户端回传数据叫响应

请求又分为 GET 请求,和 POST 请求两种

GET 请求

1、请求行
(1)请求的方式 GET
(2)请求的资源路径[+?+请求参数]
(3)请求的协议的版本号 HTTP/1.1
2、请求头
key:value 组成 不同的键值对,表示不同的含义

POST 请求

1、请求行
(1)请求的方式 POST
(2)请求的资源路径[+?+请求参数]
(3)请求的协议的版本号 HTTP/1.1

常见请求头的说明

Accept: 表示客户端可以接收的数据类型
Accept-Languege: 表示客户端可以接收的语言类型
User-Agent: 表示客户端浏览器的信息
Host: 表示请求时的服务器 Ip 和端口号

哪些是 GET 请求,哪些是 POST 请求

GET 请求有哪些
1、form 标签 method=get
2、a标签
3、link 标签引入 css
4、script 标签引入 js 文件
5、img 标签引入图片
6、iframe 引入 html 页面
7、在浏览器地址栏中输入地址后敲回车

POST 请求有哪些:
8、form 标签 method=post

4.3、响应的 HTTP 协议格式

1、响应行
(1)响应的协议和版本号
(2)响应状态码
(3)响应状态描述符
2、响应头
(1)key:value 不同的响应头,有其不同含义
空行
3、响应体 —>>> 就是回传给客户端的数据

4.4、常用的响应码说明

200 表示请求成功
302 表示请求重定向
404 表示请求服务器已经收到了,但是你要的数据不存在(请求地址错误)
500 表示服务器已经收到请求,但是服务器内部错误(代码错误)

4.5、MIME 类型 说明

谷歌浏览器如何查看 HTTP 协议

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659454.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号