JSP概念

JSP脚本

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
System.out.println("hello,jsp");
int i=5;
String contextPath = request.getContextPath();
out.print(contextPath);
%>
<br>
<%!
int i=3;
%>
<%=
"hello"
%>
</body>
</html>
JSP内置对象

案例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//获取所有cookie
Cookie[] cookies = request.getCookies();
boolean flag=false; //没有
//遍历数组
if(cookies!=null&&cookies.length>0){
for(Cookie cookie:cookies){
//获取cookie名称
String name = cookie.getName();
//判断名称是否为lastTime
if("lastTime".equals(name)){
//有cookie,不是第一次访问
flag=true;
//响应数据
//获取cookie的value,时间
String value = cookie.getValue();
System.out.println("解码前"+value);
//URL解码
value= URLDecoder.decode(value,"utf-8");
System.out.println("解码后"+value);
out.write("<h1>欢迎回来,您上次访问时间为:"+value+"</h1>");
//设置cookie的value
//获取当前时间的字符串,重新设置cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date);
System.out.println("编码前"+str_date);
//URL编码
str_date= URLEncoder.encode(str_date,"utf-8");
System.out.println("编码后"+str_date);
cookie.setValue(str_date);
//设置cookie的存活时间
cookie.setMaxAge(60*60*24*30); //一个月
response.addCookie(cookie);
break;
}
}
}
if(cookies==null||cookies.length==0||flag==false){
//没有,第一次访问
//设置cookie的value
//获取当前时间的字符串,重新设置cookie的值,重新发送cookie
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String str_date = sdf.format(date);
System.out.println("编码前"+str_date);
//URL编码
str_date= URLEncoder.encode(str_date,"utf-8");
System.out.println("编码后"+str_date);
Cookie cookie=new Cookie("lastTime",str_date);
//设置cookie的存活时间
cookie.setMaxAge(60*60*24*30); //一个月
response.addCookie(cookie);
out.write("<h1>您好,欢迎首次访问</h1>");
}
%>
</body>
</html>
JSP指令

JSP 注释

JSP 内置对象
