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)]