zoukankan      html  css  js  c++  java
  • session共享问题

    1.cookie和session的区别:

                                  session              cookie

    保存的位置               服务端                客户端

    保存的内容               Object                String

    2.

    String getContextPath() 虚拟路径

    String getRealPath(String name)虚拟路径相对的绝对路径(虚拟路径)

    index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.Date" %>
    <html>
      <head>
        <title>$Title$</title>
      </head>
      <body>
    <%="当前项目的虚拟路径:"+application.getContextPath()+"<br/>"%>
    <%="虚拟路径的绝对路径"+application.getRealPath("/untitled_war_exploded")%>
    <%! String name,pwd; %> 
    <% int flag=0; //在缓存中寻找用户名和密码 Cookie[] cookies=request.getCookies();
    for(Cookie cookie:cookies)

    { if(cookie.getName().equals("uname"))
    { name=cookie.getValue(); flag++;
    }
    if(cookie.getName().equals("upwd"))

    { pwd=cookie.getValue(); flag++; }
    } if(flag!=2)

    { out.print("cookie fail"); }

    else out.print("cookie success"); %>
    <form action="register.jsp"method="post">
    用户名:<input type="text" name="uname" value="<%=(name==null?"":name)%>"><br/>
    密码:<input type="password" name="upwd" value="<%=(pwd==null?"":pwd)%>"><br/>
    <input type="submit" value="register"><br/>
    </form>
    </body>
    </html>

    register.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%
        //设置编码
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("uname");
        String pwd=request.getParameter("upwd");
        Cookie cookie=new Cookie("uname",name);
        Cookie cookie1=new Cookie("upwd",pwd);
        response.addCookie(cookie);
        response.addCookie(cookie1);
        //response.sendRedirect("success.jsp");
        if(name.equals("z")&&pwd.equals("z")){
            //只有登入成功,session中才会存在uname/upwd
            session.setAttribute("uname",name);
            session.setAttribute("upwd",pwd);
            session.setMaxInactiveInterval(60*30);
            //response.sendRedirect("success.jsp");
            out.print("sessionId   "+session.getId());
            Cookie cookie2=new Cookie("uname",name);//cookie服务端产生
            response.addCookie(cookie2);
            request.getRequestDispatcher("success.jsp").forward(request,response);//响应给客户端
        }else
            response.sendRedirect("index.jsp");
    %>
    <br/>
    </body>
    </html>

    success.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>title</title>
    </head>
    <body>
    登入成功!!
    <%
        out.println("sessionId   "+session.getId());
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie:cookies){
            if(cookie.getName().equals("JSESSIONID")){
                out.print("JSESSIONID   "+cookie.getValue());
            }
        }
        //String name=request.getParameter("uname");
        String name=(String)session.getAttribute("uname");
        //如果用户没有登录,而是通,过地址栏访问success.jsp,则必然获取到的name值为null
        //如果没有登入,应该跳转登录页面
        if(name!=null)
        {out.println("登入成功用户"+name);
        %>
    <a href="invalidate.jsp">
    注销
    </a>
    <%
        } else
            response.sendRedirect("login.jsp");
    %>
    </body>
    </html>

    参考:http://www.xuehuile.com/blog/fb1da44cf8b943d5a1a09b234bd1b12d.html

  • 相关阅读:
    Python数据分析入门
    非常干货之Python资源大全
    机器学习实战笔记之非均衡分类问题
    2^x mod n = 1 【杭电-HDOJ-1395】 附题
    android学习--视图列表(ListView和ListActivity)
    C++第13周(春)项目1
    多个线程怎样操作同一个epoll fd
    UVA
    自己定义progressdialog,改善用户体验
    总结个人项目设计保障5大原则
  • 原文地址:https://www.cnblogs.com/zuiaimiusi/p/11478874.html
Copyright © 2011-2022 走看看