forked from DinnerPig/Dubbo-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathETagUtil.java
More file actions
32 lines (29 loc) · 1.17 KB
/
ETagUtil.java
File metadata and controls
32 lines (29 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.ziyuan.etag;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
public class ETagUtil {
public static boolean judgeEtag(HttpServletRequest request, HttpServletResponse response, String tag) {
response.setHeader("ETag", tag);
String previousTag = request.getHeader("If-None-Match");
boolean flag = false;
if (previousTag != null && previousTag.equals(tag)) {
try {
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
// use the same date we sent when we created the ETag the first time through
response.setHeader("Last-Modified", request.getHeader("If-Modified-Since"));
flag = true;
} catch (IOException e) {
flag = false;
}
} else {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MILLISECOND, 0);
Date lastModified = cal.getTime();
response.setDateHeader("Last-Modified", lastModified.getTime());
}
return flag;
}
}