亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java啟動(dòng)Tomcat的實(shí)現(xiàn)步驟

 更新時(shí)間:2022年05月05日 10:17:47   作者:qq_32510597  
本文主要介紹了Java啟動(dòng)Tomcat的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

目標(biāo):

學(xué)習(xí)使用java代碼啟動(dòng)Tomcat。

實(shí)現(xiàn):

一、前期準(zhǔn)備

實(shí)現(xiàn)自定義注解,并用自定義注解模擬正常的業(yè)務(wù)邏輯,實(shí)現(xiàn)將用戶(hù)發(fā)送給服務(wù)器的數(shù)據(jù)回寫(xiě)給用戶(hù)的功能。

1、加入依賴(lài)

本項(xiàng)目使用java代碼操作Tomcat,所以需要引入servletjar包。為了在瀏覽器等前端顯示信息,所以需要引入JSP相關(guān)jar包。源碼如下:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>handwritingproject</artifactId>
        <groupId>com.njust</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>javaStartTomcat</artifactId>
    <dependencies>

        <!--Java語(yǔ)言操作tomcat -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.16</version>
        </dependency>

        <!-- tomcat對(duì)jsp支持 -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>8.5.16</version>
        </dependency>

    </dependencies>

</project>

2、定義Servlet類(lèi)

定義IndexServlet類(lèi),繼承 HttpServlet,該類(lèi)只是簡(jiǎn)單的向客戶(hù)端輸出一行信息以證明服務(wù)成功啟動(dòng)。源碼如下:

IndexServlet .java

package com.njust.servlet;

import java.io.IOException;

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

public class IndexServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().print("this is index... tomcat");
	}

}

3、定義Servlet類(lèi)

定義IndexServletC類(lèi),繼承 HttpServlet,該類(lèi)只是簡(jiǎn)單的向客戶(hù)端輸出一行信息以證明服務(wù)成功啟動(dòng)。該類(lèi)和上面的類(lèi)功能相同,只是輸出不同的信息,以證明Tomcat服務(wù)器可以將請(qǐng)求分發(fā)到不同的servlet處理。源碼如下:

IndexServletC .java

package com.njust.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 IndexServletC extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().print("this is index... tomcat IndexServletC");
	}

}

4、啟動(dòng)Tomcat并測(cè)試

定義Main類(lèi)。配置Tomcat的基本信息。同時(shí)將不同路徑映射到不同的servlet。源碼如下:

Main .java

package com.njust.tomcat;


import com.njust.servlet.IndexServletC;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.Tomcat.FixContextListener;

import com.njust.servlet.IndexServlet;

/**
 * 使用Java語(yǔ)言創(chuàng)建Tomcat服務(wù)器<br>
 * Tomcat 底層執(zhí)行程序 最終servlet<br>
 * SpringMVC底層使用servlet 包裝<br>
 *
 */
public class Main {
    private static int PORT = 8080;
    private static String CONTEX_PATH = "/njust";
    private static String SERVLET_NAME = "indexServlet1";
    private static String SERVLET_NAMET = "indexServletC";

    public static void main(String[] args) throws LifecycleException, InterruptedException {

        // 創(chuàng)建tomcat服務(wù)器
        Tomcat tomcatServer = new Tomcat();
        // 指定端口號(hào)
        tomcatServer.setPort(PORT);
        // 是否設(shè)置自動(dòng)部署
        tomcatServer.getHost().setAutoDeploy(false);
        // 創(chuàng)建上下文
        StandardContext standardContex = new StandardContext();
        standardContex.setPath(CONTEX_PATH);
        // 監(jiān)聽(tīng)上下文
        standardContex.addLifecycleListener(new FixContextListener());
        // tomcat容器添加standardContex
        tomcatServer.getHost().addChild(standardContex);

        // 創(chuàng)建Servlet
        tomcatServer.addServlet(CONTEX_PATH, SERVLET_NAME, new IndexServlet());
        tomcatServer.addServlet(CONTEX_PATH, SERVLET_NAMET, new IndexServletC());
        // servleturl映射
        standardContex.addServletMappingDecoded("/index", SERVLET_NAME);
        standardContex.addServletMappingDecoded("/indexc", SERVLET_NAMET);
        tomcatServer.start();
        System.out.println("tomcat start success..");
        // 異步進(jìn)行接收請(qǐng)求
        tomcatServer.getServer().await();

    }

}

運(yùn)行該程序。控制臺(tái)輸出如下:

Console

四月 03, 2020 3:32:30 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8080"]
四月 03, 2020 3:32:31 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
四月 03, 2020 3:32:31 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service [Tomcat]
四月 03, 2020 3:32:31 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/8.5.16
四月 03, 2020 3:32:31 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
tomcat start success..

根據(jù)控制臺(tái)信息,我們發(fā)現(xiàn)程序成功啟動(dòng)。接下來(lái)我們?cè)跒g覽器訪(fǎng)問(wèn)Tomcat服務(wù)器。

在這里插入圖片描述

我們?cè)L問(wèn) http://localhost:8080/njust/indexc可以定位到 IndexServletC,訪(fǎng)問(wèn) http://localhost:8080/njust/index,可以定位到 IndexServlet。說(shuō)明的Tomcat可以正常工作了。

在這里插入圖片描述

總結(jié)

流程圖

重點(diǎn)及易錯(cuò)點(diǎn)

1、服務(wù)器阻塞等待請(qǐng)求

tomcatServer.start();
System.out.println("tomcat start success..");
// 異步進(jìn)行接收請(qǐng)求
tomcatServer.getServer().await();

在服務(wù)啟動(dòng)后,一定要調(diào)用 await() 方法異步進(jìn)行接收請(qǐng)求,否則服務(wù)器啟動(dòng)成功就直接關(guān)閉了。

到此這篇關(guān)于Java啟動(dòng)Tomcat的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)Java啟動(dòng)Tomcat內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論