session
session
芥末小弟 发表于8个月前
session
  • 发表于 8个月前
  • 阅读 0
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

session
    就是cookie技术不能满足所有需求.cookie 不够安全.记录的信息过于简单.
    于是我们需要学习session技术.
    session技术是在服务器端保存信息的技术.
    session技术 可以理解为 在服务器端开辟了一个空间给你装用户的信息.
    1.什么时候创建这个空间呢? ==>当浏览器 第一次访问我们的项目时.会给这个浏览器开辟一个空间(MAP).并把这个空间对应的ID 发送给浏览器.
    2.之后浏览器每次访问都带着这个ID ==> 服务器就可以根据浏览器带过来的ID查找到对应session域.
    3.session什么时候销毁呢? ==>  默认30 分钟后销毁 ,依据如下配置.
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    细节:因为使用cookie记录了session的ID,而记录seesionID的Cookie有效时间是浏览器打开时,浏览器一旦关闭cookie丢失,sessionID丢失,服务器中的session就永远获得不到了.
session操作
Seesion的例子
    1.购物车
    2.验证码

image

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort() + path + "/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<script> function fun1(obj) { obj.src="<%=path %>/DServlet?random=" + new Date(); } </script>
</head>

<body>
    <div style="color:red">
        <% String error = (String) request.getAttribute("error"); if (error != null) { out.println(error); } %>

    </div>
    <form action="<%=path%>/CServlet" method="post"> 用户名:<input type="text" name="userName" /><br /> 密码:<input type="password" name="pwd" /><br /> 验证码:<input type="text" name="code"><img src="<%=path %>/DServlet" onclick="fun1(this);" /><br /> <input type="submit" value="提交">

    </form>
</body>
</html>
package cn.jiemoxiaodi.session; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = (String) request.getParameter("code"); String sessionCode = (String) request.getSession().getAttribute("code"); if(code!=null&&code.equalsIgnoreCase(sessionCode)){ response.sendRedirect(request.getContextPath()+"/index.jsp"); }else{ request.setAttribute("error", "对不起,您输入的恶有错误"); request.getRequestDispatcher("/index.jsp").forward(request, response); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
package cn.jiemoxiaodi.session; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.dsna.util.images.ValidateCode; public class DServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ValidateCode vacode = new ValidateCode(122, 33, 2, 1); response.setContentType("imge/jpeg"); String code = vacode.getCode(); request.getSession().setAttribute("code", code); OutputStream os = response.getOutputStream(); vacode.write(os); } }
共有 人打赏支持
粉丝 0
博文 202
码字总数 120715
×
芥末小弟
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: