一.JSP之Cookie
1.cookie实现思维导图
2.jsp页面实例
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Cookie user = new Cookie("user","hly");
Cookie password = new Cookie("password","123");
/*设置有效期*/
user.setMaxAge(60*60);
password.setMaxAge(60*60);
/*添加cookie*/
response.addCookie(user);
response.addCookie(password);
%>
<html>
<head>
<title>coolie</title>
</head>
<body>
<%
/*获得所有cookie*/
Cookie[] cookies = request.getCookies();
for(int i = 0 ;i<cookies.length;i++){
out.println(cookies[i].getName()+" "+cookies[i].getValue());
if(cookies[i].getName().compareTo("message")==0){
cookies[i].setMaxAge(0);
/*google设置路径才能删除*/
cookies[i].setPath("/");
response.addCookie(cookies[i]);
out.println("已将删除cookie:"+cookies[i].getName());
}
}
%>
</body>
</html>
3.cookie保存表单数据实例
前端jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>myCookie</title>
</head>
<body>
<form action="/MyCookie" method="get">
<input type="text" placeholder="userId" name="userId">
<input type="text" placeholder="userPassword" name="userPassword">
<input type="submit" value="提交">
</form>
</body>
</html>
后端Servlet接收数据,然后就能在cookie里面查找到数据了
package com.hly.jsp.cookie;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author :hly
* @github :github.com/SiriusHly
* @blog :blog.csdn.net/Sirius_hly
* @date :2018/8/18
*/
@WebServlet("/MyCookie")
public class MyCookie extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String userId = request.getParameter("userId");
String userPassword = request.getParameter("userPassword");
System.out.println(userId+" "+userPassword);
/*创建cookie对象*/
Cookie id= new Cookie("id",userId);
Cookie password = new Cookie("pw",userPassword);
id.setMaxAge(60);
password.setMaxAge(60);
/*添加cookie*/
response.addCookie(id);
response.addCookie(password);
PrintWriter out = response.getWriter();
out.println("success");
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException {
doGet(request,response);
}
}
二.JSP值Session
1.session思维导图
2.session代码实例
页面运行时应先执行以下页面代码,当用户第一次浏览网页后,session的值就会发生改变,如果获取一个空的session值,就会出现500报错。
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
Date createTime = new Date(session.getCreationTime());
Date lastTime = new Date(session.getLastAccessedTime());
Integer visitNum = 0;
if(session.isNew()){
session.setAttribute("visitNum",visitNum);
}
else{
visitNum = (Integer) session.getAttribute("visitNum");
/*词句需要放在一开始运行的网页,不然会出现空指针报错*/
visitNum+=1;
session.setAttribute("visitNum",visitNum);
}
%>
<html>
<head>
<title>mySession</title>
</head>
<body>
<form action="session.jsp" method="get">
<input type="text" placeholder="userId" name="userId">
<input type="submit" value="提交">
</form>
</body>
</html>
跳转到的页面为:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.io.*,java.util.*" %>
<%
String userId = request.getParameter("userId");
Integer visitNum = (Integer) session.getAttribute("visitNum");
%>
<html>
<head>
<title>session</title>
</head>
<body>
欢迎<%=userId%>第<%=visitNum%>次访问!
</body>
</html>