Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
下载文件后如何将jsp中的变量写入HTMLdiv_Html_Jsp - Fatal编程技术网

下载文件后如何将jsp中的变量写入HTMLdiv

下载文件后如何将jsp中的变量写入HTMLdiv,html,jsp,Html,Jsp,我正在尝试构建一个非常简单的应用程序。将有一个密码字段。如果用户输入了错误的密码,则会显示一条消息“密码不正确”。如果密码正确,则我必须执行两项任务: 将下载一个文件(假设为A.txt)。该文件位于WEB-INF文件夹中 显示消息“密码正确” 我试图以以下方式显示该消息: <div style=""> <%=passwordMatchText%> </div> 当用户输入正确的密码时,文件将被下载,但div中的文本不会更改。jsp似乎没有在div中写

我正在尝试构建一个非常简单的应用程序。将有一个密码字段。如果用户输入了错误的密码,则会显示一条消息“密码不正确”。如果密码正确,则我必须执行两项任务:

  • 将下载一个文件(假设为A.txt)。该文件位于WEB-INF文件夹中

  • 显示消息“密码正确”

  • 我试图以以下方式显示该消息:

    <div style="">
      <%=passwordMatchText%>
    </div>
    
    
    
    当用户输入正确的密码时,文件将被下载,但div中的文本不会更改。jsp似乎没有在div中写入passwordMatchText的值。我已经用system.out.print()打印了文本;文本在console中打印正确,但在div中写入不正确

    我还尝试将该值放入会话变量并写入div,但没有一个有效。我怎样才能解决这个问题

    <%@ page trimDirectiveWhitespaces="true" %>
    <%@ page language="java" %>
    <%@ page import="java.util.ArrayList" %>
    <%@ page import="java.io.FileInputStream" %>
    <%@ page import="java.io.FileOutputStream" %>
    <%@ page import="java.io.File" %>
    
    <%
        String header=request.getHeader("user-agent");
        String fileName="a.txt";
    
        String originalPassword="1";
    
        String passwordMatchText="";
        String passwordTextStyle="";
    
        String password = request.getParameter("password");
    
    if(password==null  ||!password.equals(originalPassword)){
        passwordMatchText="Password does not match.";
        passwordTextStyle="color:red;";
    }
        else {
            passwordMatchText="Password matched.";
            passwordTextStyle="color:green;";
    
            String baseFilePath = request.getServletContext().getRealPath("/") + "WEB-INF/" + fileName;
    
            File newFile= new File(baseFilePath);
            if(newFile.exists()){
                ServletOutputStream outputStream = response.getOutputStream();
                FileInputStream inputStream = new FileInputStream(baseFilePath);
                int fileLength = inputStream.available();
    
                response.setContentType("application/octet-stream");
                response.setContentLength(fileLength);
                response.setHeader("Content-Disposition","attachment;filename=" + fileName);
    
                byte[] outputByte = new byte[4096];
                // copy binary content to output stream
                while (inputStream.read(outputByte, 0, 4096) != -1) {
                    outputStream.write(outputByte, 0, 4096);
                    //out.write(outputByte.toString());
                }
    
                inputStream.close();
                outputStream.flush();
                outputStream.close();
    
            }
            else {
                response.getWriter().println("File does not exists.");
            }
    
        }
    %><!DOCTYPE HTML>
    <html>
        <head>
            <html:base />
            <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
            <meta name="ProgId" content="FrontPage.Editor.Document">
            <meta name="viewport" content="width=device-width, initial-scale=1">
    
        </head>
        <body>
            <!-- header menu closed  -->
            <div > <!-- container for body -->
                <div  style="border:1px solid black;margin-top:10px;padding:10px;"> <!-- div for content -->
                    <form  action="test2.jsp" method="POST">
                    <div>
    
                            <div style=""><%=passwordMatchText%>
                            </div>
                        </div>  
    
                        <div >
                            <div sytle ="width:100px">
                                Enter Password :                
                            </div>
                            <div >
                                <input type="password" name="password" size="20" style="width: 100%;" />                    
                            </div>
                        </div>
                        <div >
                            <div >
                                <input class="cmd" type="submit" value="Submit" name="B2">
                            </div>
                        </div>                                  
                    </form>
                </div>
            </div>
            <!-- body closed  -->
    
        </body>
    </html>
    
    
    输入密码:
    
    使用自动提交可以解决此问题。例如:

    <%@ page trimDirectiveWhitespaces="true"%>
    <%@ page language="java"%>
    <%@ page import="java.util.ArrayList"%>
    <%@ page import="java.io.FileInputStream"%>
    <%@ page import="java.io.FileOutputStream"%>
    <%@ page import="java.io.File"%>
    
    <%
        String header = request.getHeader("user-agent");
                String fileName = "a.txt";
    
                String originalPassword = "1";
    
                String passwordMatchText = "";
                String passwordTextStyle = "";
    
                String password = request.getParameter("password");
    
                if ("".equals(password) && "val1".equals(request.getParameter("name1"))) {
                    String baseFilePath = request.getServletContext().getRealPath("/") + "/WEB-INF/" + fileName;
    
                    File newFile = new File(baseFilePath);
                    if (newFile.exists()) {
                        ServletOutputStream outputStream = response.getOutputStream();
                        FileInputStream inputStream = new FileInputStream(baseFilePath);
                        int fileLength = inputStream.available();
    
                        response.setContentType("application/octet-stream");
                        response.setContentLength(fileLength);
                        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
    
                        byte[] outputByte = new byte[4096];
                        // copy binary content to output stream
                        while (inputStream.read(outputByte, 0, 4096) != -1) {
                            outputStream.write(outputByte, 0, 4096);
                            //out.write(outputByte.toString());
                        }
    
                        inputStream.close();
                        outputStream.flush();
                        outputStream.close();
    
                    } else {
                        response.getWriter().println("File does not exists.");
                    }
                } else if (password == null || !password.equals(originalPassword)) {
                    passwordMatchText = "Password does not match.";
                    passwordTextStyle = "color:red;";
                } else {
                    passwordMatchText = "Password matched.";
                    passwordTextStyle = "color:green;";
    
                }
    %><!DOCTYPE HTML>
    <html>
    <head>
    <html:base />
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    </head>
    <body>
        <!-- header menu closed  -->
        <div>
            <!-- container for body -->
            <div style="border: 1px solid black; margin-top: 10px; padding: 10px;">
                <!-- div for content -->
                <form name="test2form" action="test2.jsp" method="POST">
                    <%
                        if (password != null && password.equals(originalPassword)) {
                    %>
                    <input type="hidden" name="name1" value="val1">
                    <%
                        }
                    %>
    
                    <div>
    
                        <div style="<%=passwordTextStyle%>"><%=passwordMatchText%>
                        </div>
                    </div>
    
                    <div>
                        <div sytle="width:100px">Enter Password :</div>
                        <div>
                            <input type="password" name="password" size="20"
                                style="width: 100%;" />
                        </div>
                    </div>
                    <div>
                        <div>
                            <input class="cmd" type="submit" value="Submit" name="B2">
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <!-- body closed  -->
    
    </body>
    <%
        if (password != null && password.equals(originalPassword)) {
    %>
    <SCRIPT language="JavaScript">
        document.test2form.submit();
    </SCRIPT>
    <%
        }
    %>
    
    
    输入密码:
    document.test2form.submit();
    
    response.setHeader(“内容处置”、“附件;文件名=“+filename”)