JSP与Session

 

 案例:验证码的实现  案例重点:理解重定向和服务器转发的区别   使用session存储和取出数据

登录页面
<%--
  Created by IntelliJ IDEA.
  User: bed
  Date: 2021/10/26
  Time: 20:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
    <script>
        window.onload=function () {
            document.getElementById("img").onclick=function () {
                this.src="/day16/CheckcodeDemo04?time="+new Date().getTime();
            }
        }
    </script>
    <style>
        div{color: coral}
    </style>
</head>
<body>
<form action="/day16//LoginServlet" method="post">
    <table>
        <tr>
            <td>用户名</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td>验证码</td>
            <td><input type="text" name="checkcode"></td>
        </tr>
        <tr>
           <td colspan="2"><img id="img" src="/day16/CheckcodeDemo04"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="登录"></td>
        </tr>
    </table>
</form>>
        <div><%=request.getAttribute("checkcode error")%></div>
        <div><%=request.getAttribute("namepass error")%></div>
</body>
</html>

服务器内容
package cn.bed.checkcodeV;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkcode = request.getParameter("checkcode");
        HttpSession session=request.getSession();
        String checkcode_session = (String) request.getSession().getAttribute("checkCode_session");
        if(checkcode_session.equalsIgnoreCase(checkcode)){
            if("bed".equals(username)&&"bed123".equals(password)){
                session.setAttribute("user",username);
                response.sendRedirect(request.getContextPath()+"/success.jsp");
            }else{
                request.setAttribute("namepass error","用户名或密码错误");
                request.getRequestDispatcher("/Login.jsp").forward(request,response);
            }
        }else{
            request.setAttribute("checkcode error","验证码错误");
            request.getRequestDispatcher("/Login.jsp").forward(request,response);
        }
    }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值