HttpSession

HttpSession 服务端的技术

服务器会为每一个用户创建一个独立的HttpSession

/**
 * @author lanou
 * HttpSession原理:
 * 当用户第一次访问Servlet时,服务器端会给该用户创建一个独立的Session,并且生成一个SessionID
 * 这个SessionID在响应浏览器的时候,会被装进cookie中,从而被保存到浏览器中
 * 当用户再一次访问Servlet的时候,请求会携带着cookie中的SessionID去访问
 * 服务器会根据这个SessionID去查看是否有对应的 Session对象
 * 有,就拿出来使用;没有,就创建一个Session(相当于用第一次访问)(看病案例)
 * 
 * 域的范围
 * Context域 > Session域 > Request域
 * Session域,只要会话不结束就会存在,但是Session有默认的存活时间 30分钟
 * 
 */
public class Demo01 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获取参数
        String username = request.getParameter("username");

        //获取session对象
        HttpSession session = request.getSession();
        //保存数据
        session.setAttribute("username", username);
        System.out.println(session.getId());
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
/**
 * 测试获取session域中的数据
 * @author lanou
 *
 */
public class Demo02 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获取session域中的数据
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        System.out.println(username);
        System.out.println(session.getId());
        //响应到网页上
        response.getWriter().write(username+" "+session.getId());

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

人类与计算机的图灵测试

1.

public class DoLogin extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    // 获取到请求过来的参数(表单提交过来的数据)
    String userName = request.getParameter("userName");
    String pwd = request.getParameter("pwd");
    String code = request.getParameter("code");
    // 获取域中的session
    HttpSession session = request.getSession();
    String wcode = (String) session.getAttribute("wcode");
    PrintWriter out = response.getWriter();
    //连接数据库
    if (userName.equals("zhangsan") && pwd.equals("123")) {
        //判断验证码,忽略大小写
        if (!code.equalsIgnoreCase(wcode)) {
            //提示用户 验证码错误
            //把错误信息从doLogin页面传到1.jsp页面上
            //使用request域保存验证码错误的信息
            request.setAttribute("msg", "验证码错误");
            //请求转发
            request.getRequestDispatcher("1.jsp").forward(request, response);

        }
    } else {
        // 登录失败
        // 两秒回登录页面
        response.setHeader("refresh", "2;url="+request.getContextPath()+"/1.jsp");
    }
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
}

}

2.
public class CodeServlert extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //使用jar生成验证码
        ValidateCode vCode = new ValidateCode(100, 25, 4, 9);
        //获取生成的验证码字符串
        String code = vCode.getCode();
        //传值方式 1.拼接网址字符串 2.使用域对象
        //使用session来储存验证码
        request.getSession().setAttribute("wcode", code);
        System.out.println(code);
        //写到网页上(通过响应中的字符流 写回网页)
        vCode.write(response.getOutputStream());
    }

    private void fun(HttpServletResponse response) throws IOException {
        int width = 110;
        int height = 25;
        // 在内存中创建一个图像对象
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        // 创建一个画笔
        Graphics g = img.getGraphics();

        // 给图片添加背景色
        g.setColor(Color.PINK);// 设置一个颜色
        g.fillRect(1, 1, width - 2, height - 2);// 填充颜色

        // 给边框一个色
        g.setColor(Color.RED);
        g.drawRect(0, 0, width - 1, height - 1);// 设置边框的显示坐标

        // 设置文本样式
        g.setColor(Color.BLUE);
        g.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 15));

        // 给图片添加文本
        Random rand = new Random();
        int position = 20;
        for (int i = 0; i < 4; i++) {
            g.drawString(rand.nextInt(10) + "", position, 20);// 给图片填充文本
            position += 20;
        }

        // 添加9条干扰线
        for (int i = 0; i < 9; i++) {
            g.drawLine(rand.nextInt(width), rand.nextInt(height), rand.nextInt(width), rand.nextInt(height));
        }
        // 将图片对象以流的方式输出的客户端
        ImageIO.write(img, "jpg", response.getOutputStream());
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

3.jsp文件

<script type="text/javascript">
    function changeCode() {
        /* 从文档中取出img图片*/
        var img = document.getElementsByTagName("img")[0];
        /* 获取图片的src属性,并赋值
         注意:如果请求的网址完全相同,则浏览器不会帮你刷新
         可以拼接当前的时间,让每次请求的网址都不一样
        */
        img.src = "/java-web-servlet-3.29/codeServlet?time=" + new Date().getTime();
    }
</script>
</head>
<body>
    <%
       //书写java代码
       //获取request域中的数据
       String msg = (String)request.getAttribute("msg");
       if(msg != null){
             out.write(msg);
       }


    %>
    <form action="/java-web-servlet-3.29/doLogin" method="post">
        用户名:<input type="text" name="userName"/><br>
        密码:<input type="password" name="pwd"/><br>
        验证码:<input type="text" name="code"/>
        <img src="/java-web-servlet-3.29/codeServlet" onclick="changeCode()"/>
        <a href="javascript:changeCode()">看不清换一张</a>
        <br>
        <input type="submit" value="登录"/><br>
    </form>
</body>

4.
配置web.xml

【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值