0830
response的運行流程:
1.客戶端發出http請求
2.tomcat解析請求的資源
3.將請求的信息封裝城一個request對象,同時創建一個response對象
4.創建servlet對象
5.調用service方法,當service調用結束後,方法返回.會從tomcat內核的response緩存區獲取設置的內容(其中PrintWriter流、ServletOutputStream流的writer方法會將內容寫入response緩衝區。)
6.tomcat服務器會從response獲取內容,將響應的內容封裝起來,返回一個http響應(相應行,響應頭,響應體),傳遞到客戶端,遊覽器將對內容進行解析.
BUG問題
1.該問題是無法利用new Date對cookies設置值.能打印,轉爲字符串也行,代碼真沒問題但是就是不行.
解決方法:利用system.cruentTimeMillis獲取秒數,然後利用new Date()的構造函數解決.
但是我總感覺不是代碼出現了問題,而是…自己試試我也想知道
修改之前:
/*
* 使用Cookie記錄客戶端上次訪問的時間
*
* 思路:
* 訪問時獲取想要的Cookie 如果沒有獲取到說明是第一次訪問,把當前時間存到一個Cookie中響應給客戶端
* 如果獲取到了,從cookie中取時間(上次訪問時間),再把當前時間存入Cookie中
*
* */
public class CookieDemo4Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
Cookie[] cs = request.getCookies();
Cookie c = CookieUtils.findCookieByName("lasttime", cs);
/*if(cs!=null){
for(Cookie cc:cs){
if(cc.getName().equals("lasttime")){
c = cc;
break;
}
}
}*/
if(c!=null){
out.print("<h1>上次訪問時間爲:"+c.getValue()+"</h1>");
}else{ //第一次訪問
out.print("<h1>歡迎第一次訪問</h1>");
}
Cookie cookie = new Cookie("lasttime",new Date()+"");
cookie.setMaxAge(10000);
response.addCookie(cookie);
}
}
修改之後:
/*
* 使用Cookie記錄客戶端上次訪問的時間
*
* 思路:
* 訪問時獲取想要的Cookie 如果沒有獲取到說明是第一次訪問,把當前時間存到一個Cookie中響應給客戶端
* 如果獲取到了,從cookie中取時間(上次訪問時間),再把當前時間存入Cookie中
*
* */
@WebServlet("/CookieDemo4Servlet")
public class CookieDemo4Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
Cookie[] cs = request.getCookies();
Cookie c = CookieUtils.findCookieByName("lasttime", cs);
/*if(cs!=null){
for(Cookie cc:cs){
if(cc.getName().equals("lasttime")){
c = cc;
break;
}
}
}*/
if(c!=null){
long cookieValue = Long.parseLong (c.getValue ( )); //得到用戶的上次訪問時間
Date date = new Date (cookieValue); //將long轉換爲日期
out.print("<h1>上次訪問時間爲:"+date+"</h1>");
}else{ //第一次訪問
out.print("<h1>歡迎第一次訪問</h1>");
}
Cookie cookie = new Cookie("lasttime",System.currentTimeMillis () + "");
cookie.setMaxAge(60*20*24);
cookie.setPath ("/");
response.addCookie(cookie);
}
}
涉及到的cookie工具類
public class CookieUtils {
public static Cookie findCookieByName(String name,Cookie[] cs){
if(cs!=null){
for(Cookie c:cs){
if(name.equals(c.getName())){
return c;
}
}
}
return null;
}
}
Java8新特性LocalDateTime類(順便教你讀源碼)
/**
* A date-time without a time-zone in the ISO-8601 calendar system,
* such as {@code 2007-12-03T10:15:30}.
* <p>
* {@code LocalDateTime} is an immutable date-time object that represents a date-time,
* often viewed as year-month-day-hour-minute-second. Other date and time fields,
* such as day-of-year, day-of-week and week-of-year, can also be accessed.
* Time is represented to nanosecond precision.
* For example, the value "2nd October 2007 at 13:45.30.123456789" can be
* stored in a {@code LocalDateTime}.
* <p>
* This class does not store or represent a time-zone.
* Instead, it is a description of the date, as used for birthdays, combined with
* the local time as seen on a wall clock.
* It cannot represent an instant on the time-line without additional information
* such as an offset or time-zone.
* <p>
* The ISO-8601 calendar system is the modern civil calendar system used today
* in most of the world. It is equivalent to the proleptic Gregorian calendar
* system, in which today's rules for leap years are applied for all time.
* For most applications written today, the ISO-8601 rules are entirely suitable.
* However, any application that makes use of historical dates, and requires them
* to be accurate will find the ISO-8601 approach unsuitable.
*
* <p>
* This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
* class; use of identity-sensitive operations (including reference equality
* ({@code ==}), identity hash code, or synchronization) on instances of
* {@code LocalDateTime} may have unpredictable results and should be avoided.
* The {@code equals} method should be used for comparisons.
*
* @implSpec
* This class is immutable and thread-safe.
*
* @since 1.8
*/
1.讀源碼之前看看該類的註釋,英文不好沒有關係(有道翻譯),咱們看重點
{@code LocalDateTime} is an immutable date-time object that represents a date-time是一個不可變的日期時間對象,表示日期時間
Time is represented to nanosecond precision.時間以納秒精度表示
This class is immutable and thread-safe.這個類是不可變的,線程安全的
咱們可以大概總結一下就是,這個新特性的類是不可變的日期對象,時間精度到達納秒級別,而且最重要的是線程安全.
2.不管是idea還是eclipse都可以打開一個類的方法列表,咱們先來看一下.第一眼從詞彙中就可以讀出來,無法就是各種日期的構造函數,及獲取時間屬性字段的方法.到了這一步就算是對該類有了初步的認識.
3.讀源碼不是讀全部,是挑重點讀,讀重點,加猜想.你不可能讀全部內容,猜讀和精讀結合然後返回的去驗證所猜所想,大膽的去讀去猜就完事啦.說的跟屁話一樣,實際上真的就是屁話.我看懂的話還需要你來指點江山.先繼續往下走,你就明白啦
進入正題3.1
讀實現 讀繼承關係,看看該類繼承何處.localDateTime繼承一堆***,我就看懂一個serializable序列化,說明該類可以被序列化.那麼它肯定有一個序列化id號.
沒錯下面果然有serversionID,其他的可能繼承與跟date,time有關的接口或者抽象類,真是是什麼咱不用去看和咱沒有太打關係.用到纔讀,不用不讀.記住啦沒.
除了序列化id號還有其他關於時間,日期的成員變量說明該類與Local’Date,LocalTime類存在依賴關係.間接的說明了該類的出現可能是對這些做了優化,不然你寫它幹什麼. 已經存在的事情如果還需要再次做一遍那就是因爲它做的不夠好.
4.找一個方法來讀吧.看下面這個方法從字面上可以知道應該是關於日期格式的問題,英文好還是很重要的.這是對日期格式的轉化.咱們看看的內部方法
5.看不懂呀,是我也看不懂,看看上的註解.
* @param <T> the type of the parsed date-time
* @param text the text to parse, not null
* @param query the query defining the type to parse to, not null
* @return the parsed date-time, not null
* @throws DateTimeParseException if unable to parse the requested result
第一個參數應該是傳入一個date-time類型的參數,對把
第二個參數是文本格式,對,肯定是,因爲這個方法不就是日期格式轉換嗎
它會返回一個data-time類型的結果.
會拋出異常
總上可知,該類就是一個日期格式轉換類,傳入日期,制定格式,返回相應日期類型.
文件下載
1.告訴遊覽器的文件類型response.setContentType(文件的MIME類型)\
2.告訴遊覽器的下載方式
response.setHeader(“Content-Disposition”,“attachment;filename=文件名稱”)
關於base64編碼Encode和Decode編碼
轉載地址:https://magiclen.org/java-base64/
[外鏈圖片轉存失敗(img-UNnLyC1x-1567279481891)(C:\Users\15566\AppData\Roaming\Typora\typora-user-images\1567259933100.png)]