码迷,mamicode.com
首页 > 其他好文 > 详细

session共享问题

时间:2019-09-07 00:30:59      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:ref   注销   context   real   没有   BMI   max   std   value   

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

session共享问题

标签:ref   注销   context   real   没有   BMI   max   std   value   

原文地址:https://www.cnblogs.com/zuiaimiusi/p/11478874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!