淺談servlet的生命週期 ,以及【Request請求】
阿新 • • 發佈:2018-12-12
servlet 的生命週期,從建立到銷燬;
在接收request的時候Tomcat呼叫init方法建立servlet,service對request 或response 進行處理,關閉伺服器時候呼叫destory方法,servlet銷燬;【多次請求 init()方法僅僅被呼叫一次】;
request 常用到的方法:
// 請求頭key 請求頭value // referer 瀏覽器通知伺服器,當前請求來自何處,如果是直接訪問,則不會有這個頭。常用於:防盜鏈 // If-modified-Since 瀏覽器通知伺服器,本地快取的最後變更時間。與另一個響應頭組合控制瀏覽器頁面的快取。 // cookie 與會話有關技術,用於存放瀏覽器快取的cookie資訊。 // user-agent 瀏覽器通知伺服器,客戶端瀏覽器與作業系統相關資訊 // connection 保持連線狀態。Keep-Alive 連線中,close 已關閉 // host 請求的伺服器主機名 // content-length 請求體的長度 // content-type如POST請求,response.setHeader("content-type", "text/html;charset=UTF-8"); // accept 瀏覽器可支援的MIME型別。檔案型別的一種描述方式。 // mime格式 瀏覽器請求資料的型別,例如: text/html ,html檔案 text/css,css檔案 // text/javascript,js檔案 image/*,所有圖片檔案 // accept-encoding 瀏覽器通知伺服器,瀏覽器支援的資料壓縮格式。如:GZIP壓縮 // accept-language 瀏覽器通知伺服器,瀏覽器支援的語言。各國語言(國際化i18n)
怎麼判斷請求的方式:
使用 getMethod()方法,來判斷請求是post還是get;
package com.baidu; 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; @WebServlet(name = "ServletDemo2", urlPatterns = "/ServletDemo2") public class ServletDemo2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ doGet(request, response);// } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //String getMethod()獲取請求方式的型別 //StringBuffer getRequestURL()獲取客戶端發出請求完整URL //String getRemoteAddr()獲取IP地址 //String getProtocol()獲取當前協議的名稱和版本 sop(request.getMethod()); sop(request.getRequestURL()); sop(request.getRemoteAddr()); sop(request.getProtocol()); System.out.println("-------------------------------"); sop(request.getHeader("user-agent"));//作業系統相關資訊; } public void sop(Object obj){ System.out.println(obj); } }
請求域【 轉發,servlet 中的資料共享】:
//RequestDispatcher getRequestDispatcher(String path)獲取請求轉發器(request物件方法) //將請求轉發到另一個資源(Servlet)上 //void forward(ServletRequest request, ServletResponse response)
//獲取請求體: //String getParameter(String name) //getParameter獲得指定引數名對應的值。如果沒有返回null,如果只有多個獲得第一個。 例如:username=jack //String[] getParameterValues(name) //getParameterValues[] 獲取請求資料key相同的多個數據 // request 的生命週期 innit-->destroy //void setAttribute(String name, Object o)往request域中設定值 //Object getAttribute(String name)從request域中取值 //void removeAttribute(String name)從request域中移除值
請求轉發:
ServletDemo4 轉發至 urlPatterns=" / post " 的 ServletDemo3,然後獲取request 域中的資料;
package com.baidu;
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;
@WebServlet(name = "ServletDemo4", urlPatterns = "/ServletDemo4")
public class ServletDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);//
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("forwardName", "王五");
//現在把demo4 的請求轉發到demo3 上request域中
request.getRequestDispatcher("/post").forward(request, response);
}
}
package com.baidu;
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.Arrays;
@WebServlet(name = "ServletDemo3", urlPatterns = "/post")
public class ServletDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);//
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//存在亂碼開啟下面編碼集
// request.setCharacterEncoding("UTF-8");
//response.setContentType("text/html;charset=utf-8");
//response.setHeader("content-type", "text/html;charset=UTF-8");
String username = request.getParameter("username");//獲取value
String password = request.getParameter("password");
String[] hobbies = request.getParameterValues("hobby");//hobby 擁有多個parameters
System.out.println(username+password+ Arrays.toString(hobbies));
//
request.setAttribute("name","張三" );
request.setAttribute("age","18" );
Object name = request.getAttribute("name");
Object age = request.getAttribute("age");
System.out.println(name+"="+age);
// 從servletDemo4中接收到轉發的請求,從請求域request中取出來
Object forwardName = request.getAttribute("forwardName");
System.out.println(forwardName);
}
}
然後請求ServletDemo4, 在ServletDemo3中通過request域 獲取屬性,結果如下:
測試ServletDemo3的post請求:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>post的方式提交</title>
</head>
<body>
<form action="http://localhost:8080/post" method="post">
使用者名稱:<input type="text" name="username"/><br/>
密碼:<input type="text" name="password"/><br/>
愛好:<input type="checkbox" name="hobby"value="smoking"/> 抽菸
<input type="checkbox" name="hobby"value="drinking"/> 喝酒
<input type="checkbox" name="hobby" value="tangtou"/> 燙頭 <br/>
<select name="education">
<option value="gaozhong">高中</option>
<option value="dazhuan">大專</option>
<option value="benke">本科</option>
</select><br/>
<input type="submit"value="post的方式提交" />
</form>
</body>
</html>
啟動Tomcat伺服器,點選提交按鈕:
發現存在亂碼,設定編碼集即可【上面講過】;
request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8");
再次部署專案執行: