Cookie
Cookie:小甜点 , 是客户端技术,将我们的共享数据保存到客户端(浏览器) 后面再请求的时候就会将共享数据发送到服务器.
此Cookie的作用就是 :我打个比方, 就是你去超市买东西,而你想要优惠的话就必须要办会员卡,而这会员卡就想Cookie,你下次再进来买东西就只要带会员卡就能证明你的身份了.
看了我上一张博客是有结构的,不懂结构的话可能会有点蒙:
@WebServlet("/cookie/login")
public class LoginServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求格式
req.setCharacterEncoding("utf-8");
//设置响应格式
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
//获取请求时的用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
//创建Cookie对象 这是重点
Cookie c = new Cookie("username",username);
resp.addCookie(c);
//验证用户名和密码 在此我账号密码写死
if("admin".equals(username)&&"1234".equals(password)){
//进入邮箱界面
out.println("<h1>欢迎登录:"+username+"的邮箱</h1>");
out.println("<a href='/dowork/cookie/list'>未读邮件</a>");
}else{
System.out.println("登录失败");
}
//关闭流
out.close();
}
}
@WebServlet("/cookie/list")
public class ListServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求的格式
req.setCharacterEncoding("utf-8");
//设置响应格式
resp.setContentType("text/html;charset=utf-8");
//获取cookie
Cookie[] cookies = req.getCookies();
String username = null;
for (Cookie cookie : cookies) {
if("username".equals(cookie.getName())){
username = cookie.getValue();
}
}
PrintWriter out = resp.getWriter();
out.println("欢迎查看:"+username+"邮件");
out.println("<a href = '/dowork/cookie/content'>邮件1</a>");
out.println("<a href = '/dowork/cookie/content'>邮件2</a>");
out.println("<a href = '/dowork/cookie/content'>邮件3</a>");
out.close();
}
}
/**
*
* @author AfricaYoung
* @Date 2016-08-15
*/
@WebServlet("/cookie/content")
public class ContentServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String username = null;
Cookie[] cookies = req.getCookies();
for (Cookie cookie : cookies) {
if("username".equals(cookie.getName())){
username = cookie.getValue();
}
}
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("最后一页啦:"+username+"大大");
out.print("<br/>宝宝炸了");
out.close();
}
}
Cookie 的缺陷:
1.Cookie中的共享数据的类型只能是String类型,如果需要共享多个数据,就需要创建多个Cookie对象,很麻烦
2.Cookie不支持中文
3.Cookie是保存在浏览器中,不安全
4.Cookie有数据大小的限制,一个Cookie支持4kb 一个浏览器中只能保存300个Cookie信息 一个应用在一个浏览器中最多保存20个Cookie
Session
Session:会话,在浏览器打开的时候创建,浏览器关闭的时候销毁 是一个服务端的技术,将共享数据保存在服务器中 Session底层就是Cookie
/**
*
* @author AfricaYoung
* @Date 2016-08-15
*/
@WebServlet("/session/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 设置请求格式
req.setCharacterEncoding("utf-8");
// 设置响应格式
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
// 获取请求时的用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
// 获取session
HttpSession session = req.getSession();
session.setAttribute("USERNAME_IN_SESSION", username);
// 修改共享数据
// 验证用户名和密码
if ("admin".equals(username) && "1234".equals(password)) {
// 进入邮箱界面
out.println("<h1>欢迎登录:" + username + "的邮箱</h1>");
out.println("<a href='/dowork/session/list'>未读邮件</a>");
// URL重写(浏览器可以禁用Cookie,这样我们的jsessionid就不能保存在浏览器中,那么服务器中的数据也就获取不到)
// 方式:使用URL重写,自动的根据浏览器是否禁用Cookie来拼接jsessionid
String url = resp.encodeURL("/dowork/session/list");
out.print("<a href='" + url + "'>收件箱(4)</a><br/>");
} else {
System.out.println("登录失败");
}
out.close();
}
}
@WebServlet("/session/list")
public class ListServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求的格式
req.setCharacterEncoding("utf-8");
//设置响应格式
resp.setContentType("text/html;charset=utf-8");
String username = (String) req.getSession().getAttribute("USERNAME_IN_SESSION");
PrintWriter out = resp.getWriter();
out.println("欢迎查看:"+username+"邮件");
out.println("<a href = '/dowork/session/content'>邮件1</a>");
out.println("<a href = '/dowork/session/content'>邮件2</a>");
out.println("<a href = '/dowork/session/content'>邮件3</a>");
out.close();
}
}
@WebServlet("/session/content")
public class ContentServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String username = (String) req.getSession().getAttribute("USERNAME_IN_SESSION");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("最后一页啦:"+username+"大大");
out.print("<br/>宝宝炸了");
out.close();
}
}