Servlethtml
server applet運行在服務器端的小程序,servlet就是一個接口,定義了Java類被瀏覽器訪問到的規則(Java類重寫這個接口,就能夠被瀏覽器(tomcat)識別)java
Servlet方法:web
init方法:只執行一次,Servlet在第一次被訪問時或者在服務器啓動時被建立,就會執行init方法,小程序
service方法:能夠執行屢次,每一次訪問服務器就執行一次數組
destroy方法:在服務器被關閉前,就執行一次瀏覽器
ServletConfig getSerletConfig方法:獲取ServletConfig對象tomcat
String getServletInfo:獲取Servlet的一些信息服務器
在Servlet3.0以前須要這樣web.xml來配置項目的路徑cookie
在Servlet3.0以後能夠經過註解配置來選擇項目路徑了app
servlet是一個接口,他有兩個抽象子類GenericServlet和HttpServlet.
package com.Data.HttpServlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; //Http一共有其中請求方式 //可使用HttpServlet的方法來判斷請求方式 //繼承HttpServlet @WebServlet("/demo1") public class HttpServletDemo extends HttpServlet{ //重寫父類方法 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //若是是Get請求方式就執行此語句 System.out.println("doGet.....");//瀏覽器直接訪問服務器的地址使用的是Get方式 } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //若是是Post請求方式就執行此語句 System.out.println("doPost");//輸出結果doPost } }
使用post方式訪問服務器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!--指定地址demo3--> <!--指定訪問方式post--> <form action="/demo1" method="post"> <input name="username" placeholder="請輸入用戶名"> <input type="submit" value="登陸"> </form> </body> </html>
概念:Hyper Text Transfer Protocol 超文本傳輸協議,定義了客戶端和服務器端通訊時,發送數據的格式
1.請求行 :
格式:請求方式(GET) 請求url(/Hello.html) 請求協議/版本(HTTP1.1)
當請求協議爲GET時,請求的參數在請求行中,爲POST時,請求的參數在請求體
2.請求頭:就是我瀏覽器告訴服務器我有哪些信息
字符串格式:請求頭名稱:請求頭值
常見的請求頭:
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Referer: http://localhost:8080/Hello.html(我火狐瀏覽器不知道怎麼會沒有顯示這個頭.....),這個頭能夠告訴服務器我從哪裏來
3.請求空行:就是一個空行,分隔請求頭和請求體
請求體:封裝POST請求消息的請求體參數的,username: zhangsan
1.request和response對象是由服務器建立的,
2.request對象是來獲取請求消息,response對象是來設置響應消息
ServletRequest(父接口)-->HttpServletRequest(子接口)--->RequestFacade(實現類)
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; //演示Request對象獲取請求行數據 @WebServlet( "/demo") public class Servlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取請求行的請求方式 //request對象有tomcat服務器建立 String method = request.getMethod(); System.out.println(method);//GET //獲取請求行的虛擬目錄 String method1 = request.getContextPath(); System.out.println(method1);// /day13 //獲取請求行的URI String method2 = request.getRequestURI(); System.out.println(method2);// /day13/demo //獲取客戶機的IP地址 String method3 = request.getRemoteAddr(); System.out.println(method3);// 0:0:0:0:0:0:0:1 //獲取請求行的請求參數 String method4 = request.getQueryString(); System.out.println(method4);// name=zhangsan } }
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Enumeration; @WebServlet("/RDemo1") public class RuquestDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取全部請求頭的名稱 Enumeration<String> headerNames = request.getHeaderNames(); //遍歷,至關於迭代器 //有下一個元素就爲真 while(headerNames.hasMoreElements()){ //獲取元素 String name = headerNames.nextElement(); //經過key找到value String value = request.getHeader(name); System.out.println(name+"------"+value); // 全部頭,獲取出來了 // host------localhost:8080 // connection------keep-alive // upgrade-insecure-requests------1 // user-agent------Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36 // accept------text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 //accept-encoding------gzip, deflate, br //accept-language------zh-CN,zh-HK;q=0.9,zh;q=0.8,en;q=0.7 //cookie------JSESSIONID=0329F34A83457E749B1818AFBE4C0A57; Idea-a0bb733=23efa255-9ce7-42d9-8d0b-9853348226b8; JSESSIONID=8FE68A2847DC38B4290ADBCE1BFD164F } } }
請求體:只有在POST請求方式,纔有請求體,在請求體中封裝了POST請求的請求參數
首先建立一個html文件,
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!--綁定服務器的地址--> <form action="/day13/RDemo1" method="post"> <input type="text" name="username"><br> <input type="text" name="password"><br> <!--向服務器提交數據--> <input type="submit" value="註冊"> </form> </body> </html>
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; @WebServlet("/RDemo1") public class RuquestDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取請求體的請求參數 //獲取字符流 BufferedReader reader = request.getReader(); String line=null; while((line=reader.readLine())!=null){ //讀取一行寫一行 System.out.println(line);// username=zhangsan&password=123 } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
其餘功能:
獲取請求參數通用方式,不管是get請求仍是post請求均可以使用下列方法來獲取參數值
1.String getParameter(String name):根據參數名稱獲取參數值
2.String[] getParameterValues(String name):根據參數名稱獲取參數值的數組
3.Enumeration
getParameterNames();獲取全部請求的參數名稱 4.Map<String,String[]> getParameterMap():獲取全部參數的map集合(與3不一樣的地方是,3所得到的鍵值對,是一個鍵只獲取一個值,4方法的話一個鍵能夠對應對個值)
Map方法的 演示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!--綁定服務器的地址--> <!--//不管是post請求仍是get請求都同樣--> <form action="/day13/Demo3" method="GET"> <input type="text" name="username"><br> <input type="text" name="password"><br> <input type="checkbox" name="hobby" value="打遊戲">打遊戲 <input type="checkbox" name="hobby" value="看電影">看電影 <!--向服務器提交數據--> <input type="submit" value="註冊"> </form> </body> </html>
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Map; import java.util.Set; @WebServlet("/Demo3") //不管是GET請求仍是POST請求均可以使用下面的方法獲取請求的參數 public class RequestDemo3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String,String[]> map = request.getParameterMap(); //把全部的鍵存到一個集合中 Set<String> strings = map.keySet(); for(String key : strings){ //獲取出每個鍵 System.out.println(key); //獲取出值的集合 String[] values = map.get(key); //獲取出每個值 for(String key2 : values){ System.out.println(key2); } System.out.println("---------------"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }