1. 程式人生 > 實用技巧 >Servlet程式建立的兩種方式

Servlet程式建立的兩種方式

方式一:實現Servlet介面

步驟:
* 1.宣告一個Servlet類,實現Servlet介面
* 2.實現service方法,處理請求,並響應資料
* 3.到web.xml中去配置Servlet程式的訪問地址

方式二:繼承HttpServlet類

步驟:
* 1.宣告一個Servlet類,繼承於HttpServlet類
* 2.重寫doGet()方法或者doPost()方法
* 3.到web.xml中去配置Servlet程式的訪問地址


相關程式碼
package com.orzjiangxiaoyu.servlet;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServlet;

import java.io.IOException; /** * Servlet程式實現方式一:實現Servlet介面 *步驟: * 1.宣告一個Servlet類,實現Servlet介面 * 2.實現service方法,處理請求,並響應資料 * 3.到web.xml中去配置Servlet程式的訪問地址 * @author orz * @create 2020-09-09 23:23 */ //1.宣告一個Servlet類,繼承於Servlet介面 public class Servlet1 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 { // 2.實現service方法,處理請求,並響應資料
System.out.println("實現service方法,處理請求"); } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
View Code
package com.orzjiangxiaoyu.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Servlet程式實現方式二:繼承HttpServlet類
 * 步驟:
 * 1.宣告一個Servlet類,繼承於HttpServlet類
 * 2.重寫doGet()方法或者doPost()方法
 * 3.到web.xml中去配置Servlet程式的訪問地址
 * @author orz
 * @create 2020-09-09 23:33
 */

//1.宣告一個Servlet類,繼承於HttpServlet類

public class Servlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        2.重寫doGet()方法或者doPost()方法
        System.out.println("Servlet2 doGet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Servlet2 doPost");
    }
}
View Code
package com.orzjiangxiaoyu.servlet;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;

import java.io.IOException;

/**Servlet程式實現方式一:實現Servlet介面,加上doGet請求或doPost請求
 * @author orz
 * @create 2020-09-09 23:39
 */

//1.宣告一個Servlet類,繼承於Servlet介面

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

    }

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

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

        //2.實現service方法,處理請求,並響應資料
        HttpServletRequest servletRequest1 = (HttpServletRequest) servletRequest;
        String method = servletRequest1.getMethod();
        if("GET".equals(method))
        {
            doGet();
        }
        else if("POST".equals(method))
        {
            doPost();
        }
    }

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

    @Override
    public void destroy() {

    }
}
View Code

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
<!--  Servlet程式實現方式一:實現Servlet介面   -->
    <!--    到web.xml中去配置Servlet程式的訪問地址-->
    <servlet>
        <servlet-name>Servlet1</servlet-name>
        <servlet-class>com.orzjiangxiaoyu.servlet.Servlet1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet1</servlet-name>
        <url-pattern>/servlet1</url-pattern>
    </servlet-mapping>

<!--    -->

<!--    Servlet程式實現方式二:繼承HttpServlet類-->
    <servlet>
        <servlet-name>Servlet2</servlet-name>
        <servlet-class>com.orzjiangxiaoyu.servlet.Servlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet2</servlet-name>
        <url-pattern>/servlet2</url-pattern>
    </servlet-mapping>
<!--    -->

<!--    Servlet程式實現方式一:實現Servlet介面,加上doGet請求或doPost請求-->
    <servlet>
        <servlet-name>Servlet3</servlet-name>
        <servlet-class>com.orzjiangxiaoyu.servlet.Servlet3</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet3</servlet-name>
        <url-pattern>/servlet3</url-pattern>
    </servlet-mapping>
</web-app>
View Code