发表于: 2017-04-29 23:20:46
1 1367
今天完成的事:
1、学习cookie,session概念及其区别
2、照着例子在jsp中生成cookie。
@RequestMapping("/test")
public String testcookie(HttpServletRequest request, HttpServletResponse response){
log.info("testcookie方法被调用");
// Cookie[] cookies=request.getCookies();
// for (Cookie cookie:cookies) {
//// log.info("cookie的内容为:"+cookie);
// }
return "test";
}
@RequestMapping("/main")
public String main(HttpServletRequest request,HttpServletResponse response){
return "main";
}
@RequestMapping("cookie")
public String cookie(HttpServletRequest request,HttpServletResponse response){
return "cookie";
}
}
(main。jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %>
<%
// 编码,解决中文乱码
String str = URLEncoder.encode(request.getParameter("name"),"utf-8");
// 设置 name 和 url cookie
Cookie name = new Cookie("name",
str);
Cookie url = new Cookie("url",
request.getParameter("url"));
// 设置cookie过期时间为24小时。
name.setMaxAge(60*60*24);
url.setMaxAge(60*60*24);
// 在响应头部添加cookie
response.addCookie( name );
response.addCookie( url );
%>
<html>
<head>
<title>设置 Cookie</title>
</head>
<body>
<h1>设置 Cookie</h1>
<ul>
<li><p><b>网站名:</b>
<%= request.getParameter("name")%>
</p></li>
<li><p><b>网址:</b>
<%= request.getParameter("url")%>
</p></li>
</ul>
</body>
</html>
(test.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%--<!DOCTYPE html>--%>
<html>
<head>
<%--<meta charset="utf-8">--%>
<title>菜鸟教程</title>
</head>
<body>
<form action="/Task01/main" method=GET>
站点名: <input type="text" name="name">
<br />
网址: <input type="text" name="url" />
<input type="submit" value="提交" />
</form>
</body>
</html>
(cookie.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>获取 Cookie</title>
</head>
<body>
<%
Cookie cookie = null;
Cookie[] cookies = null;
// 获取cookies的数据,是一个数组
cookies = request.getCookies();
if( cookies != null ){
out.println("<h2> 查找 Cookie 名与值</h2>");
for (int i = 0; i < cookies.length; i++){
cookie = cookies[i];
out.print("参数名 : " + cookie.getName());
out.print("<br>");
out.print("参数值: " + URLDecoder.decode(cookie.getValue(), "utf-8") +" <br>");
out.print("------------------------------------<br>");
}
}else{
out.println("<h2>没有发现 Cookie</h2>");
}
%>
</body>
</html>
明天计划的事:明天请假。
问题:对概念的东西理解比较慢。
收获:没感觉
总结:好好学习
评论