1. 程式人生 > 實用技巧 >springboot中對sqlSessionFactoryBean自定義

springboot中對sqlSessionFactoryBean自定義

1、簡介

Tomcat工作機制

(1)什麼是Servlet?

Java Servlet 是執行在 Web 伺服器或應用伺服器上的程式,它是作為來自 Web 瀏覽器或其他 HTTP 客戶端的請求和 HTTP 伺服器上的資料庫或應用程式之間的中間層

(2)編寫Servlet

①建立一個HelloServlet繼承HttpServlet,重寫doGet和doPost方法(快捷鍵Alt+Ins),也就是看請求的方式是get還是post,然後用不同的處理方式來處理請求

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

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

②在web.xml中配置HelloServlet (讓瀏覽器發出的請求知道到達哪個servlet,也就是讓tomcat將封裝好的request找到對應的servlet讓其使用)

<?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"
         metadata-complete="true">

    <servlet>

        <servlet-name>hello</servlet-name>
        <servlet-class>com.yu.servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

配置之後,瀏覽器是如何通過我們配置的資訊來找到對應的servlet的呢?
按照步驟,首先瀏覽器通過http://localhost:8080/s1/HelloServlet來找到web.xml中的url-pattern,這就是第一步,匹配到了url-pattern後,就會找到第二步servlet的名字hello,知道了名字,就可以通過servlet-name找到第三步,到了第三步,也就能夠知道servlet的位置了。然後到其中找到對應的處理方式進行處理。

例圖:

參考連結:https://www.cnblogs.com/whgk/p/6399262.html
https://blog.csdn.net/qq_19782019/article/details/80292110