Request Dispatcher 不包括不执行文件但显示其源代码

问题描述 投票:0回答:1
Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","12345");
            Statement st =con.createStatement();
            String mi = "select * from empinfo where EEmailID = '"+email+"'";
            
            ResultSet rs = st.executeQuery(mi);
            if(rs.next()) {
            if(email.equals(rs.getString("EEMailID"))){
                 // Generate a random OTP
        Random random = new Random();
        int otp = 100000 + random.nextInt(900000);

        // Send OTP through email Calling Function
        sendEmail(otp, email);

        // Store OTP in the session for Verification
        HttpSession session = request.getSession();
        session.setAttribute("OTP", Integer.toString(otp));
        session.setAttribute("eemail",email);

        // Redirect the user to the OTP verification page
        response.sendRedirect("otp.jsp");
            }}
            else{
                out.println("Enter Registered Email Id only");
                RequestDispatcher dis= getServletContext().getRequestDispatcher("/forgotpassword.jsp");
                dis.include(request, response);
                }

它在输出中显示:-

仅输入注册的电子邮件 ID

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="bootstrap-4.0.0-dist/css/bootstrap.css" rel="stylesheet" type="text/css"/>
        <link href="bootstrap-4.0.0-dist/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
        <title>JSP Page</title>
    </head>
    <body>
       
        <center>
        <form action="OTPLoginServlet" method="post">
            <label for="email">Email:</label>
            <input type="email" id="email" name="email"><br><br>
            <input type="submit" value="Submit">
        </form>
    </center>
    </body>
</html>

上面是 forgotpassword.jsp 中的代码,它没有执行,只是显示而已。 那么,我能做些什么来解决这个错误呢?

java servlets servlet-3.0
1个回答
0
投票

使用以下方法。

不是从 getServletContext() 中检索“RequestDispatcher”的对象,而是从 HttpServletRequest 中检索相同的对象,例如 -

RequestDispatcher rd = request.getRequestDispatcher("/forgotpassword.jsp");
    
rd.include(request, response);
© www.soinside.com 2019 - 2024. All rights reserved.