工作以來公司項目從開始的Spring+SpringMVC到現(xiàn)在直接使用Springboot丸氛,框架越用越爽,但凡涉及到框架內(nèi)部的東西自己始終是一知半解。只會簡單的使用饲齐,并不知道它內(nèi)部的實現(xiàn),很多時候程序報錯咧最,只要涉及框架永遠都是百度一下捂人,不知為何報錯以及為何要如此處理∈秆兀看著大神們的各種解決方案滥搭,自己始終不得其理,遂先從web基礎(chǔ)走起
一捣鲸、Servlet的概述
Servlet是用Java編寫的服務(wù)端程序瑟匆,其作用在于Servlet作為中間層連接著
瀏覽器或其他HTTP客戶端請求
和HTTP服務(wù)器上的數(shù)據(jù)庫或應(yīng)用程序
。就瀏覽器而言栽惶,如果把Servlet比作橋梁
那么servlet一端連接瀏覽器愁溜,一端連接服務(wù)器,他的作用就是保證瀏覽器和服務(wù)器之間的通信
Servlet在整個web程序中的位置
二外厂、Servlet生命周期
servlet中 javax.servlet.Servlet 接口的 提供了三個方法
init() 初始化時調(diào)用
service() 用來處理客戶端發(fā)送過來的請求
destory() servlet被銷毀調(diào)用
代碼中一窺就近
1冕象、創(chuàng)建一個web項目
2、在src新增一個包汁蝶,包下新增一個類繼承HttpServlet渐扮,類中代碼如下
public class ServletController extends HttpServlet {
private static final long serialVersionUID = 8721524830738176364L;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
System.out.println("init-初始化");
}
@Override
public void service(HttpServletRequest request, HttpServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("執(zhí)行了service方法;");
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("執(zhí)行了doPost方法;");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("執(zhí)行了doGet方法");
PrintWriter out = resp.getWriter();
resp.setContentType("text/html;charset=ISO-8859-1");
out.println("<h1>你好GET方法</h1>");;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("執(zhí)行了destroy方法;");
}
}
web.xml中配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servletdemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- GET請求 -->
<servlet>
<!-- servlet名稱 -->
<servlet-name>servletDemo</servlet-name>
<!-- 名稱對應(yīng)的類 -->
<servlet-class>com.ServletController</servlet-class>
</servlet>
<servlet-mapping>
<!-- 這里的servlet名稱和<servlet>中的名保持一致 -->
<servlet-name>servletDemo</servlet-name>
<!-- 請求的時url -->
<url-pattern>/servlet/hellowServlet</url-pattern>
</servlet-mapping>
<!-- GET請求 -->
<!-- POST請求 -->
<servlet>
<servlet-name>servletPost</servlet-name>
<servlet-class>com.ServletController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletPost</servlet-name>
<url-pattern>/servlet/postServlet</url-pattern>
</servlet-mapping>
<!-- POST請求 -->
</web-app>
inde.jsp中的內(nèi)容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Servlet</h1>
<!-- a標簽?zāi)J的GET請求 -->
<a href="servlet/hellowServlet">GET請求</a>
<!-- 表單提交 -->
<form action="servlet/postServlet" method="post">
<p>客戶名稱: <input type="text" name="CustomerName" style="width: 300px" /></p>
<p>客戶電話: <input type="text" name="CustomerTel" style="width: 300px" /></p>
<input type="submit" value="POST請求" />
</form>
</body>
</html>
配置文件說明:
請求地址:本機地址:tomact端口/項目名 ===>http://localhost:8089/servletdemo
ps
:Tomcat默認端口是8080,我這里改了所以是8089穿仪,因為web.xml中的<welcome-file-list></welcome-file-list>的配置默認是依次往下找席爽,所以根據(jù)上述地址能找到index.jsp
通過代碼運行可知
第一次運行時先執(zhí)行init()然后執(zhí)行了 service()
第二執(zhí)行時只執(zhí)行了service()方法
當我通過stop關(guān)閉tomcat時執(zhí)行了destory()方法
當service()方法存在
時,無論是執(zhí)行GET請求或者POST請求時啊片,都是執(zhí)行service()
當service()方法注釋
時只锻,執(zhí)行GET請求時調(diào)用的時doGet(),執(zhí)行POST請求時執(zhí)行doPost()
ps:關(guān)于service()方法和doGet()及doPost()方法之間的關(guān)系我這里做一個簡單的說明
這里大概的意思是:在接收HTTP請求時紫谷,service()方法會將請求分派到該類的定義的doXXX方法
當重寫了service()方法后齐饮,請求執(zhí)行的是我們重寫的service()方法,所以當執(zhí)行到service()并未做分派笤昨,反之祖驱,則亦然,所以才會出現(xiàn)上述關(guān)于service()方法的情況