• jsp4 Cookie


    Screenshot_2016-05-04-11-11-52-576.png
    (1)Screenshot_2016-05-04-11-12-53-120.png
    (2)Session----Coolie
    (3)
    Screenshot_2016-05-04-11-15-08-930.png

    Screenshot_2016-05-04-11-16-16-891.png
    (4)Screenshot_2016-05-04-11-16-43-735.pngScreenshot_2016-05-04-11-17-38-989.png

    cookie保存
    index.jsp
    1. <%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8. <head>
    9. <base href="<%=basePath%>">
    10. <title>My JSP 'index.jsp' starting page</title>
    11. <meta http-equiv="pragma" content="no-cache">
    12. <meta http-equiv="cache-control" content="no-cache">
    13. <meta http-equiv="expires" content="0">
    14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    15. <meta http-equiv="description" content="This is my page">
    16. </head>
    17. <body>
    18. <h1>用户登录</h1>
    19. <hr>
    20. <%
    21. request.setCharacterEncoding("utf-8");
    22. String username = "";
    23. String password = "";
    24. Cookie[] cookies = request.getCookies();
    25. if (cookies != null && cookies.length > 0) {
    26. for (Cookie c : cookies) {
    27. if (c.getName().equals("username")) {//(名称)
    28. username=URLDecoder.decode(c.getValue(),"utf-8");
    29. }
    30. if( c.getName().equals("password")){
    31. password=URLDecoder.decode(c.getValue(),"utf-8");
    32. }
    33. }
    34. }
    35. %>
    36. <form action="dologin.jsp" method="post" name="loginForm">
    37. <table>
    38. <tr><td>用户名:</td><td><input type="text" name="username" value="<%=username %>" /> </td></tr>
    39. <tr><td>密码:</td><td><input type="password" name="password" value="<%=password %>" /></td></tr>
    40. <tr><td colspan="2" ><input type="checkbox" name="isUseCookie" checked="checkd" />十天内记住登陆状态</td></tr>
    41. <tr><td colspan="2" aligin="center"><input type="submit" value="登录"></td></tr>
    42. </table>
    43. </form>
    44. </body>
    45. </html>
    dologin.jsp
    1. <%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8. <head>
    9. <base href="<%=basePath%>">
    10. <title>My JSP 'dologin.jsp' starting page</title>
    11. <meta http-equiv="pragma" content="no-cache">
    12. <meta http-equiv="cache-control" content="no-cache">
    13. <meta http-equiv="expires" content="0">
    14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    15. <meta http-equiv="description" content="This is my page">
    16. </head>
    17. <body>
    18. <h1>登陆成功</h1> <br><br><br><br>
    19. <%
    20. request.setCharacterEncoding("utf-8");//防止中文乱码
    21. //首先判断用户是否选择了记录登陆状态
    22. String[] isUseCookie=request.getParameterValues("isUseCookie");//返回包含参数name的所有值的数组
    23. if(isUseCookie!=null&&isUseCookie.length>0){
    24. //吧用户名和密码保存在Cookie对象里面
    25. String username= URLEncoder.encode(request.getParameter("username"),"utf-8");//返回name指定函数的参数值
    26. //URLEncoder.encode(s, enc)防止中文乱码。。短码
    27. String password= URLEncoder.encode(request.getParameter("password"),"utf-8");
    28. Cookie usernameCookie=new Cookie("username",username);//创建cookie对象
    29. Cookie passwordCookie=new Cookie("password",password);
    30. usernameCookie.setMaxAge(8640000);//设置最大生存期限为10天(有效期)
    31. passwordCookie.setMaxAge(8640000);
    32. response.addCookie(usernameCookie);//加入cookie对象
    33. response.addCookie(passwordCookie);
    34. }else{
    35. Cookie[] cookies=request.getCookies();//读取cookie对象
    36. if(cookies!=null&&cookies.length>0){
    37. for(Cookie c:cookies){
    38. if(c.getName().equals("username")||c.getName().equals("password")){//(名称)
    39. c.setMaxAge(0);//设置cookie失效
    40. response.addCookie(c);//重新保存
    41. }
    42. }
    43. }
    44. }
    45. %>
    46. <a href="users.jsp" target="_blank">查看用户信息</a>
    47. </body>
    48. </html>
    users.jsp
    1. <%@ page language="java" import="java.util.*,java.net.*"
    2. contentType="text/html; charset=UTF-8"%>
    3. <%
    4. String path = request.getContextPath();
    5. String basePath = request.getScheme() + "://"
    6. + request.getServerName() + ":" + request.getServerPort()
    7. + path + "/";
    8. %>
    9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    10. <html>
    11. <head>
    12. <base href="<%=basePath%>">
    13. <title>My JSP 'users.jsp' starting page</title>
    14. <meta http-equiv="pragma" content="no-cache">
    15. <meta http-equiv="cache-control" content="no-cache">
    16. <meta http-equiv="expires" content="0">
    17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18. <meta http-equiv="description" content="This is my page">
    19. </head>
    20. <body>
    21. <h1>用户信息</h1>
    22. <br>
    23. <br>
    24. <br>
    25. <br>
    26. <%
    27. request.setCharacterEncoding("utf-8");
    28. String username = "";
    29. String password = "";
    30. Cookie[] cookies = request.getCookies();
    31. if (cookies != null && cookies.length > 0) {
    32. for (Cookie c : cookies) {
    33. if (c.getName().equals("username")) {//(名称)
    34. //解码用URLDecoder.decode(s, enc)
    35. username=URLDecoder.decode(c.getValue(),"utf-8");
    36. }
    37. if( c.getName().equals("password")){
    38. password=URLDecoder.decode(c.getValue(),"utf-8");
    39. }
    40. }
    41. }
    42. %>
    43. 用户名:<%=username %>
    44. <br> 密码:<%=password %>
    45. <br>
    46. </body>
    47. </html>
    (5)Session-Cookie





  • 相关阅读:
    梯度下降进阶
    梯度下降基础
    python---matplotlib
    python---numpy
    浅析Jupyter Notebook
    anaconda安装
    机器学习---导学
    python---线程与进程
    mapping values are not allowed in this context at line 115 column 10
    laravel进行单元测试的时候如何模拟数据库以及mockery的调用
  • 原文地址:https://www.cnblogs.com/liuruimiku/p/5459225.html
Copyright © 2020-2023  润新知