웹 사 이 트 는 지난번 방문 시간 servlet 실현 을 기억 합 니 다.
4
분석:쿠키 로 완성 가능
서버 에 servlet 에서 last time 이라는 쿠키 가 있 는 지 판단 합 니 다.
처음 방문 한 것 이 아 닙 니 다.
처음 방문
응답 데이터
쿠키
public class Cookietest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
response.setContentType("text/html;charset=utf-8");
boolean flag = false;
// cookie
Cookie[] cc = request.getCookies();
if(cc!=null && cc.length>0){
for(Cookie c : cc){
if("lastTime".equals(c.getName())){
// cookie value
// cookie
String v = c.getValue();
v= URLDecoder.decode(v,"utf-8");//url
response.getWriter().write(" "+v);
//
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
String st_date = sd.format(date);
st_date = URLEncoder.encode(st_date,"utf-8");//url
c.setValue(st_date);
response.addCookie(c);
flag=true;
// cookie
c.setMaxAge(60*60*24*30);
break;
}
}
}
if(cc == null || cc.length==0 || flag == false) {
//
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
String st_date = sd.format(date);
st_date = URLEncoder.encode(st_date,"utf-8");//url
Cookie lastTime = new Cookie("lastTime", st_date);
response.addCookie(lastTime);
// cookie
lastTime.setMaxAge(60*60*24*30);
st_date = URLDecoder.decode(st_date,"utf-8");
response.getWriter().write(" " + st_date+"");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
jsp 스타일
Title
0){
for(Cookie c : cc){
if("lastTime".equals(c.getName())){
// cookie value
String v = c.getValue();
v= URLDecoder.decode(v,"utf-8");//url
response.getWriter().write(" "+v);
// cookie
//
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
String st_date = sd.format(date);
st_date = URLEncoder.encode(st_date,"utf-8");//url
c.setValue(st_date);
response.addCookie(c);
flag=true;
// cookie
c.setMaxAge(60*60*24*30);
break;
}
}
}
if(cc == null || cc.length==0 || flag == false) {
//
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
String st_date = sd.format(date);
st_date = URLEncoder.encode(st_date,"utf-8");//url
Cookie lastTime = new Cookie("lastTime", st_date);
response.addCookie(lastTime);
// cookie
lastTime.setMaxAge(60*60*24*30);
st_date = URLDecoder.decode(st_date,"utf-8");
response.getWriter().write(" " + st_date+"");
}
%>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
5분 안에 JSP 배우기Java 생태계는 지난 20년 동안 많이 발전했으며 이는 시장에서 변함없이 인기를 얻고 있음으로 알 수 있습니다. 글쎄, 이것은 다른 프로그래밍 언어 중 하나에 대한 탄탄한 배경이 있거나 새로운 개발자이지만 시간이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.