欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

荐 JavaWeb之Servlet(二)Servlet生命周期方法与GET/POST请求分发

程序员文章站 2022-07-06 16:31:40
文章目录前言一、Servlet生命周期方法二、Servlet请求分发Ⅰ、提交表单Ⅱ、请求分发完前言–>视频教程传送门<–项目结构一、Servlet生命周期方法只调用一次执行Servlet构造器方法执行init初始化方法每次访问服务器时调用执行service方法关闭时调用执行destroy方法使用IDEA快捷键Alt+Insert创建以下方法package com.HolaAs;import javax.servlet.*;import...

导航

·JavaWeb之Servlet(一)Maven创建Servlet项目时的换源与运行时404的可能解决方案
·JavaWeb之Servlet(二)Servlet生命周期方法与GET/POST请求分发
·JavaWeb之Servlet(三)ServletConfig与ServletContext
·JavaWeb之Servlet(四)HttpServletRequest与HttpServletResponse

前言

–>视频教程传送门<–

项目结构
荐
                                                        JavaWeb之Servlet(二)Servlet生命周期方法与GET/POST请求分发

一、Servlet生命周期方法

只调用一次

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

每次访问服务器时调用

  1. 执行service方法

关闭时调用

  1. 执行destroy方法

使用IDEA快捷键Alt+Insert创建以下方法

package com.HolaAs;

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

public class testServlet implements Servlet {

    public testServlet() {
        System.out.println("First:Construction");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException{
        System.out.println("Second:Initialization");
    }

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

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

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

    @Override
    public void destroy() {
        System.out.println("Forth:Destruction");
    }

}

浏览器访问testServlet,Server命令台有回显:
荐
                                                        JavaWeb之Servlet(二)Servlet生命周期方法与GET/POST请求分发

二、Servlet请求分发

Ⅰ、尝试GET和POST请求

在webapp文件夹创建一个提交页面,action指向url-pattern

<!DOCTYPE html>
<html lang="en">
	<head>
    	<meta charset="UTF-8">
    	<title>This is for test</title>
	</head>
	
	<body>
		<form action="http://localhost:8080/javaWeb_Repository_war_exploded/testServlet" method="get">
    		<input type="submit">
		</form>
	</body>
</html>

访问http://localhost:8080/javaWeb_Repository_war_exploded/forTest.html
提交表单,网页定位到http://localhost:8080/javaWeb_Repository_war_exploded/testServlet?
访问了testServlet程序,服务器命令台有回显

而method改成post时,与get的效果一致

由于get和post是两种方法,我们希望把二者分开,即分发请求

Ⅱ、请求分发

修改service方法
通过多态的向下转型,将ServletRequest转型成HttpServletRequest
HttpServletRequest类提供了getmethod()可以得到请求类型

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

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String method = httpServletRequest.getMethod();

        if (method.equals("GET")){
            System.out.println("method == GET");
        }else if(method.equals("POST")){
            System.out.println("method == POST");
        }

    }

荐
                                                        JavaWeb之Servlet(二)Servlet生命周期方法与GET/POST请求分发
验证分发成功
最终封装修改程序为:

package com.HolaAs;

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

public class testServlet implements Servlet {

    public testServlet() {
        System.out.println("First:Construction");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException{
        System.out.println("Second:Initialization");
    }

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

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

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String method = httpServletRequest.getMethod();

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

    }

    public void doGet(){
        System.out.println("method == GET");
    }

    public void doPost(){
        System.out.println("method == POST");
    }

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

    @Override
    public void destroy() {
        System.out.println("Forth:Destruction");
    }

}

三、实际开发时通过继承HttpServlet实现Servlet程序

程序代码:

package com.HolaAs;

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 testServlet2 extends HttpServlet {

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

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

再次提交表单观察命令台
荐
                                                        JavaWeb之Servlet(二)Servlet生命周期方法与GET/POST请求分发
成功分发

欢迎在评论区留言
感谢浏览

本文地址:https://blog.csdn.net/Xxy605/article/details/107249213