Servlet技術 用來動態生成 網頁數據資源
Servlet生成HTML 頁面數據時,所有內容都是通過 response.getWriter response.getOutputStream 向瀏覽器輸出的
<html>
<head>
</head>
<body>
Hello
</body>
</html>
用Servlet 輸出流打印網頁信息
response.getWriter().print("<html>");
Servlet生成網頁缺點
1、通過print輸出網頁 開發很不方便
2、美工人員經常對頁面進行CSS修飾,美工人員在java代碼中調試CSS
3、無法使用開發工具對頁面代碼進行調試 JS CSS
98前后 主流網頁設計技術 ASP PHP , sun推出Servlet,sun參考ASP語法推出JSP
* 什么是 JSP ? JSP 和Servlet技術一樣,都是動態網頁開發技術 ?Java Server Page
* JSP 和 Servlet 關系? JSP在運行時,需要先翻譯為Servlet才能運行 ------ JSP 就是 Servlet ,這個工作是由編譯器給搞定了?
Servlet2.5 版本 ---- JavaEE 5.0 ---- JSP 版本2.1
編寫第一個JSP程序
1、JSP位于WebRoot下 --- 不能放入WEB-INF(不能訪問)
2、修改JSP默認編碼 window --- preferences --- JSP 修改編碼 utf-8
3、修改JSP 默認編輯器 window --- preferences --- general ---- editor ---- File Associations 將 JSP編輯器改為 JSP Editor
4、在WebRoot下 新建 JSP ---- hello.jsp
5、寫JSP過程和編寫HTML一樣,可以通過<%%> 嵌入java代碼
* 有人比喻 Servlet:嵌入HTML的java文件 ; JSP :嵌入Java的HTML文件
JSP運行原理:
1、客戶端訪問hello.jsp
2、服務器讀取hello.jsp內容到內存
3、服務器根據hello.jsp內容生成Servlet程序 ----保存在哪? tomcat/work
4、Servlet編譯運行
JSP翻譯Servlet 固定包名 :org.apache.jsp
hello.jsp ---- hello_jsp.java
JSP中 HTML代碼 <%%> 代碼都會被翻譯Servlet 中 _jspService
翻譯規則:
1、JSP 中 HTML 翻譯 out.write
2. JSP中java代碼 不會翻譯
JSP中腳本元素 中都是使用%
1、聲明 <%! %> --- 定義內容將會被翻譯Servlet類 成員變量和方法
2、表達式 <%= %> ---- 用于向頁面輸出內容 等價于 out.print()
3、代碼塊<% %> --- 在<%%> 之間編寫任何java代碼
* 代碼塊可以和html嵌套使用
開發網頁程序,使用Servlet? 使用JSP呢?
Java代碼多 --- Servlet
HTML代碼多 ---- JSP
JSP運行時 總要翻譯Servlet ,效率很低 ?
* 只有第一次訪問JSP 進行翻譯 ,以后訪問,如果沒有修改 JSP 不會重新翻譯
EL 全名為Expression Language ---- 表達式語言
語法:${標識符}
${applicationScope.name } 等價于 <%=getServletContext().getAttribute("name") %>
${requestScope.address } 等價于 <%=request.getAttribute("address") %>
JSTL (JSP Standard Taglib Liberary) --- JSP 標準標簽庫
JSTL干嘛用的? 簡化頁面<%%> 與 HTML嵌套寫法 ----- 簡化JSP開發
今天學習目標: <c:forEach> <c:if> ----- 用來替換頁面中for循環 和 if條件判斷
在頁面內使用JSTL 簡化 if 和 for
1、導入jstl的jar包 --- MyEclipse中存在 javaee5 類庫 ---存在 jstl1.2.jar
2、在頁面最上方 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
* 定義了很多標簽,為標簽指定名稱空間 uri ,在使用標簽前 引用名稱空間
JSTL+EL 簡化 <%%> 腳本代碼
------------------------------------------------------------------------------------------------------
什么是會話? 用戶打開瀏覽器,訪問站點,連續進行多次操作,關閉瀏覽器 ,整個過程稱為會話 。
管理HTTP協議會話狀態 :Cookie 和Session
Cookie:將用戶相關數據,保存客戶端 , 用戶每次訪問服務器自動攜帶cookie數據 。
Session : 將用戶相關數據 保存服務器端,為每個客戶端生成一個獨立Session數據對象,通過對象唯一編號,區分哪個瀏覽器對應 哪個Session
Cookie快速入門 案例:上次訪問時間
1、通過服務器向客戶端寫cookie
Cookie cookie = new Cookie(name,value);
response.addCookie(cookie);
* 在HTTP協議響應頭信息中 Set-Cookie: last=1339556457609
2、當客戶端存在cookie之后,以后每次請求自動攜帶 HTTP協議請求頭信息 Cookie: last=1339556456859
服務器端獲得需要cookie數據
Cookie[] cookies = request.getCookies(); ---- 獲得客戶端所有cookie
if(cookies==null){} 判斷cookie是否存在
遍歷cookie獲得需要信息
for (Cookie cookie : cookies) {
// 獲得每個cookie
if (cookie.getName().equals("last")) {
// 找到了需要cookie
}
}
代碼附上:
public class CookieServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Cookie[] cookies = request.getCookies();response.setContentType("text/html;charset=utf-8");if (cookies == null) {long now = System.currentTimeMillis();response.addCookie(new Cookie("lastTime", now + ""));response.getWriter().write("歡迎首次來購物!");} else {for (Cookie cookie : cookies) {if (cookie.getName().equals("lastTime")) {String lastTime = cookie.getValue();System.out.println("第二次");Long time = Long.parseLong(lastTime);System.out.println(time);Date date = new Date(time);// 沒想到在這個時間上費了很大的力氣SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");response.getWriter().write("上次訪問時間是:" + dateFormat.format(date));}}}long now = System.currentTimeMillis();response.addCookie(new Cookie("lastTime", now + ""));}// 添加了這個后會更新上次的訪問時間
?
?
?
CookieAPI 詳解
1、將cookie寫回客戶端 response.addCookie(cookie);
2、讀取請求中 cookie信息 request.getCookies
3、Cookie對象創建 new Cookie(name,value )
* cookie的name 不允許改變 ----- getName 、getValue 、setValue
4、什么是會話cookie ,什么是持久cookie ?
cookie信息默認情況 保存在瀏覽器內存中 ------ 會話cookie
會話cookie 會在關閉瀏覽器時 清除
持久Cookie,cookie數據保存客戶端硬盤上
通過setMaxAge 設置Cookie為持久Cookie
* 在JavaAPI 中所有與時間相關參數,int 類型 單位秒, long類型 單位毫秒
5、訪問cookie有效路徑path
默認情況下,生成cookie時,產生默認有效訪問路徑 (默認生成cookie程序路徑)
http://localhost/day07/lastvisit --- 生成cookie --- path 默認值: /day07
http://localhost/day07/servlet/path ---- 生成cookie ---- path 默認值:/day07/servlet
第二次訪問程序攜帶cookie信息,如果訪問路徑與path不一致,不會攜帶cookie 信息
company cookie : path --- /day07/serlvet
last cookie : path --- /day07
訪問 :http://localhost/day07/servlet/path ---- 同時滿足/day07/serlvet、/day07 攜帶 company 和 last兩個cookie信息
訪問 :http://localhost/day07/lastvisit ---- 滿足 /day07 不滿足/day07/serlvet 攜帶 last 一個cookie 信息
* 以后程序開發,盡量設置path ---- setPath方法 ? ?主要是為了統一,好管理 這是我認為的 ?主要是為了方便管理cookie在哪里,什么時候能全部用到
6、第一方cookie 和 第三方cookie
通過setDomain 設置cookie 有效域名
訪問google時,生成cookie過程中 cookie.setDomain(".baidu.com") ---- 生成百度cookie ------ 第三方cookie
* 第三方cookie 屬于不安全 ----- 一般瀏覽器不接受第三方cookie
訪問google時,生成cookie,cookie.setDomain(.google.con) ---- 生成google的cookie ------ 第一方cookie
案例:刪除上次訪問時間
原理:設置cookie MaxAge為 0
* 刪除cookie時 必須設置path 與cookie的 path一致
案例:商品瀏覽記錄 ---- 可以通過Cookie實現
代碼附上:
Goods.jsp
<body> <h1>請選擇你想購買的商品</h1><a href="/day7/BuyHistory?id=1">洗衣機</a><a href="/day7/BuyHistory?id=2">冰箱</a><a href="/day7/BuyHistory?id=3">茅臺</a><a href="/day7/BuyHistory?id=4">手機</a><a href="/day7/BuyHistory?id=5">電腦</a><a href="/day7/BuyHistory?id=6">電視</a><br><h1>你上次瀏覽的商品是:</h1><br><a href="/day7/clearServlet">清除瀏覽記錄</a><br> <%String[] goods = { "洗衣機", "冰箱", "茅臺", "手機", "電腦", "電視" };Cookie[] cookies = request.getCookies();Cookie cookie = CookieUntil.findCookie(cookies,"history");String[] idsArr = cookie.getValue().split(",");for(String string :idsArr){out.print(goods[Integer.parseInt(string)-1]+"<br>");} %> </body>
Buyhistory.java
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");String[] goods = { "洗衣機", "冰箱", "茅臺", "手機", "電腦", "電視" };String id = request.getParameter("id");response.getWriter().write("你現在正在訪問的商品是:" + goods[Integer.parseInt(id) - 1]);response.getWriter().write("<a href=\"/day7/Goods.jsp\">點擊返回首頁</a>");Cookie[] cookies = request.getCookies();Cookie cookie = CookieUntil.findCookie(cookies, "history");if (cookie == null) {cookie = new Cookie("history", id);cookie.setMaxAge(60 * 60 * 60);cookie.setPath("/day7");response.addCookie(cookie);} else {String[] idsArr = cookie.getValue().split(",");String str = null;for (String string : idsArr) {if (string.equals(id)) {// 比較字符串一定要用equals方法return;} else {str = cookie.getValue() + "," + id;}}cookie.setValue(str);cookie.setMaxAge(60 * 60 * 60);cookie.setPath("/day7");response.addCookie(cookie);}}
CookieUntils.java
public class CookieUntil {public static Cookie findCookie(Cookie[] cookies, String name) {if (cookies == null) {return null;} else {for (Cookie cookie : cookies) {if (cookie.getName().equals(name)) {return cookie;}}}return null;} }
ClearServlet.java
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {Cookie cookie = new Cookie("history", "");cookie.setMaxAge(-1);cookie.setPath("/day7");response.addCookie(cookie);}
?
?
-----------------------------------------------------------------------------------------------------------------------
Session 是服務器端會話管理技術,服務器會為每個瀏覽器創建一個單獨Session對象,保存該瀏覽器(會話)操作相關信息。
與Cookie區別:cookie是保存在客戶端,Session保存在服務器端
為什么有Cookie技術?還需要Session ?
Cookie 存在客戶端 ----存在安全問題
Session將數據存在服務器,數據更加安全
* Session將數據保存在服務器端,占用服務器內存資源 ,Cookie不會占用服務器資源
例如:京東購物車信息 保存Cookie 、淘寶購物車信息保存Session
Session共享
實驗:用IE6 向Session保存一個數據,用另一個IE6讀取數據?
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {HttpSession httpSession = request.getSession();httpSession.setAttribute("name", "kongbin");response.setContentType("text/html;charset=utf-8");response.getWriter().write("已經寫入到了Session");}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");response.getWriter().print(request.getSession().getAttribute("name"));}
?
用第一個IE6保存session數據,當前瀏覽器可以獲得,但是第二個IE6 無法獲得第一個瀏覽器保存Session 數據
IE8以上 或 火狐瀏覽器:當打開多個瀏覽器窗口,之間Session共享
原因:IE6 cookie 中保存jsessionId 默認會話級別 ;IE8或者火狐 保存cookie中 jsessionId 默認持久cookie
問題:如何讓多個IE6 共享同一個Session ---- 將session id 持久化
問題:如果客戶端關閉瀏覽器,是否就刪除了Session ?
沒有,Session保存在服務器端
問題:IE6 保存Session,關閉瀏覽器,再次打開,數據丟失了?
IE6默認jsessionId保存會話Cookie中,關閉瀏覽器,會話cookie就會被刪除,客戶端丟失jsessionid 無法找到服務器對應Session對象
* 服務器Session對象還存在
Cookie cookie = new Cookie("JSESSIONID", httpSession.getId());cookie.setMaxAge(60 * 60);cookie.setPath("/day7");
?
?
Session購物車案例
1、商品列表,用戶點擊列表中商品,將商品添加購物車
2、查看購物車中商品,以及商品購買件數
product.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>請選擇你想購買的商品</h1><a href="/day7/shoppingcart?id=1">洗衣機</a><a href="/day7/shoppingcart?id=2">冰箱</a><a href="/day7/shoppingcart?id=3">茅臺</a><a href="/day7/shoppingcart?id=4">手機</a><a href="/day7/shoppingcart?id=5">電腦</a><a href="/day7/shoppingcart?id=6">電視</a><br><h1>你上次瀏覽的商品是:</h1><br><a href="/day7/clearServlet">查看購物車</a></body>
</html>
ShopingCart.java
package cn.binbin.com;import java.io.IOException; import java.util.HashMap; import java.util.Map;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;public class ShoppingCart extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");String[] goods = { "洗衣機", "冰箱", "茅臺", "手機", "電腦", "電視" };String id = request.getParameter("id");HttpSession httpSession = request.getSession();Map<String, Integer> map = (Map<String, Integer>) httpSession.getAttribute("map");// 這個地方是個重點,這個地方用到了一個比較好的思想if (map == null) {map = new HashMap<String, Integer>();map.put(id, 1);} else {if (map.containsKey(id)) {// 這個地方是添加的判斷map.put(id, map.get(id) + 1);} else {map.put(id, 1);// 第一次老是出現一件商品,這就是沒有添加的緣故 }}httpSession.setAttribute("map", map);// 忘記了寫回了response.setContentType("text/html;charset=utf-8");response.getWriter().write("你購買的商品是: " + goods[Integer.parseInt(id) - 1] + " ");response.getWriter().write("<a href = '/day7/cart.jsp'>" + " 查看購物車" + "</a>");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}
car.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@page import="java.util.HashMap"%> <%@page import="java.util.Map"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String[] goods = { "洗衣機", "冰箱", "茅臺", "手機", "電腦", "電視" };HashMap <String ,Integer> map = (HashMap<String,Integer>)request.getSession().getAttribute("map");for(String name :map.keySet()){out.print(goods[Integer.parseInt(name)-1]);out.println(map.get(name)+"<br>");}out.print("<a href='/day7/product.jsp'>"+"返回購物界面"+"</a>"); %> </body> </html>
?
?
禁用Cookie后,Session還能否使用?
可以,可以對url進行重寫,原理在url;jsessionid=xxx ---- 在程序中可以調用response.encodeURL進行URL重寫
* 如果服務器進行URL重寫,所有路徑都必須重寫
* 不要讓用戶禁用cookie
Cookie生命周期
Cookie 對象何時創建,何時銷毀
創建 : Cookie cookie = new Cookie(name,value) ; response.addCookie(cookie);
銷毀 : 會話cookie會在瀏覽器關閉時銷毀,持久cookie會在cookie過期(MaxAge)后銷毀
Session生命周期
創建:request.getSession() 創建Session
銷毀:三種 1、服務器關閉時銷毀 2、session過期時銷毀 3、手動調用session.invalidate
設置session過期時間
1、配置web.xml
<session-config>單位是分鐘:連續30分鐘不使用session
<session-timeout>30</session-timeout>
</session-config>
2、調用session對象 setMaxInactiveInterval(int interval) 單位是秒
HttpSession session = request.getSession();
session.setMaxInactiveInterval(60*60); 設置session過期時間1小時
上述是經典面試題
?
案例:Session經常用來完成系統權限和認證功能
權限:在用戶身份認證后,根據權限知道 --- 你能做什么
認證:用戶登陸 --- 你是誰
將登陸用戶信息保存到Session 有什么作用 ? ---- 如果session中沒有用戶信息 未登陸
案例:通過session實現一次性驗證碼
* 驗證碼從session獲取后,馬上刪除 ---- 驗證碼只能使用一次
CheckImg.java
package cn.binbin.com;import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random;import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;public class CheckImg extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 1、在內存中緩沖一張圖片int width = 120;int height = 30;BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);// 2、設置背景色Graphics2D graphics = (Graphics2D) image.getGraphics();graphics.setColor(Color.YELLOW);graphics.fillRect(0, 0, width, height);// 3、畫邊框 graphics.setColor(Color.BLUE);graphics.drawRect(0, 0, width - 1, height - 1);// 5、開始找字庫了String content = "ASDFGHJKJKLQWERTYUIOZXCVBNM1234567890";// 6、設置字體graphics.setFont(new Font("宋體", Font.BOLD, 20));// 如果驗證碼是中文,必須用中文的字庫Random random = new Random();int x = 10;int y = 20;StringBuffer str = new StringBuffer("");for (int i = 0; i < 4; i++) {int index = random.nextInt(content.length());char c = content.charAt(index);str.append(c);double angle = random.nextInt(60) - 30;// 這個地方是因為randomo不能生產負數double theta = angle / 180 * Math.PI;graphics.rotate(theta, x, y);graphics.drawString(c + "", x, y);graphics.rotate(-theta, x, y);x += 20;}String checkImg = str.toString();HttpSession httpSession = request.getSession();httpSession.setAttribute("checkImg", checkImg);// 7、繪制干擾線int x1, x2, y1, y2;graphics.setColor(Color.LIGHT_GRAY);for (int i = 0; i < 10; i++) {x1 = random.nextInt(width);x2 = random.nextInt(width);y1 = random.nextInt(height);y2 = random.nextInt(height);graphics.drawLine(x1, y1, x2, y2);}// 7、添加旋轉,用的是graphic2D來畫// 4、把內存中的圖片輸出到瀏覽器ImageIO.write(image, "jpg", response.getOutputStream());}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript">function change(){document.getElementById("img").src="/day7/CheckImg";} </script> </head> <body> <h2 style="color: red">${requestScope.msg}</h2> <form action="/day7/LoginServlet">姓名:<input type="text" name="username">密碼:<input type="password" name = "password">驗證碼:<input type="text" name = "checkimg"><img src="/day7/CheckImg" οnclick="change()"id = "img" style="cursor: pointer"><input type="submit" value="提交"> </form> </body> </html>
LoginServlet.java
package cn.binbin.com;import java.io.IOException;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {HttpSession httpSession = request.getSession();String name = request.getParameter("username");String password = request.getParameter("password");String checkImg = request.getParameter("checkimg");String checkImg2 = (String) httpSession.getAttribute("checkImg");httpSession.removeAttribute("checkImg");// 這個地方的銷毀是為了進行一次驗證// 這個地方驗證是有先后順序的if (checkImg.equals(checkImg2)) {if (name.equals("kongbin") && password.equals("kongbin")) {httpSession.setAttribute("name", name);response.sendRedirect("/day7/welcome.jsp");} else {request.setAttribute("msg", "用戶名或密碼錯誤");request.getRequestDispatcher("/login.jsp").forward(request,response);}} else {request.setAttribute("msg", "驗證碼錯誤");request.getRequestDispatcher("/login.jsp").forward(request,response);}// if ((name.equals("kongbin") && password.equals("kongbin"))// && (checkImg.equals(checkImg2))) {// httpSession.setAttribute("name", name);// response.sendRedirect("/day7/welcome.jsp");// } else {// response.sendRedirect("/day7/login.jsp");// } }public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}}
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>歡迎回來<%=request.getSession().getAttribute("name") %></h1>
</body>
</html>
?
?
---------------------------------------------------------------------------------------
Servlet 三種數據范圍
ServletContext
HttpServletRequest
HttpSession
三種數據范圍,每個對象各自維護類似map集合結構,具備相同幾個方法
setAttribute 存入一個屬性
getAttribute 取出一個屬性
removeAttribute 移除一個屬性
三種數據范圍對象 在哪些情況使用?
ServletContext 服務器啟動時創建,服務器關閉銷毀 所有Servlet共享 ---- 保存一些全局數據
例如:數據庫連接池、工程配置屬性、配置文件內容 ----- 一般不建議使用,服務器占用內存過多
HttpSession 在request.getSession時創建,在三種情況下銷毀 ----- 保存與用戶相關數據
例如:系統登陸信息、購物車數據
HttpServletRequest 在客戶端發起請求時,服務器創建對象,在響應結束時 對象銷毀 ----- 保存Servlet向JSP傳輸數據信息
例如:執行某個操作,Servlet將操作結果傳遞JSP (Servlet將登陸錯誤信息傳遞JSP )、Servlet將數據庫查詢結果傳遞JSP
用request的時候是比較多的
?
以上三種數據范圍 ServletContext > HttpSession > HttpServletRequest
* 宗旨 優先使用生命周期短的,降低服務器的壓力
總結 :
day5
1、編寫一個Servlet --- HelloWorld response.getWriter().print()
2、設置response編碼集,向瀏覽器輸入中文信息 response.setContentType("text/html;charset=utf-8");
3、將九九乘法表打印瀏覽器頁面 ----- 添加for循環
4、編寫頁面讓用戶輸入一個數字,在Servlet根據用戶輸入數字 打印乘法表
5、用戶輸入一段字母內容,在瀏覽器上輸入 每個字符出現幾次
6、網站訪問次數 (選做)
7、讀取web工程中一個文件 (getServletContext().getRealPath())
理論:
1、Servlet生命周期
2、ServletConfig和ServletContext
3、url-pattern 三種寫法
day6
1、重定向 ---- response.setStatus response.setHeader
2、自動跳轉 ---- refresh
3、禁用緩存 ---- 三個頭信息
4、文件下載 (復雜值得做)
5、驗證碼程序 整理一下 (不用重寫)
6、獲得當前訪問資源路徑 request.getRequestURI().substring(request.getContextPath().length());
7、獲得客戶端 IP request.getRemoteAddr()
8、防盜鏈 request.getHeader(referer)
9、獲得請求參數 亂碼問題 post get 亂碼解決 (昨天重點**** )
理論:
1、200、302 、304 、404 、500 意思
2、refrer refresh Content-Type Content-Disposition 禁止緩存三個頭 ----- 含義
3、get和post請求方式區別
4、亂碼發生原因 (掌握)
day7
1、記錄上次訪問時間 cookie
2、商品瀏覽記錄 cookie
3、商品購物車 session
4、登陸一次性驗證碼 session
理論:
1、cookie和session區別
2、會話cookie 和持久cookie
3、第一方cookie和第三方cookie
4、關閉瀏覽器是否清除cookie ?
5、持久cookie刪除
6、關閉瀏覽器,重新打開瀏覽器session還能繼續使用 ?原理 ?
7、瀏覽器禁用cookie,session還能否使用
8、session銷毀三種情況
9、一次性驗證碼原理
10、Servlet三種數據范圍區別